@fluidframework/tree-agent 2.63.0 → 2.70.0-360753
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/api-report/tree-agent.alpha.api.md +20 -4
- package/dist/agent.d.ts +30 -1
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +56 -37
- package/dist/agent.js.map +1 -1
- package/dist/alpha.d.ts +5 -0
- package/dist/api.d.ts +35 -22
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/prompt.d.ts.map +1 -1
- package/dist/prompt.js +38 -3
- package/dist/prompt.js.map +1 -1
- package/lib/agent.d.ts +30 -1
- package/lib/agent.d.ts.map +1 -1
- package/lib/agent.js +53 -36
- package/lib/agent.js.map +1 -1
- package/lib/alpha.d.ts +5 -0
- package/lib/api.d.ts +35 -22
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/prompt.d.ts.map +1 -1
- package/lib/prompt.js +38 -3
- package/lib/prompt.js.map +1 -1
- package/package.json +10 -12
- package/src/agent.ts +92 -45
- package/src/api.ts +44 -29
- package/src/index.ts +8 -1
- package/src/prompt.ts +45 -3
package/src/api.ts
CHANGED
|
@@ -4,9 +4,13 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import type { ImplicitFieldSchema, TreeNode } from "@fluidframework/tree";
|
|
7
|
-
//
|
|
7
|
+
// These are used for doc links
|
|
8
8
|
import type { FactoryContentObject, ReadableField } from "@fluidframework/tree/alpha";
|
|
9
9
|
|
|
10
|
+
// This is used for doc links
|
|
11
|
+
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
12
|
+
import type { bindEditor, defaultEditor } from "./agent.js";
|
|
13
|
+
|
|
10
14
|
/**
|
|
11
15
|
* Logger interface for logging events from a {@link SharedTreeSemanticAgent}.
|
|
12
16
|
* @alpha
|
|
@@ -18,6 +22,26 @@ export interface Logger {
|
|
|
18
22
|
log(message: string): void;
|
|
19
23
|
}
|
|
20
24
|
|
|
25
|
+
/**
|
|
26
|
+
* A synchronous function that executes a string of JavaScript code to perform an edit within a {@link SharedTreeSemanticAgent}.
|
|
27
|
+
* @param context - An object that must be provided to the generated code as a variable named "context" in its top-level scope.
|
|
28
|
+
* @param code - The JavaScript code that should be executed.
|
|
29
|
+
* @remarks To simulate the execution of an editor outside of an {@link SharedTreeSemanticAgent | agent}, you can use {@link bindEditor | bindEditor} to bind an editor to a specific subtree.
|
|
30
|
+
* @alpha
|
|
31
|
+
*/
|
|
32
|
+
export type SynchronousEditor = (context: Record<string, unknown>, code: string) => void;
|
|
33
|
+
/**
|
|
34
|
+
* An asynchronous function that executes a string of JavaScript code to perform an edit within a {@link SharedTreeSemanticAgent}.
|
|
35
|
+
* @param context - An object that must be provided to the generated code as a variable named "context" in its top-level scope.
|
|
36
|
+
* @param code - The JavaScript code that should be executed.
|
|
37
|
+
* @remarks To simulate the execution of an editor outside of an {@link SharedTreeSemanticAgent | agent}, you can use {@link bindEditor | bindEditor} to bind an editor to a specific subtree.
|
|
38
|
+
* @alpha
|
|
39
|
+
*/
|
|
40
|
+
export type AsynchronousEditor = (
|
|
41
|
+
context: Record<string, unknown>,
|
|
42
|
+
code: string,
|
|
43
|
+
) => Promise<void>;
|
|
44
|
+
|
|
21
45
|
/**
|
|
22
46
|
* Options used to parameterize the creation of a {@link SharedTreeSemanticAgent}.
|
|
23
47
|
* @alpha
|
|
@@ -28,22 +52,14 @@ export interface SemanticAgentOptions {
|
|
|
28
52
|
*/
|
|
29
53
|
domainHints?: string;
|
|
30
54
|
/**
|
|
31
|
-
*
|
|
32
|
-
* @remarks
|
|
33
|
-
* @
|
|
34
|
-
* @throws If the code is invalid, this function should throw an error with a human-readable message describing why it is invalid.
|
|
35
|
-
*/
|
|
36
|
-
validateEdit?: (code: string) => void | Promise<void>;
|
|
37
|
-
/**
|
|
38
|
-
* Evaluates/runs any generated JavaScript created by the {@link SharedTreeChatModel.editToolName | model's editing tool}.
|
|
39
|
-
* @remarks This happens only after the code has been successfully validated by the optional {@link SemanticAgentOptions.validateEdit | validateEdit} function.
|
|
40
|
-
* @param context - An object that must be provided to the generated code as a variable named "context" in its top-level scope.
|
|
41
|
-
* @param code - The generated JavaScript code as a string.
|
|
42
|
-
* @throws If an error is thrown while executing the code, it will be caught and the message will be forwarded to the model for debugging.
|
|
43
|
-
* @remarks If this function is not provided, the generated code will be executed using a simple `eval` call, which may not provide sufficient security guarantees for some environments.
|
|
55
|
+
* Executes any generated JavaScript created by the {@link SharedTreeChatModel.editToolName | model's editing tool}.
|
|
56
|
+
* @remarks If an error is thrown while executing the code, it will be caught and the message will be forwarded to the {@link SharedTreeChatModel | model} for debugging.
|
|
57
|
+
* @remarks If this function is not provided, the generated code will be executed using a {@link defaultEditor | simple default} which may not provide sufficient security guarantees for some environments.
|
|
44
58
|
* Use a library such as SES to provide a more secure implementation - see `@fluidframework/tree-agent-ses` for a drop-in implementation.
|
|
59
|
+
*
|
|
60
|
+
* To simulate the execution of an editor outside of an {@link SharedTreeSemanticAgent | agent}, you can use {@link bindEditor | bindEditor} to bind an editor to a specific subtree.
|
|
45
61
|
*/
|
|
46
|
-
|
|
62
|
+
editor?: SynchronousEditor | AsynchronousEditor;
|
|
47
63
|
/**
|
|
48
64
|
* The maximum number of sequential edits the LLM can make before we assume it's stuck in a loop.
|
|
49
65
|
*/
|
|
@@ -56,23 +72,19 @@ export interface SemanticAgentOptions {
|
|
|
56
72
|
|
|
57
73
|
/**
|
|
58
74
|
* A result from an edit attempt via the {@link SharedTreeChatQuery.edit} function.
|
|
59
|
-
* @remarks
|
|
60
|
-
* - `success`: The edit was successfully applied.
|
|
61
|
-
* - `disabledError`: The model is not allowed to edit the tree (i.e. {@link SharedTreeChatModel.editToolName} was not provided).
|
|
62
|
-
* - `validationError`: The provided JavaScript did not pass the optional {@link SemanticAgentOptions.validateEdit} function.
|
|
63
|
-
* - `executionError`: An error was thrown while parsing or executing the provided JavaScript.
|
|
64
|
-
* - `tooManyEditsError`: The {@link SharedTreeChatQuery.edit} function has been called more than the number of times specified by {@link SemanticAgentOptions.maximumSequentialEdits} for the same message.
|
|
65
|
-
* - `expiredError`: The {@link SharedTreeChatQuery.edit} function was called after the issuing query has already completed.
|
|
66
75
|
* @alpha
|
|
67
76
|
*/
|
|
68
77
|
export interface EditResult {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
78
|
+
/**
|
|
79
|
+
* The type of the edit result.
|
|
80
|
+
* @remarks
|
|
81
|
+
* - `success`: The edit was successfully applied.
|
|
82
|
+
* - `disabledError`: The model is not allowed to edit the tree (i.e. {@link SharedTreeChatModel.editToolName} was not provided).
|
|
83
|
+
* - `editingError`: An error was thrown while parsing or executing the provided JavaScript.
|
|
84
|
+
* - `tooManyEditsError`: The {@link SharedTreeChatQuery.edit} function has been called more than the number of times specified by {@link SemanticAgentOptions.maximumSequentialEdits} for the same message.
|
|
85
|
+
* - `expiredError`: The {@link SharedTreeChatQuery.edit} function was called after the issuing query has already completed.
|
|
86
|
+
*/
|
|
87
|
+
type: "success" | "disabledError" | "editingError" | "tooManyEditsError" | "expiredError";
|
|
76
88
|
|
|
77
89
|
/**
|
|
78
90
|
* A human-readable message describing the result of the edit attempt.
|
|
@@ -107,6 +119,9 @@ export interface SharedTreeChatQuery {
|
|
|
107
119
|
/**
|
|
108
120
|
* Edit the tree with the provided JavaScript function code.
|
|
109
121
|
* @remarks Attempting an edit may fail for a variety of reasons which are captured in the {@link EditResult | returned object}.
|
|
122
|
+
* If an edit fails, the tree will not be modified and the model may attempt another edit if desired.
|
|
123
|
+
* When the query ends, if the last edit attempt was successful, all edits made during the query will be merged into the agent's SharedTree.
|
|
124
|
+
* Otherwise, all edits made during the query will be discarded.
|
|
110
125
|
*/
|
|
111
126
|
edit(js: string): Promise<EditResult>;
|
|
112
127
|
}
|
package/src/index.ts
CHANGED
|
@@ -9,13 +9,20 @@
|
|
|
9
9
|
* @packageDocumentation
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
export {
|
|
12
|
+
export {
|
|
13
|
+
SharedTreeSemanticAgent,
|
|
14
|
+
bindEditor,
|
|
15
|
+
bindEditorImpl,
|
|
16
|
+
defaultEditor,
|
|
17
|
+
} from "./agent.js";
|
|
13
18
|
export type {
|
|
14
19
|
EditResult,
|
|
15
20
|
SharedTreeChatModel,
|
|
16
21
|
SharedTreeChatQuery,
|
|
17
22
|
Logger,
|
|
18
23
|
SemanticAgentOptions,
|
|
24
|
+
SynchronousEditor,
|
|
25
|
+
AsynchronousEditor,
|
|
19
26
|
} from "./api.js";
|
|
20
27
|
export { type TreeView, llmDefault } from "./utils.js";
|
|
21
28
|
export {
|
package/src/prompt.ts
CHANGED
|
@@ -87,6 +87,49 @@ const ${communize(exampleObjectName)} = context.create.${exampleObjectName}({ /*
|
|
|
87
87
|
// const ${communize(exampleObjectName)} = { /* ...properties... */ };
|
|
88
88
|
\`\`\`\n\n`;
|
|
89
89
|
|
|
90
|
+
const reinsertionExplanation = `Once non-primitive data has been removed from the tree (e.g. replaced via assignment, or removed from an array), that data cannot be re-inserted into the tree.
|
|
91
|
+
Instead, it must be deep cloned and recreated.
|
|
92
|
+
${
|
|
93
|
+
exampleObjectName === undefined
|
|
94
|
+
? ""
|
|
95
|
+
: `For example:
|
|
96
|
+
|
|
97
|
+
\`\`\`javascript
|
|
98
|
+
// Data is removed from the tree:
|
|
99
|
+
const ${communize(exampleObjectName)} = parent.${communize(exampleObjectName)};
|
|
100
|
+
parent.${communize(exampleObjectName)} = undefined;
|
|
101
|
+
// \`${communize(exampleObjectName)}\` cannot be directly re-inserted into the tree - this will throw an error:
|
|
102
|
+
// parent.${communize(exampleObjectName)} = ${communize(exampleObjectName)}; // ❌ A node may not be inserted into the tree more than once
|
|
103
|
+
// Instead, it must be deep cloned and recreated before insertion:
|
|
104
|
+
parent.${communize(exampleObjectName)} = context.create.${exampleObjectName}({ /*... deep clone all properties from \`${communize(exampleObjectName)}\` */ });
|
|
105
|
+
\`\`\`${
|
|
106
|
+
hasArrays
|
|
107
|
+
? `\n\nThe same applies when using arrays:\n\`\`\`javascript
|
|
108
|
+
// Data is removed from the tree:
|
|
109
|
+
const item = arrayOf${exampleObjectName}[0];
|
|
110
|
+
arrayOf${exampleObjectName}.removeAt(0);
|
|
111
|
+
// \`item\` cannot be directly re-inserted into the tree - this will throw an error:
|
|
112
|
+
arrayOf${exampleObjectName}.insertAt(0, item); // ❌ A node may not be inserted into the tree more than once
|
|
113
|
+
// Instead, it must be deep cloned and recreated before insertion:
|
|
114
|
+
arrayOf${exampleObjectName}.insertAt(0, context.create.${exampleObjectName}({ /*... deep clone all properties from \`item\` */ }));
|
|
115
|
+
\`\`\``
|
|
116
|
+
: ""
|
|
117
|
+
}${
|
|
118
|
+
hasMaps
|
|
119
|
+
? `\n\nThe same applies when using maps:
|
|
120
|
+
\`\`\`javascript
|
|
121
|
+
// Data is removed from the tree:
|
|
122
|
+
const value = mapOf${exampleObjectName}.get("someKey");
|
|
123
|
+
mapOf${exampleObjectName}.delete("someKey");
|
|
124
|
+
// \`value\` cannot be directly re-inserted into the tree - this will throw an error:
|
|
125
|
+
mapOf${exampleObjectName}.set("someKey", value); // ❌ A node may not be inserted into the tree more than once
|
|
126
|
+
// Instead, it must be deep cloned and recreated before insertion:
|
|
127
|
+
mapOf${exampleObjectName}.set("someKey", context.create.${exampleObjectName}({ /*... deep clone all properties from \`value\` */ }));
|
|
128
|
+
\`\`\``
|
|
129
|
+
: ""
|
|
130
|
+
}`
|
|
131
|
+
}`;
|
|
132
|
+
|
|
90
133
|
const arrayEditing = `#### Editing Arrays
|
|
91
134
|
|
|
92
135
|
The arrays in the tree are somewhat different than normal JavaScript \`Array\`s.
|
|
@@ -133,14 +176,13 @@ This \`context\` variable holds the current state of the tree in the \`root\` pr
|
|
|
133
176
|
You may mutate any part of the root tree as necessary, taking into account the caveats around${hasArrays ? ` arrays${hasMaps ? " and" : ""}` : ""}${hasMaps ? " maps" : ""} detailed below.
|
|
134
177
|
You may also set the \`root\` property of the context to be an entirely new value as long as it is one of the types allowed at the root of the tree (\`${Array.from(rootTypes.values(), (t) => getFriendlyName(t)).join(" | ")}\`).
|
|
135
178
|
${helperMethodExplanation}
|
|
136
|
-
|
|
137
179
|
${hasArrays ? arrayEditing : ""}${hasMaps ? mapEditing : ""}#### Additional Notes
|
|
138
180
|
|
|
139
181
|
Before outputting the edit function, you should check that it is valid according to both the application tree's schema and any restrictions of the editing APIs described above.
|
|
140
182
|
|
|
141
|
-
|
|
183
|
+
${builderExplanation}${reinsertionExplanation}
|
|
142
184
|
|
|
143
|
-
|
|
185
|
+
Finally, double check that the edits would accomplish the user's request (if it is possible).
|
|
144
186
|
|
|
145
187
|
`;
|
|
146
188
|
|