@fluidframework/tree-agent 2.63.0-359461 → 2.63.0-359734

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 (70) hide show
  1. package/api-report/tree-agent.alpha.api.md +9 -2
  2. package/dist/agent.d.ts +0 -3
  3. package/dist/agent.d.ts.map +1 -1
  4. package/dist/agent.js +21 -49
  5. package/dist/agent.js.map +1 -1
  6. package/dist/alpha.d.ts +1 -0
  7. package/dist/api.d.ts +18 -7
  8. package/dist/api.d.ts.map +1 -1
  9. package/dist/api.js.map +1 -1
  10. package/dist/index.d.ts +1 -0
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +3 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/langchain.d.ts.map +1 -1
  15. package/dist/langchain.js +2 -3
  16. package/dist/langchain.js.map +1 -1
  17. package/dist/prompt.d.ts.map +1 -1
  18. package/dist/prompt.js +15 -17
  19. package/dist/prompt.js.map +1 -1
  20. package/dist/ses.d.ts +21 -0
  21. package/dist/ses.d.ts.map +1 -0
  22. package/dist/ses.js +64 -0
  23. package/dist/ses.js.map +1 -0
  24. package/dist/utils.d.ts +4 -0
  25. package/dist/utils.d.ts.map +1 -1
  26. package/dist/utils.js +16 -1
  27. package/dist/utils.js.map +1 -1
  28. package/lib/agent.d.ts +0 -3
  29. package/lib/agent.d.ts.map +1 -1
  30. package/lib/agent.js +22 -50
  31. package/lib/agent.js.map +1 -1
  32. package/lib/alpha.d.ts +1 -0
  33. package/lib/api.d.ts +18 -7
  34. package/lib/api.d.ts.map +1 -1
  35. package/lib/api.js.map +1 -1
  36. package/lib/index.d.ts +1 -0
  37. package/lib/index.d.ts.map +1 -1
  38. package/lib/index.js +1 -0
  39. package/lib/index.js.map +1 -1
  40. package/lib/langchain.d.ts.map +1 -1
  41. package/lib/langchain.js +2 -3
  42. package/lib/langchain.js.map +1 -1
  43. package/lib/prompt.d.ts.map +1 -1
  44. package/lib/prompt.js +15 -17
  45. package/lib/prompt.js.map +1 -1
  46. package/lib/ses.d.ts +21 -0
  47. package/lib/ses.d.ts.map +1 -0
  48. package/lib/ses.js +60 -0
  49. package/lib/ses.js.map +1 -0
  50. package/lib/utils.d.ts +4 -0
  51. package/lib/utils.d.ts.map +1 -1
  52. package/lib/utils.js +14 -0
  53. package/lib/utils.js.map +1 -1
  54. package/package.json +10 -10
  55. package/src/agent.ts +29 -61
  56. package/src/api.ts +19 -12
  57. package/src/index.ts +1 -0
  58. package/src/langchain.ts +2 -3
  59. package/src/prompt.ts +15 -17
  60. package/src/ses.ts +73 -0
  61. package/src/utils.ts +14 -0
  62. package/dist/functionParsing.d.ts +0 -13
  63. package/dist/functionParsing.d.ts.map +0 -1
  64. package/dist/functionParsing.js +0 -215
  65. package/dist/functionParsing.js.map +0 -1
  66. package/lib/functionParsing.d.ts +0 -13
  67. package/lib/functionParsing.d.ts.map +0 -1
  68. package/lib/functionParsing.js +0 -210
  69. package/lib/functionParsing.js.map +0 -1
  70. package/src/functionParsing.ts +0 -268
package/src/ses.ts ADDED
@@ -0,0 +1,73 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import type { CompartmentOptions, LockdownOptions } from "ses";
7
+
8
+ import type { SemanticAgentOptions } from "./api.js";
9
+ import { toErrorString } from "./utils.js";
10
+
11
+ const lockdownSymbol = Symbol.for("tree-agent.ses.locked");
12
+
13
+ /**
14
+ * Create an implementation of {@link SemanticAgentOptions.executeEdit} that uses the SES library to run the provided code in a secure environment.
15
+ * @param createCompartment - This function can be used to optionally configure the SES Compartment used to execute the code.
16
+ * The provided globals must be included in the compartment's globals and must not conflict with any additional globals passed in.
17
+ * @param lockdownOptions - Optional configuration passed to the SES `lockdown` function.
18
+ * @returns A function that can be used as the {@link SemanticAgentOptions.executeEdit | executeEdit} callback.
19
+ * @remarks This function will both import the SES library and call its `lockdown` function the first time it is called.
20
+ * Therefore, this function should be called only once, early in an application's lifetime.
21
+ * @alpha
22
+ */
23
+ export async function createSesEditEvaluator(options?: {
24
+ compartmentOptions?: CompartmentOptions;
25
+ lockdownOptions?: LockdownOptions;
26
+ }): Promise<SemanticAgentOptions["executeEdit"]> {
27
+ const optionsGlobals: Map<string, unknown> =
28
+ options?.compartmentOptions?.globals ?? new Map<string, unknown>();
29
+ if (optionsGlobals.has("context") === true) {
30
+ throw new Error(
31
+ "The 'context' global is reserved and cannot be overridden in the compartment options.",
32
+ );
33
+ }
34
+
35
+ // Importing 'ses' has side effects, so we do it lazily to avoid impacting environments that don't use this evaluator.
36
+ await import("ses");
37
+
38
+ if (!(lockdownSymbol in globalThis)) {
39
+ try {
40
+ lockdown(options?.lockdownOptions);
41
+ Object.defineProperty(globalThis, lockdownSymbol, {
42
+ value: true,
43
+ writable: false,
44
+ configurable: false,
45
+ enumerable: false,
46
+ });
47
+ } catch (error: unknown) {
48
+ if (toErrorString(error).includes("SES_ALREADY_LOCKED_DOWN")) {
49
+ Object.defineProperty(globalThis, lockdownSymbol, {
50
+ value: true,
51
+ writable: false,
52
+ configurable: false,
53
+ enumerable: false,
54
+ });
55
+ } else {
56
+ throw error;
57
+ }
58
+ }
59
+ }
60
+
61
+ return async (context: Record<string, unknown>, code: string) => {
62
+ const compartmentOptions = {
63
+ ...options?.compartmentOptions,
64
+ globals: {
65
+ ...Object.fromEntries(optionsGlobals),
66
+ context,
67
+ },
68
+ };
69
+
70
+ const compartment = new Compartment({ ...compartmentOptions, __options__: true });
71
+ await compartment.evaluate(code);
72
+ };
73
+ }
package/src/utils.ts CHANGED
@@ -619,3 +619,17 @@ export function findNamedSchemas(
619
619
  export function communize(str: string): string {
620
620
  return str.charAt(0).toLowerCase() + str.slice(1);
621
621
  }
622
+
623
+ /**
624
+ * Stringify an unknown error value
625
+ */
626
+ export function toErrorString(error: unknown): string {
627
+ if (error instanceof Error) {
628
+ return error.message;
629
+ }
630
+ try {
631
+ return JSON.stringify(error);
632
+ } catch {
633
+ return String(error);
634
+ }
635
+ }
@@ -1,13 +0,0 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
- /**
6
- * Finds the name of the first invocable function in the given code string.
7
- */
8
- export declare function findInvocableFunctionName(code: string): string | undefined;
9
- /**
10
- * Removes top-level export syntax so that the provided code can execute in a classic script context.
11
- */
12
- export declare function stripExportSyntax(code: string): string;
13
- //# sourceMappingURL=functionParsing.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"functionParsing.d.ts","sourceRoot":"","sources":["../src/functionParsing.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAsBH;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAc1E;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CA0DtD"}
@@ -1,215 +0,0 @@
1
- "use strict";
2
- /*!
3
- * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
- * Licensed under the MIT License.
5
- */
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.stripExportSyntax = exports.findInvocableFunctionName = void 0;
8
- /* eslint-disable @rushstack/no-new-null */
9
- const acorn_1 = require("acorn");
10
- /**
11
- * Finds the name of the first invocable function in the given code string.
12
- */
13
- function findInvocableFunctionName(code) {
14
- const program = parseProgram(code);
15
- if (program === undefined) {
16
- return undefined;
17
- }
18
- for (const node of program.body) {
19
- const name = getNameFromTopLevelNode(node);
20
- if (name !== undefined) {
21
- return name;
22
- }
23
- }
24
- return undefined;
25
- }
26
- exports.findInvocableFunctionName = findInvocableFunctionName;
27
- /**
28
- * Removes top-level export syntax so that the provided code can execute in a classic script context.
29
- */
30
- function stripExportSyntax(code) {
31
- const program = parseProgram(code);
32
- if (program === undefined) {
33
- return code;
34
- }
35
- const replacements = [];
36
- for (const node of program.body) {
37
- switch (node.type) {
38
- case "ExportNamedDeclaration": {
39
- if (node.declaration !== undefined && node.declaration !== null) {
40
- replacements.push({
41
- start: node.start,
42
- end: node.declaration.start,
43
- replacement: "",
44
- });
45
- }
46
- else {
47
- replacements.push({ start: node.start, end: node.end, replacement: "" });
48
- }
49
- break;
50
- }
51
- case "ExportDefaultDeclaration": {
52
- const { declaration, start, end } = node;
53
- if (declaration.type === "FunctionDeclaration" ||
54
- declaration.type === "FunctionExpression") {
55
- replacements.push({
56
- start,
57
- end: declaration.start,
58
- replacement: "",
59
- });
60
- }
61
- else {
62
- replacements.push({ start, end, replacement: "" });
63
- }
64
- break;
65
- }
66
- case "ExportAllDeclaration": {
67
- replacements.push({ start: node.start, end: node.end, replacement: "" });
68
- break;
69
- }
70
- // No default
71
- }
72
- }
73
- if (replacements.length === 0) {
74
- return code;
75
- }
76
- replacements.sort((a, b) => b.start - a.start);
77
- let sanitized = code;
78
- for (const { start, end, replacement } of replacements) {
79
- sanitized = `${sanitized.slice(0, start)}${replacement}${sanitized.slice(end)}`;
80
- }
81
- return sanitized;
82
- }
83
- exports.stripExportSyntax = stripExportSyntax;
84
- function parseProgram(code) {
85
- try {
86
- return (0, acorn_1.parse)(code, {
87
- ecmaVersion: "latest",
88
- sourceType: "module",
89
- });
90
- }
91
- catch {
92
- try {
93
- return (0, acorn_1.parse)(code, {
94
- ecmaVersion: "latest",
95
- sourceType: "script",
96
- allowReturnOutsideFunction: true,
97
- allowAwaitOutsideFunction: true,
98
- allowSuperOutsideMethod: true,
99
- });
100
- }
101
- catch {
102
- return undefined;
103
- }
104
- }
105
- }
106
- function getNameFromTopLevelNode(node) {
107
- switch (node.type) {
108
- case "FunctionDeclaration": {
109
- return getFunctionIdentifier(node);
110
- }
111
- case "VariableDeclaration": {
112
- return getNameFromVariableDeclaration(node);
113
- }
114
- case "ExpressionStatement": {
115
- return getNameFromExpression(node.expression);
116
- }
117
- case "ExportNamedDeclaration": {
118
- return getNameFromExportNamed(node);
119
- }
120
- case "ExportDefaultDeclaration": {
121
- return getNameFromExportDefault(node);
122
- }
123
- default: {
124
- return undefined;
125
- }
126
- }
127
- }
128
- function getNameFromVariableDeclaration(node) {
129
- for (const declarator of node.declarations) {
130
- const name = getIdentifierFromPattern(declarator.id);
131
- if (name === undefined) {
132
- continue;
133
- }
134
- if (isFunctionLikeExpression(declarator.init)) {
135
- return name;
136
- }
137
- }
138
- return undefined;
139
- }
140
- function getNameFromExpression(expression) {
141
- if (!isAssignmentExpression(expression)) {
142
- return undefined;
143
- }
144
- if (expression.operator !== "=") {
145
- return undefined;
146
- }
147
- const target = getIdentifierFromPattern(expression.left);
148
- if (target === undefined) {
149
- return undefined;
150
- }
151
- return isFunctionLikeExpression(expression.right) ? target : undefined;
152
- }
153
- function getNameFromExportNamed(node) {
154
- const declaration = node.declaration;
155
- if (declaration !== undefined && declaration !== null) {
156
- if (declaration.type === "FunctionDeclaration") {
157
- const name = getFunctionIdentifier(declaration);
158
- if (name !== undefined) {
159
- return name;
160
- }
161
- }
162
- else if (declaration.type === "VariableDeclaration") {
163
- const name = getNameFromVariableDeclaration(declaration);
164
- if (name !== undefined) {
165
- return name;
166
- }
167
- }
168
- }
169
- if (node.source === undefined || node.source === null) {
170
- for (const specifier of node.specifiers) {
171
- const localName = getIdentifierName(specifier.local);
172
- if (localName !== undefined) {
173
- return localName;
174
- }
175
- }
176
- }
177
- return undefined;
178
- }
179
- function getNameFromExportDefault(node) {
180
- const declaration = node.declaration;
181
- if (declaration.type === "Identifier") {
182
- return declaration.name;
183
- }
184
- if (declaration.type === "FunctionDeclaration" ||
185
- declaration.type === "FunctionExpression") {
186
- return getFunctionIdentifier(declaration);
187
- }
188
- return undefined;
189
- }
190
- function isAssignmentExpression(expression) {
191
- return expression?.type === "AssignmentExpression";
192
- }
193
- function getIdentifierFromPattern(pattern) {
194
- if (pattern.type === "Identifier") {
195
- return pattern.name;
196
- }
197
- return undefined;
198
- }
199
- function isFunctionLikeExpression(expression) {
200
- return (expression?.type === "FunctionExpression" || expression?.type === "ArrowFunctionExpression");
201
- }
202
- function getFunctionIdentifier(fn) {
203
- const id = fn.id;
204
- if (id === undefined || id === null) {
205
- return undefined;
206
- }
207
- return id.name;
208
- }
209
- function getIdentifierName(node) {
210
- if (node.type === "Identifier") {
211
- return node.name;
212
- }
213
- return undefined;
214
- }
215
- //# sourceMappingURL=functionParsing.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"functionParsing.js","sourceRoot":"","sources":["../src/functionParsing.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,2CAA2C;AAE3C,iCAA8B;AAkB9B;;GAEG;AACH,SAAgB,yBAAyB,CAAC,IAAY;IACrD,MAAM,OAAO,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IACnC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,KAAK,MAAM,IAAI,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;QACjC,MAAM,IAAI,GAAG,uBAAuB,CAAC,IAAI,CAAC,CAAC;QAC3C,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;IAED,OAAO,SAAS,CAAC;AAClB,CAAC;AAdD,8DAcC;AAED;;GAEG;AACH,SAAgB,iBAAiB,CAAC,IAAY;IAC7C,MAAM,OAAO,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IACnC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO,IAAI,CAAC;IACb,CAAC;IAED,MAAM,YAAY,GAA0D,EAAE,CAAC;IAC/E,KAAK,MAAM,IAAI,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;QACjC,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;YACnB,KAAK,wBAAwB,CAAC,CAAC,CAAC;gBAC/B,IAAI,IAAI,CAAC,WAAW,KAAK,SAAS,IAAI,IAAI,CAAC,WAAW,KAAK,IAAI,EAAE,CAA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S,sBAAsB,CAC9B,UAA4C;IAE5C,OAAO,UAAU,EAAE,IAAI,KAAK,sBAAsB,CAAC;AACpD,CAAC;AAED,SAAS,wBAAwB,CAAC,OAAgB;IACjD,IAAI,OAAO,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QACnC,OAAO,OAAO,CAAC,IAAI,CAAC;IACrB,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC;AAED,SAAS,wBAAwB,CAChC,UAAyC;IAEzC,OAAO,CACN,UAAU,EAAE,IAAI,KAAK,oBAAoB,IAAI,UAAU,EAAE,IAAI,KAAK,yBAAyB,CAC3F,CAAC;AACH,CAAC;AAED,SAAS,qBAAqB,CAC7B,EAAyE;IAEzE,MAAM,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC;IACjB,IAAI,EAAE,KAAK,SAAS,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC;QACrC,OAAO,SAAS,CAAC;IAClB,CAAC;IACD,OAAO,EAAE,CAAC,IAAI,CAAC;AAChB,CAAC;AAED,SAAS,iBAAiB,CAAC,IAA0B;IACpD,IAAI,IAAI,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QAChC,OAAO,IAAI,CAAC,IAAI,CAAC;IAClB,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable @rushstack/no-new-null */\n\nimport { parse } from \"acorn\";\nimport type {\n\tProgram,\n\tStatement,\n\tModuleDeclaration,\n\tVariableDeclaration,\n\tExpression,\n\tAssignmentExpression,\n\tPattern,\n\tIdentifier,\n\tLiteral,\n\tExportNamedDeclaration,\n\tExportDefaultDeclaration,\n\tFunctionDeclaration,\n\tFunctionExpression,\n\tArrowFunctionExpression,\n} from \"acorn\";\n\n/**\n * Finds the name of the first invocable function in the given code string.\n */\nexport function findInvocableFunctionName(code: string): string | undefined {\n\tconst program = parseProgram(code);\n\tif (program === undefined) {\n\t\treturn undefined;\n\t}\n\n\tfor (const node of program.body) {\n\t\tconst name = getNameFromTopLevelNode(node);\n\t\tif (name !== undefined) {\n\t\t\treturn name;\n\t\t}\n\t}\n\n\treturn undefined;\n}\n\n/**\n * Removes top-level export syntax so that the provided code can execute in a classic script context.\n */\nexport function stripExportSyntax(code: string): string {\n\tconst program = parseProgram(code);\n\tif (program === undefined) {\n\t\treturn code;\n\t}\n\n\tconst replacements: { start: number; end: number; replacement: string }[] = [];\n\tfor (const node of program.body) {\n\t\tswitch (node.type) {\n\t\t\tcase \"ExportNamedDeclaration\": {\n\t\t\t\tif (node.declaration !== undefined && node.declaration !== null) {\n\t\t\t\t\treplacements.push({\n\t\t\t\t\t\tstart: node.start,\n\t\t\t\t\t\tend: node.declaration.start,\n\t\t\t\t\t\treplacement: \"\",\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\treplacements.push({ start: node.start, end: node.end, replacement: \"\" });\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"ExportDefaultDeclaration\": {\n\t\t\t\tconst { declaration, start, end } = node;\n\t\t\t\tif (\n\t\t\t\t\tdeclaration.type === \"FunctionDeclaration\" ||\n\t\t\t\t\tdeclaration.type === \"FunctionExpression\"\n\t\t\t\t) {\n\t\t\t\t\treplacements.push({\n\t\t\t\t\t\tstart,\n\t\t\t\t\t\tend: declaration.start,\n\t\t\t\t\t\treplacement: \"\",\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\treplacements.push({ start, end, replacement: \"\" });\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"ExportAllDeclaration\": {\n\t\t\t\treplacements.push({ start: node.start, end: node.end, replacement: \"\" });\n\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t// No default\n\t\t}\n\t}\n\n\tif (replacements.length === 0) {\n\t\treturn code;\n\t}\n\n\treplacements.sort((a, b) => b.start - a.start);\n\tlet sanitized = code;\n\tfor (const { start, end, replacement } of replacements) {\n\t\tsanitized = `${sanitized.slice(0, start)}${replacement}${sanitized.slice(end)}`;\n\t}\n\treturn sanitized;\n}\n\ntype TopLevelNode = Statement | ModuleDeclaration;\n\nfunction parseProgram(code: string): Program | undefined {\n\ttry {\n\t\treturn parse(code, {\n\t\t\tecmaVersion: \"latest\",\n\t\t\tsourceType: \"module\",\n\t\t});\n\t} catch {\n\t\ttry {\n\t\t\treturn parse(code, {\n\t\t\t\tecmaVersion: \"latest\",\n\t\t\t\tsourceType: \"script\",\n\t\t\t\tallowReturnOutsideFunction: true,\n\t\t\t\tallowAwaitOutsideFunction: true,\n\t\t\t\tallowSuperOutsideMethod: true,\n\t\t\t});\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t}\n}\n\nfunction getNameFromTopLevelNode(node: TopLevelNode): string | undefined {\n\tswitch (node.type) {\n\t\tcase \"FunctionDeclaration\": {\n\t\t\treturn getFunctionIdentifier(node);\n\t\t}\n\t\tcase \"VariableDeclaration\": {\n\t\t\treturn getNameFromVariableDeclaration(node);\n\t\t}\n\t\tcase \"ExpressionStatement\": {\n\t\t\treturn getNameFromExpression(node.expression);\n\t\t}\n\t\tcase \"ExportNamedDeclaration\": {\n\t\t\treturn getNameFromExportNamed(node);\n\t\t}\n\t\tcase \"ExportDefaultDeclaration\": {\n\t\t\treturn getNameFromExportDefault(node);\n\t\t}\n\t\tdefault: {\n\t\t\treturn undefined;\n\t\t}\n\t}\n}\n\nfunction getNameFromVariableDeclaration(node: VariableDeclaration): string | undefined {\n\tfor (const declarator of node.declarations) {\n\t\tconst name = getIdentifierFromPattern(declarator.id);\n\t\tif (name === undefined) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (isFunctionLikeExpression(declarator.init)) {\n\t\t\treturn name;\n\t\t}\n\t}\n\n\treturn undefined;\n}\n\nfunction getNameFromExpression(\n\texpression: Expression | Literal | undefined,\n): string | undefined {\n\tif (!isAssignmentExpression(expression)) {\n\t\treturn undefined;\n\t}\n\n\tif (expression.operator !== \"=\") {\n\t\treturn undefined;\n\t}\n\n\tconst target = getIdentifierFromPattern(expression.left);\n\tif (target === undefined) {\n\t\treturn undefined;\n\t}\n\n\treturn isFunctionLikeExpression(expression.right) ? target : undefined;\n}\n\nfunction getNameFromExportNamed(node: ExportNamedDeclaration): string | undefined {\n\tconst declaration = node.declaration;\n\tif (declaration !== undefined && declaration !== null) {\n\t\tif (declaration.type === \"FunctionDeclaration\") {\n\t\t\tconst name = getFunctionIdentifier(declaration);\n\t\t\tif (name !== undefined) {\n\t\t\t\treturn name;\n\t\t\t}\n\t\t} else if (declaration.type === \"VariableDeclaration\") {\n\t\t\tconst name = getNameFromVariableDeclaration(declaration);\n\t\t\tif (name !== undefined) {\n\t\t\t\treturn name;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (node.source === undefined || node.source === null) {\n\t\tfor (const specifier of node.specifiers) {\n\t\t\tconst localName = getIdentifierName(specifier.local);\n\t\t\tif (localName !== undefined) {\n\t\t\t\treturn localName;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn undefined;\n}\n\nfunction getNameFromExportDefault(node: ExportDefaultDeclaration): string | undefined {\n\tconst declaration = node.declaration;\n\tif (declaration.type === \"Identifier\") {\n\t\treturn declaration.name;\n\t}\n\n\tif (\n\t\tdeclaration.type === \"FunctionDeclaration\" ||\n\t\tdeclaration.type === \"FunctionExpression\"\n\t) {\n\t\treturn getFunctionIdentifier(declaration);\n\t}\n\n\treturn undefined;\n}\n\nfunction isAssignmentExpression(\n\texpression: Expression | Literal | undefined,\n): expression is AssignmentExpression {\n\treturn expression?.type === \"AssignmentExpression\";\n}\n\nfunction getIdentifierFromPattern(pattern: Pattern): string | undefined {\n\tif (pattern.type === \"Identifier\") {\n\t\treturn pattern.name;\n\t}\n\treturn undefined;\n}\n\nfunction isFunctionLikeExpression(\n\texpression: Expression | null | undefined,\n): expression is FunctionExpression | ArrowFunctionExpression {\n\treturn (\n\t\texpression?.type === \"FunctionExpression\" || expression?.type === \"ArrowFunctionExpression\"\n\t);\n}\n\nfunction getFunctionIdentifier(\n\tfn: FunctionDeclaration | FunctionExpression | { id?: Identifier | null },\n): string | undefined {\n\tconst id = fn.id;\n\tif (id === undefined || id === null) {\n\t\treturn undefined;\n\t}\n\treturn id.name;\n}\n\nfunction getIdentifierName(node: Identifier | Literal): string | undefined {\n\tif (node.type === \"Identifier\") {\n\t\treturn node.name;\n\t}\n\treturn undefined;\n}\n"]}
@@ -1,13 +0,0 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
- /**
6
- * Finds the name of the first invocable function in the given code string.
7
- */
8
- export declare function findInvocableFunctionName(code: string): string | undefined;
9
- /**
10
- * Removes top-level export syntax so that the provided code can execute in a classic script context.
11
- */
12
- export declare function stripExportSyntax(code: string): string;
13
- //# sourceMappingURL=functionParsing.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"functionParsing.d.ts","sourceRoot":"","sources":["../src/functionParsing.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAsBH;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAc1E;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CA0DtD"}
@@ -1,210 +0,0 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
- * Licensed under the MIT License.
4
- */
5
- /* eslint-disable @rushstack/no-new-null */
6
- import { parse } from "acorn";
7
- /**
8
- * Finds the name of the first invocable function in the given code string.
9
- */
10
- export function findInvocableFunctionName(code) {
11
- const program = parseProgram(code);
12
- if (program === undefined) {
13
- return undefined;
14
- }
15
- for (const node of program.body) {
16
- const name = getNameFromTopLevelNode(node);
17
- if (name !== undefined) {
18
- return name;
19
- }
20
- }
21
- return undefined;
22
- }
23
- /**
24
- * Removes top-level export syntax so that the provided code can execute in a classic script context.
25
- */
26
- export function stripExportSyntax(code) {
27
- const program = parseProgram(code);
28
- if (program === undefined) {
29
- return code;
30
- }
31
- const replacements = [];
32
- for (const node of program.body) {
33
- switch (node.type) {
34
- case "ExportNamedDeclaration": {
35
- if (node.declaration !== undefined && node.declaration !== null) {
36
- replacements.push({
37
- start: node.start,
38
- end: node.declaration.start,
39
- replacement: "",
40
- });
41
- }
42
- else {
43
- replacements.push({ start: node.start, end: node.end, replacement: "" });
44
- }
45
- break;
46
- }
47
- case "ExportDefaultDeclaration": {
48
- const { declaration, start, end } = node;
49
- if (declaration.type === "FunctionDeclaration" ||
50
- declaration.type === "FunctionExpression") {
51
- replacements.push({
52
- start,
53
- end: declaration.start,
54
- replacement: "",
55
- });
56
- }
57
- else {
58
- replacements.push({ start, end, replacement: "" });
59
- }
60
- break;
61
- }
62
- case "ExportAllDeclaration": {
63
- replacements.push({ start: node.start, end: node.end, replacement: "" });
64
- break;
65
- }
66
- // No default
67
- }
68
- }
69
- if (replacements.length === 0) {
70
- return code;
71
- }
72
- replacements.sort((a, b) => b.start - a.start);
73
- let sanitized = code;
74
- for (const { start, end, replacement } of replacements) {
75
- sanitized = `${sanitized.slice(0, start)}${replacement}${sanitized.slice(end)}`;
76
- }
77
- return sanitized;
78
- }
79
- function parseProgram(code) {
80
- try {
81
- return parse(code, {
82
- ecmaVersion: "latest",
83
- sourceType: "module",
84
- });
85
- }
86
- catch {
87
- try {
88
- return parse(code, {
89
- ecmaVersion: "latest",
90
- sourceType: "script",
91
- allowReturnOutsideFunction: true,
92
- allowAwaitOutsideFunction: true,
93
- allowSuperOutsideMethod: true,
94
- });
95
- }
96
- catch {
97
- return undefined;
98
- }
99
- }
100
- }
101
- function getNameFromTopLevelNode(node) {
102
- switch (node.type) {
103
- case "FunctionDeclaration": {
104
- return getFunctionIdentifier(node);
105
- }
106
- case "VariableDeclaration": {
107
- return getNameFromVariableDeclaration(node);
108
- }
109
- case "ExpressionStatement": {
110
- return getNameFromExpression(node.expression);
111
- }
112
- case "ExportNamedDeclaration": {
113
- return getNameFromExportNamed(node);
114
- }
115
- case "ExportDefaultDeclaration": {
116
- return getNameFromExportDefault(node);
117
- }
118
- default: {
119
- return undefined;
120
- }
121
- }
122
- }
123
- function getNameFromVariableDeclaration(node) {
124
- for (const declarator of node.declarations) {
125
- const name = getIdentifierFromPattern(declarator.id);
126
- if (name === undefined) {
127
- continue;
128
- }
129
- if (isFunctionLikeExpression(declarator.init)) {
130
- return name;
131
- }
132
- }
133
- return undefined;
134
- }
135
- function getNameFromExpression(expression) {
136
- if (!isAssignmentExpression(expression)) {
137
- return undefined;
138
- }
139
- if (expression.operator !== "=") {
140
- return undefined;
141
- }
142
- const target = getIdentifierFromPattern(expression.left);
143
- if (target === undefined) {
144
- return undefined;
145
- }
146
- return isFunctionLikeExpression(expression.right) ? target : undefined;
147
- }
148
- function getNameFromExportNamed(node) {
149
- const declaration = node.declaration;
150
- if (declaration !== undefined && declaration !== null) {
151
- if (declaration.type === "FunctionDeclaration") {
152
- const name = getFunctionIdentifier(declaration);
153
- if (name !== undefined) {
154
- return name;
155
- }
156
- }
157
- else if (declaration.type === "VariableDeclaration") {
158
- const name = getNameFromVariableDeclaration(declaration);
159
- if (name !== undefined) {
160
- return name;
161
- }
162
- }
163
- }
164
- if (node.source === undefined || node.source === null) {
165
- for (const specifier of node.specifiers) {
166
- const localName = getIdentifierName(specifier.local);
167
- if (localName !== undefined) {
168
- return localName;
169
- }
170
- }
171
- }
172
- return undefined;
173
- }
174
- function getNameFromExportDefault(node) {
175
- const declaration = node.declaration;
176
- if (declaration.type === "Identifier") {
177
- return declaration.name;
178
- }
179
- if (declaration.type === "FunctionDeclaration" ||
180
- declaration.type === "FunctionExpression") {
181
- return getFunctionIdentifier(declaration);
182
- }
183
- return undefined;
184
- }
185
- function isAssignmentExpression(expression) {
186
- return expression?.type === "AssignmentExpression";
187
- }
188
- function getIdentifierFromPattern(pattern) {
189
- if (pattern.type === "Identifier") {
190
- return pattern.name;
191
- }
192
- return undefined;
193
- }
194
- function isFunctionLikeExpression(expression) {
195
- return (expression?.type === "FunctionExpression" || expression?.type === "ArrowFunctionExpression");
196
- }
197
- function getFunctionIdentifier(fn) {
198
- const id = fn.id;
199
- if (id === undefined || id === null) {
200
- return undefined;
201
- }
202
- return id.name;
203
- }
204
- function getIdentifierName(node) {
205
- if (node.type === "Identifier") {
206
- return node.name;
207
- }
208
- return undefined;
209
- }
210
- //# sourceMappingURL=functionParsing.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"functionParsing.js","sourceRoot":"","sources":["../src/functionParsing.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,2CAA2C;AAE3C,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAkB9B;;GAEG;AACH,MAAM,UAAU,yBAAyB,CAAC,IAAY;IACrD,MAAM,OAAO,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IACnC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,KAAK,MAAM,IAAI,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;QACjC,MAAM,IAAI,GAAG,uBAAuB,CAAC,IAAI,CAAC,CAAC;QAC3C,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;IAED,OAAO,SAAS,CAAC;AAClB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC7C,MAAM,OAAO,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IACnC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO,IAAI,CAAC;IACb,CAAC;IAED,MAAM,YAAY,GAA0D,EAAE,CAAC;IAC/E,KAAK,MAAM,IAAI,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;QACjC,QAAQ,IAAI,CAAC,IAAI,EAAE,CAA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AC;QACF,OAAO,qBAAqB,CAAC,WAAW,CAAC,CAAC;IAC3C,CAAC;IAED,OAAO,SAAS,CAAC;AAClB,CAAC;AAED,SAAS,sBAAsB,CAC9B,UAA4C;IAE5C,OAAO,UAAU,EAAE,IAAI,KAAK,sBAAsB,CAAC;AACpD,CAAC;AAED,SAAS,wBAAwB,CAAC,OAAgB;IACjD,IAAI,OAAO,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QACnC,OAAO,OAAO,CAAC,IAAI,CAAC;IACrB,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC;AAED,SAAS,wBAAwB,CAChC,UAAyC;IAEzC,OAAO,CACN,UAAU,EAAE,IAAI,KAAK,oBAAoB,IAAI,UAAU,EAAE,IAAI,KAAK,yBAAyB,CAC3F,CAAC;AACH,CAAC;AAED,SAAS,qBAAqB,CAC7B,EAAyE;IAEzE,MAAM,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC;IACjB,IAAI,EAAE,KAAK,SAAS,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC;QACrC,OAAO,SAAS,CAAC;IAClB,CAAC;IACD,OAAO,EAAE,CAAC,IAAI,CAAC;AAChB,CAAC;AAED,SAAS,iBAAiB,CAAC,IAA0B;IACpD,IAAI,IAAI,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QAChC,OAAO,IAAI,CAAC,IAAI,CAAC;IAClB,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable @rushstack/no-new-null */\n\nimport { parse } from \"acorn\";\nimport type {\n\tProgram,\n\tStatement,\n\tModuleDeclaration,\n\tVariableDeclaration,\n\tExpression,\n\tAssignmentExpression,\n\tPattern,\n\tIdentifier,\n\tLiteral,\n\tExportNamedDeclaration,\n\tExportDefaultDeclaration,\n\tFunctionDeclaration,\n\tFunctionExpression,\n\tArrowFunctionExpression,\n} from \"acorn\";\n\n/**\n * Finds the name of the first invocable function in the given code string.\n */\nexport function findInvocableFunctionName(code: string): string | undefined {\n\tconst program = parseProgram(code);\n\tif (program === undefined) {\n\t\treturn undefined;\n\t}\n\n\tfor (const node of program.body) {\n\t\tconst name = getNameFromTopLevelNode(node);\n\t\tif (name !== undefined) {\n\t\t\treturn name;\n\t\t}\n\t}\n\n\treturn undefined;\n}\n\n/**\n * Removes top-level export syntax so that the provided code can execute in a classic script context.\n */\nexport function stripExportSyntax(code: string): string {\n\tconst program = parseProgram(code);\n\tif (program === undefined) {\n\t\treturn code;\n\t}\n\n\tconst replacements: { start: number; end: number; replacement: string }[] = [];\n\tfor (const node of program.body) {\n\t\tswitch (node.type) {\n\t\t\tcase \"ExportNamedDeclaration\": {\n\t\t\t\tif (node.declaration !== undefined && node.declaration !== null) {\n\t\t\t\t\treplacements.push({\n\t\t\t\t\t\tstart: node.start,\n\t\t\t\t\t\tend: node.declaration.start,\n\t\t\t\t\t\treplacement: \"\",\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\treplacements.push({ start: node.start, end: node.end, replacement: \"\" });\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"ExportDefaultDeclaration\": {\n\t\t\t\tconst { declaration, start, end } = node;\n\t\t\t\tif (\n\t\t\t\t\tdeclaration.type === \"FunctionDeclaration\" ||\n\t\t\t\t\tdeclaration.type === \"FunctionExpression\"\n\t\t\t\t) {\n\t\t\t\t\treplacements.push({\n\t\t\t\t\t\tstart,\n\t\t\t\t\t\tend: declaration.start,\n\t\t\t\t\t\treplacement: \"\",\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\treplacements.push({ start, end, replacement: \"\" });\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"ExportAllDeclaration\": {\n\t\t\t\treplacements.push({ start: node.start, end: node.end, replacement: \"\" });\n\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t// No default\n\t\t}\n\t}\n\n\tif (replacements.length === 0) {\n\t\treturn code;\n\t}\n\n\treplacements.sort((a, b) => b.start - a.start);\n\tlet sanitized = code;\n\tfor (const { start, end, replacement } of replacements) {\n\t\tsanitized = `${sanitized.slice(0, start)}${replacement}${sanitized.slice(end)}`;\n\t}\n\treturn sanitized;\n}\n\ntype TopLevelNode = Statement | ModuleDeclaration;\n\nfunction parseProgram(code: string): Program | undefined {\n\ttry {\n\t\treturn parse(code, {\n\t\t\tecmaVersion: \"latest\",\n\t\t\tsourceType: \"module\",\n\t\t});\n\t} catch {\n\t\ttry {\n\t\t\treturn parse(code, {\n\t\t\t\tecmaVersion: \"latest\",\n\t\t\t\tsourceType: \"script\",\n\t\t\t\tallowReturnOutsideFunction: true,\n\t\t\t\tallowAwaitOutsideFunction: true,\n\t\t\t\tallowSuperOutsideMethod: true,\n\t\t\t});\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t}\n}\n\nfunction getNameFromTopLevelNode(node: TopLevelNode): string | undefined {\n\tswitch (node.type) {\n\t\tcase \"FunctionDeclaration\": {\n\t\t\treturn getFunctionIdentifier(node);\n\t\t}\n\t\tcase \"VariableDeclaration\": {\n\t\t\treturn getNameFromVariableDeclaration(node);\n\t\t}\n\t\tcase \"ExpressionStatement\": {\n\t\t\treturn getNameFromExpression(node.expression);\n\t\t}\n\t\tcase \"ExportNamedDeclaration\": {\n\t\t\treturn getNameFromExportNamed(node);\n\t\t}\n\t\tcase \"ExportDefaultDeclaration\": {\n\t\t\treturn getNameFromExportDefault(node);\n\t\t}\n\t\tdefault: {\n\t\t\treturn undefined;\n\t\t}\n\t}\n}\n\nfunction getNameFromVariableDeclaration(node: VariableDeclaration): string | undefined {\n\tfor (const declarator of node.declarations) {\n\t\tconst name = getIdentifierFromPattern(declarator.id);\n\t\tif (name === undefined) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (isFunctionLikeExpression(declarator.init)) {\n\t\t\treturn name;\n\t\t}\n\t}\n\n\treturn undefined;\n}\n\nfunction getNameFromExpression(\n\texpression: Expression | Literal | undefined,\n): string | undefined {\n\tif (!isAssignmentExpression(expression)) {\n\t\treturn undefined;\n\t}\n\n\tif (expression.operator !== \"=\") {\n\t\treturn undefined;\n\t}\n\n\tconst target = getIdentifierFromPattern(expression.left);\n\tif (target === undefined) {\n\t\treturn undefined;\n\t}\n\n\treturn isFunctionLikeExpression(expression.right) ? target : undefined;\n}\n\nfunction getNameFromExportNamed(node: ExportNamedDeclaration): string | undefined {\n\tconst declaration = node.declaration;\n\tif (declaration !== undefined && declaration !== null) {\n\t\tif (declaration.type === \"FunctionDeclaration\") {\n\t\t\tconst name = getFunctionIdentifier(declaration);\n\t\t\tif (name !== undefined) {\n\t\t\t\treturn name;\n\t\t\t}\n\t\t} else if (declaration.type === \"VariableDeclaration\") {\n\t\t\tconst name = getNameFromVariableDeclaration(declaration);\n\t\t\tif (name !== undefined) {\n\t\t\t\treturn name;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (node.source === undefined || node.source === null) {\n\t\tfor (const specifier of node.specifiers) {\n\t\t\tconst localName = getIdentifierName(specifier.local);\n\t\t\tif (localName !== undefined) {\n\t\t\t\treturn localName;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn undefined;\n}\n\nfunction getNameFromExportDefault(node: ExportDefaultDeclaration): string | undefined {\n\tconst declaration = node.declaration;\n\tif (declaration.type === \"Identifier\") {\n\t\treturn declaration.name;\n\t}\n\n\tif (\n\t\tdeclaration.type === \"FunctionDeclaration\" ||\n\t\tdeclaration.type === \"FunctionExpression\"\n\t) {\n\t\treturn getFunctionIdentifier(declaration);\n\t}\n\n\treturn undefined;\n}\n\nfunction isAssignmentExpression(\n\texpression: Expression | Literal | undefined,\n): expression is AssignmentExpression {\n\treturn expression?.type === \"AssignmentExpression\";\n}\n\nfunction getIdentifierFromPattern(pattern: Pattern): string | undefined {\n\tif (pattern.type === \"Identifier\") {\n\t\treturn pattern.name;\n\t}\n\treturn undefined;\n}\n\nfunction isFunctionLikeExpression(\n\texpression: Expression | null | undefined,\n): expression is FunctionExpression | ArrowFunctionExpression {\n\treturn (\n\t\texpression?.type === \"FunctionExpression\" || expression?.type === \"ArrowFunctionExpression\"\n\t);\n}\n\nfunction getFunctionIdentifier(\n\tfn: FunctionDeclaration | FunctionExpression | { id?: Identifier | null },\n): string | undefined {\n\tconst id = fn.id;\n\tif (id === undefined || id === null) {\n\t\treturn undefined;\n\t}\n\treturn id.name;\n}\n\nfunction getIdentifierName(node: Identifier | Literal): string | undefined {\n\tif (node.type === \"Identifier\") {\n\t\treturn node.name;\n\t}\n\treturn undefined;\n}\n"]}