@fluidframework/tree-agent 2.63.0-359286 → 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 (65) hide show
  1. package/api-report/tree-agent.alpha.api.md +16 -18
  2. package/dist/agent.d.ts +1 -2
  3. package/dist/agent.d.ts.map +1 -1
  4. package/dist/agent.js +24 -51
  5. package/dist/agent.js.map +1 -1
  6. package/dist/alpha.d.ts +1 -0
  7. package/dist/api.d.ts +38 -20
  8. package/dist/api.d.ts.map +1 -1
  9. package/dist/api.js.map +1 -1
  10. package/dist/index.d.ts +2 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +10 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/langchain.d.ts +3 -4
  15. package/dist/langchain.d.ts.map +1 -1
  16. package/dist/langchain.js +2 -4
  17. package/dist/langchain.js.map +1 -1
  18. package/dist/prompt.d.ts +1 -2
  19. package/dist/prompt.d.ts.map +1 -1
  20. package/dist/prompt.js +18 -21
  21. package/dist/prompt.js.map +1 -1
  22. package/dist/ses.d.ts +21 -0
  23. package/dist/ses.d.ts.map +1 -0
  24. package/dist/ses.js +64 -0
  25. package/dist/ses.js.map +1 -0
  26. package/dist/utils.d.ts +4 -0
  27. package/dist/utils.d.ts.map +1 -1
  28. package/dist/utils.js +16 -1
  29. package/dist/utils.js.map +1 -1
  30. package/lib/agent.d.ts +1 -2
  31. package/lib/agent.d.ts.map +1 -1
  32. package/lib/agent.js +25 -52
  33. package/lib/agent.js.map +1 -1
  34. package/lib/alpha.d.ts +1 -0
  35. package/lib/api.d.ts +38 -20
  36. package/lib/api.d.ts.map +1 -1
  37. package/lib/api.js.map +1 -1
  38. package/lib/index.d.ts +2 -1
  39. package/lib/index.d.ts.map +1 -1
  40. package/lib/index.js +7 -0
  41. package/lib/index.js.map +1 -1
  42. package/lib/langchain.d.ts +3 -4
  43. package/lib/langchain.d.ts.map +1 -1
  44. package/lib/langchain.js +2 -4
  45. package/lib/langchain.js.map +1 -1
  46. package/lib/prompt.d.ts +1 -2
  47. package/lib/prompt.d.ts.map +1 -1
  48. package/lib/prompt.js +18 -21
  49. package/lib/prompt.js.map +1 -1
  50. package/lib/ses.d.ts +21 -0
  51. package/lib/ses.d.ts.map +1 -0
  52. package/lib/ses.js +60 -0
  53. package/lib/ses.js.map +1 -0
  54. package/lib/utils.d.ts +4 -0
  55. package/lib/utils.d.ts.map +1 -1
  56. package/lib/utils.js +14 -0
  57. package/lib/utils.js.map +1 -1
  58. package/package.json +10 -9
  59. package/src/agent.ts +34 -74
  60. package/src/api.ts +39 -23
  61. package/src/index.ts +2 -1
  62. package/src/langchain.ts +6 -8
  63. package/src/prompt.ts +19 -23
  64. package/src/ses.ts +73 -0
  65. package/src/utils.ts +14 -0
@@ -21,13 +21,19 @@ export function buildFunc<const Return extends z.ZodTypeAny, const Args extends
21
21
  }, ...args: Args): FunctionDef<Args, Return, Rest>;
22
22
 
23
23
  // @alpha @deprecated
24
- export function createSemanticAgent<TSchema extends ImplicitFieldSchema_2>(client: BaseChatModel, treeView: TreeView<TSchema>, options?: Readonly<SemanticAgentOptions<TSchema>>): SharedTreeSemanticAgent<TSchema>;
24
+ export function createSemanticAgent<TSchema extends ImplicitFieldSchema_2>(client: BaseChatModel, treeView: TreeView<TSchema>, options?: Readonly<SemanticAgentOptions>): SharedTreeSemanticAgent<TSchema>;
25
25
 
26
26
  // @alpha @deprecated
27
- export function createSemanticAgent<TSchema extends ImplicitFieldSchema_2>(client: BaseChatModel, node: ReadableField<TSchema> & TreeNode_2, options?: Readonly<SemanticAgentOptions<TSchema>>): SharedTreeSemanticAgent<TSchema>;
27
+ export function createSemanticAgent<TSchema extends ImplicitFieldSchema_2>(client: BaseChatModel, node: ReadableField<TSchema> & TreeNode_2, options?: Readonly<SemanticAgentOptions>): SharedTreeSemanticAgent<TSchema>;
28
28
 
29
29
  // @alpha @deprecated
30
- export function createSemanticAgent<TSchema extends ImplicitFieldSchema_2>(client: BaseChatModel, treeView: TreeView<TSchema> | (ReadableField<TSchema> & TreeNode_2), options?: Readonly<SemanticAgentOptions<TSchema>>): SharedTreeSemanticAgent<TSchema>;
30
+ export function createSemanticAgent<TSchema extends ImplicitFieldSchema_2>(client: BaseChatModel, treeView: TreeView<TSchema> | (ReadableField<TSchema> & TreeNode_2), options?: Readonly<SemanticAgentOptions>): SharedTreeSemanticAgent<TSchema>;
31
+
32
+ // @alpha
33
+ export function createSesEditEvaluator(options?: {
34
+ compartmentOptions?: CompartmentOptions;
35
+ lockdownOptions?: LockdownOptions;
36
+ }): Promise<SemanticAgentOptions["executeEdit"]>;
31
37
 
32
38
  // @alpha
33
39
  export type Ctor<T = any> = new (...args: any[]) => T;
@@ -36,7 +42,7 @@ export type Ctor<T = any> = new (...args: any[]) => T;
36
42
  export interface EditResult {
37
43
  message: string;
38
44
  // (undocumented)
39
- type: "success" | "disabledError" | "validationError" | "compileError" | "runtimeError" | "tooManyEditsError" | "expiredError";
45
+ type: "success" | "disabledError" | "validationError" | "executionError" | "tooManyEditsError" | "expiredError";
40
46
  }
41
47
 
42
48
  // @alpha
@@ -78,8 +84,6 @@ export class LangchainChatModel implements SharedTreeChatModel {
78
84
  // (undocumented)
79
85
  appendContext(text: string): void;
80
86
  // (undocumented)
81
- readonly editFunctionName = "editTree";
82
- // (undocumented)
83
87
  readonly editToolName = "GenerateTreeEditingCode";
84
88
  // (undocumented)
85
89
  get name(): string | undefined;
@@ -91,9 +95,8 @@ export class LangchainChatModel implements SharedTreeChatModel {
91
95
  export const llmDefault: unique symbol;
92
96
 
93
97
  // @alpha
94
- export interface Logger<TTree extends ReadableField<ImplicitFieldSchema> = ReadableField<ImplicitFieldSchema>> {
98
+ export interface Logger {
95
99
  log(message: string): void;
96
- treeToString?(tree: TTree): string;
97
100
  }
98
101
 
99
102
  // @alpha
@@ -102,20 +105,17 @@ export type MethodKeys<T> = {
102
105
  };
103
106
 
104
107
  // @alpha
105
- export interface SemanticAgentOptions<TSchema extends ImplicitFieldSchema> {
106
- // (undocumented)
108
+ export interface SemanticAgentOptions {
107
109
  domainHints?: string;
108
- // (undocumented)
109
- logger?: Logger<ReadableField<TSchema>>;
110
+ executeEdit?: (context: Record<string, unknown>, code: string) => void | Promise<void>;
111
+ logger?: Logger;
110
112
  maximumSequentialEdits?: number;
111
- // (undocumented)
112
- validator?: (js: string) => boolean;
113
+ validateEdit?: (code: string) => void | Promise<void>;
113
114
  }
114
115
 
115
116
  // @alpha
116
117
  export interface SharedTreeChatModel {
117
118
  appendContext?(text: string): void;
118
- editFunctionName?: string;
119
119
  editToolName?: string;
120
120
  name?: string;
121
121
  query(message: SharedTreeChatQuery): Promise<string>;
@@ -129,9 +129,7 @@ export interface SharedTreeChatQuery {
129
129
 
130
130
  // @alpha @sealed
131
131
  export class SharedTreeSemanticAgent<TSchema extends ImplicitFieldSchema> {
132
- constructor(client: SharedTreeChatModel, tree: TreeView<TSchema> | (ReadableField<TSchema> & TreeNode), options?: Readonly<SemanticAgentOptions<TSchema>> | undefined);
133
- // (undocumented)
134
- static editFunctionName: string;
132
+ constructor(client: SharedTreeChatModel, tree: TreeView<TSchema> | (ReadableField<TSchema> & TreeNode), options?: Readonly<SemanticAgentOptions> | undefined);
135
133
  query(userPrompt: string): Promise<string>;
136
134
  }
137
135
 
package/dist/agent.d.ts CHANGED
@@ -15,13 +15,12 @@ import { type TreeView } from "./utils.js";
15
15
  export declare class SharedTreeSemanticAgent<TSchema extends ImplicitFieldSchema> {
16
16
  private readonly client;
17
17
  private readonly options?;
18
- static editFunctionName: string;
19
18
  private readonly outerTree;
20
19
  /**
21
20
  * Whether or not the outer tree has changed since the last query finished.
22
21
  */
23
22
  private outerTreeIsDirty;
24
- constructor(client: SharedTreeChatModel, tree: TreeView<TSchema> | (ReadableField<TSchema> & TreeNode), options?: Readonly<SemanticAgentOptions<TSchema>> | undefined);
23
+ constructor(client: SharedTreeChatModel, tree: TreeView<TSchema> | (ReadableField<TSchema> & TreeNode), options?: Readonly<SemanticAgentOptions> | undefined);
25
24
  /**
26
25
  * Given a user prompt, return a response.
27
26
  *
@@ -1 +1 @@
1
- {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACX,mBAAmB,EAGnB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,KAAK,EACX,aAAa,EAIb,MAAM,4BAA4B,CAAC;AAGpC,OAAO,KAAK,EACX,mBAAmB,EAGnB,oBAAoB,EAEpB,MAAM,UAAU,CAAC;AAGlB,OAAO,EAIN,KAAK,QAAQ,EAGb,MAAM,YAAY,CAAC;AAapB;;;;GAIG;AACH,qBAAa,uBAAuB,CAAC,OAAO,SAAS,mBAAmB;IAUtE,OAAO,CAAC,QAAQ,CAAC,MAAM;IAEvB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC;IAX1B,OAAc,gBAAgB,SAAc;IAE5C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAmB;IAC7C;;OAEG;IACH,OAAO,CAAC,gBAAgB,CAAS;gBAGf,MAAM,EAAE,mBAAmB,EAC5C,IAAI,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,OAAO,CAAC,GAAG,QAAQ,CAAC,EAC5C,OAAO,CAAC,qDAAyC;IAkCnE;;;;;OAKG;IACU,KAAK,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAsEvD"}
1
+ {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACX,mBAAmB,EAGnB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,KAAK,EACX,aAAa,EAIb,MAAM,4BAA4B,CAAC;AAGpC,OAAO,KAAK,EAAE,mBAAmB,EAAc,oBAAoB,EAAU,MAAM,UAAU,CAAC;AAG9F,OAAO,EAIN,KAAK,QAAQ,EAGb,MAAM,YAAY,CAAC;AAQpB;;;;GAIG;AACH,qBAAa,uBAAuB,CAAC,OAAO,SAAS,mBAAmB;IAStE,OAAO,CAAC,QAAQ,CAAC,MAAM;IAEvB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC;IAT1B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAmB;IAC7C;;OAEG;IACH,OAAO,CAAC,gBAAgB,CAAS;gBAGf,MAAM,EAAE,mBAAmB,EAC5C,IAAI,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,OAAO,CAAC,GAAG,QAAQ,CAAC,EAC5C,OAAO,CAAC,4CAAgC;IAiC1D;;;;;OAKG;IACU,KAAK,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAsEvD"}
package/dist/agent.js CHANGED
@@ -15,10 +15,6 @@ const utils_js_1 = require("./utils.js");
15
15
  * @remarks This can be overridden by passing {@link SemanticAgentOptions.maximumSequentialEdits | maximumSequentialEdits} to {@link createSemanticAgent}.
16
16
  */
17
17
  const defaultMaxSequentialEdits = 20;
18
- /**
19
- * The name of the parameter passed to the edit function.
20
- */
21
- const paramsName = "params";
22
18
  /**
23
19
  * An agent that uses a {@link SharedTreeChatModel} to interact with a SharedTree.
24
20
  * @remarks This class forwards user queries to the chat model, and handles the application of any edits to the tree that the model requests.
@@ -42,7 +38,6 @@ class SharedTreeSemanticAgent {
42
38
  const prompt = (0, prompt_js_1.getPrompt)({
43
39
  subtree: this.outerTree,
44
40
  editToolName: this.client.editToolName,
45
- editFunctionName: this.client.editFunctionName,
46
41
  domainHints: this.options?.domainHints,
47
42
  });
48
43
  this.options?.logger?.log(`# Fluid Framework SharedTree AI Agent Log\n\n`);
@@ -84,9 +79,9 @@ class SharedTreeSemanticAgent {
84
79
  let active = true;
85
80
  let editCount = 0;
86
81
  let editFailed = false;
87
- const { editFunctionName } = this.client;
88
- const edit = async (js) => {
89
- if (editFunctionName === undefined) {
82
+ const { editToolName } = this.client;
83
+ const edit = async (editCode) => {
84
+ if (editToolName === undefined) {
90
85
  return {
91
86
  type: "disabledError",
92
87
  message: "Editing is not enabled for this model.",
@@ -105,7 +100,7 @@ class SharedTreeSemanticAgent {
105
100
  message: `The maximum number of edits (${maxEditCount}) for this query has been exceeded.`,
106
101
  };
107
102
  }
108
- const editResult = await applyTreeFunction(queryTree, js, editFunctionName ?? (0, utils_js_1.fail)("Expected edit function name to be defined"), this.options?.validator, this.options?.logger);
103
+ const editResult = await applyTreeFunction(queryTree, editCode, this.options?.validateEdit ?? defaultValidateEdit, this.options?.executeEdit ?? defaultExecuteEdit, this.options?.logger);
109
104
  editFailed ||= editResult.type !== "success";
110
105
  return editResult;
111
106
  };
@@ -124,7 +119,6 @@ class SharedTreeSemanticAgent {
124
119
  }
125
120
  }
126
121
  exports.SharedTreeSemanticAgent = SharedTreeSemanticAgent;
127
- SharedTreeSemanticAgent.editFunctionName = "editTree";
128
122
  /**
129
123
  * Creates an unhydrated node of the given schema with the given value.
130
124
  * @remarks If the schema is an object with {@link llmDefault | default values}, this function populates the node with those defaults.
@@ -157,14 +151,18 @@ function constructTreeNode(schema, value) {
157
151
  /**
158
152
  * Applies the given function (as a string of JavaScript code or an actual function) to the given tree.
159
153
  */
160
- async function applyTreeFunction(tree, fn, editFunctionName, validator, logger) {
154
+ async function applyTreeFunction(tree, editCode, validateEdit, executeEdit, logger) {
161
155
  logger?.log(`### Editing Tool Invoked\n\n`);
162
- logger?.log(`#### Generated Code\n\n\`\`\`javascript\n${fn}\n\`\`\`\n\n`);
163
- if (validator?.(fn.toString()) === false) {
156
+ logger?.log(`#### Generated Code\n\n\`\`\`javascript\n${editCode}\n\`\`\`\n\n`);
157
+ try {
158
+ await validateEdit(editCode);
159
+ }
160
+ catch (error) {
164
161
  logger?.log(`#### Code Validation Failed\n\n`);
162
+ logger?.log(`\`\`\`JSON\n${(0, utils_js_1.toErrorString)(error)}\n\`\`\`\n\n`);
165
163
  return {
166
164
  type: "validationError",
167
- message: "The generated code did not pass validation. Please try again.",
165
+ message: `The generated code did not pass validation: ${(0, utils_js_1.toErrorString)(error)}`,
168
166
  };
169
167
  }
170
168
  // Stick the tree schema constructors on an object passed to the function so that the LLM can create new nodes.
@@ -175,7 +173,7 @@ async function applyTreeFunction(tree, fn, editFunctionName, validator, logger)
175
173
  }
176
174
  // Fork a branch to edit. If the edit fails or produces an error, we discard this branch, otherwise we merge it.
177
175
  const editTree = tree.fork();
178
- const params = {
176
+ const context = {
179
177
  get root() {
180
178
  return editTree.field;
181
179
  },
@@ -184,55 +182,30 @@ async function applyTreeFunction(tree, fn, editFunctionName, validator, logger)
184
182
  },
185
183
  create,
186
184
  };
187
- let editFunction;
188
- if (typeof fn === "string") {
189
- try {
190
- editFunction = processLlmCode(fn, editFunctionName);
191
- }
192
- catch (error) {
193
- logger?.log(`#### Error\n\n`);
194
- const errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
195
- logger?.log(`\`\`\`JSON\n${errorMessage}\n\`\`\`\n\n`);
196
- editTree.branch.dispose();
197
- return {
198
- type: "compileError",
199
- message: `The supplied JavaScript function is not valid: ${errorMessage}`,
200
- };
201
- }
202
- }
203
- else {
204
- editFunction = fn;
205
- }
206
185
  try {
207
- await editFunction(params);
186
+ await executeEdit(context, editCode);
208
187
  }
209
188
  catch (error) {
210
189
  logger?.log(`#### Error\n\n`);
211
- const errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
212
- logger?.log(`\`\`\`JSON\n${errorMessage}\n\`\`\`\n\n`);
190
+ logger?.log(`\`\`\`JSON\n${(0, utils_js_1.toErrorString)(error)}\n\`\`\`\n\n`);
213
191
  editTree.branch.dispose();
214
192
  return {
215
- type: "runtimeError",
216
- message: `Running the function produced an error. The state of the tree will be reset to its previous state as it was before the function ran. Please try again. Here is the error: ${errorMessage}`,
193
+ type: "executionError",
194
+ message: `Running the generated code produced an error. The state of the tree will be reset to its previous state as it was before the code ran. Please try again. Here is the error: ${(0, utils_js_1.toErrorString)(error)}`,
217
195
  };
218
196
  }
219
197
  tree.branch.merge(editTree.branch);
220
198
  logger?.log(`#### New Tree State\n\n`);
221
- logger?.log(`${logger?.treeToString?.(tree.field) ?? `\`\`\`JSON\n${(0, prompt_js_1.stringifyTree)(tree.field)}\n\`\`\``}\n\n`);
199
+ logger?.log(`${`\`\`\`JSON\n${(0, prompt_js_1.stringifyTree)(tree.field)}\n\`\`\``}\n\n`);
222
200
  return {
223
201
  type: "success",
224
- message: `After running the function, the new state of the tree is:\n\n\`\`\`JSON\n${(0, prompt_js_1.stringifyTree)(tree.field)}\n\`\`\``,
202
+ message: `After running the code, the new state of the tree is:\n\n\`\`\`JSON\n${(0, prompt_js_1.stringifyTree)(tree.field)}\n\`\`\``,
225
203
  };
226
204
  }
227
- function processLlmCode(code, editFunctionName) {
228
- // TODO: use a library like Acorn to analyze the code more robustly
229
- const regex = new RegExp(`function\\s+${editFunctionName}\\s*\\(`);
230
- if (!regex.test(code)) {
231
- throw new Error(`Generated code does not contain a function named \`${editFunctionName}\``);
232
- }
233
- const executionCode = `${code}\n\n${editFunctionName}(${paramsName});`;
205
+ const defaultValidateEdit = () => { };
206
+ const defaultExecuteEdit = async (context, code) => {
234
207
  // eslint-disable-next-line no-new-func, @typescript-eslint/no-implied-eval
235
- const fn = new Function(paramsName, executionCode);
236
- return fn;
237
- }
208
+ const fn = new Function("context", code);
209
+ await fn(context);
210
+ };
238
211
  //# sourceMappingURL=agent.js.map
package/dist/agent.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAOH,+CAAgD;AAOhD,sDAAoE;AASpE,2CAAuD;AACvD,6CAAuC;AACvC,yCAOoB;AAEpB;;;GAGG;AACH,MAAM,yBAAyB,GA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K,CAAC,UAC/E,MAAM,CACN,CAAC;IACF,OAAO;QACN,IAAI,EAAE,SAAS;QACf,OAAO,EAAE,4EAA4E,IAAA,yBAAa,EAAC,IAAI,CAAC,KAAK,CAAC,UAAU;KACxH,CAAC;AACH,CAAC;AAED,SAAS,cAAc,CACtB,IAAY,EACZ,gBAAwB;IAExB,mEAAmE;IACnE,MAAM,KAAK,GAAG,IAAI,MAAM,CAAC,eAAe,gBAAgB,SAAS,CAAC,CAAC;IACnE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CACd,sDAAsD,gBAAgB,IAAI,CAC1E,CAAC;IACH,CAAC;IAED,MAAM,aAAa,GAAG,GAAG,IAAI,OAAO,gBAAgB,IAAI,UAAU,IAAI,CAAC;IACvE,2EAA2E;IAC3E,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC;IACnD,OAAO,EAA2B,CAAC;AACpC,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type {\n\tImplicitFieldSchema,\n\tTreeFieldFromImplicitField,\n\tTreeNodeSchema,\n} from \"@fluidframework/tree\";\nimport { TreeNode } from \"@fluidframework/tree\";\nimport type {\n\tReadableField,\n\tFactoryContentObject,\n\tInsertableContent,\n\tReadSchema,\n} from \"@fluidframework/tree/alpha\";\nimport { ObjectNodeSchema, Tree } from \"@fluidframework/tree/alpha\";\n\nimport type {\n\tSharedTreeChatModel,\n\tEditFunction,\n\tEditResult,\n\tSemanticAgentOptions,\n\tLogger,\n} from \"./api.js\";\nimport { getPrompt, stringifyTree } from \"./prompt.js\";\nimport { Subtree } from \"./subtree.js\";\nimport {\n\tconstructNode,\n\tgetFriendlyName,\n\tllmDefault,\n\ttype TreeView,\n\tfindNamedSchemas,\n\tfail,\n} from \"./utils.js\";\n\n/**\n * The default maximum number of sequential edits the LLM can make before we assume it's stuck in a loop.\n * @remarks This can be overridden by passing {@link SemanticAgentOptions.maximumSequentialEdits | maximumSequentialEdits} to {@link createSemanticAgent}.\n */\nconst defaultMaxSequentialEdits = 20;\n\n/**\n * The name of the parameter passed to the edit function.\n */\nconst paramsName = \"params\";\n\n/**\n * An agent that uses a {@link SharedTreeChatModel} to interact with a SharedTree.\n * @remarks This class forwards user queries to the chat model, and handles the application of any edits to the tree that the model requests.\n * @alpha @sealed\n */\nexport class SharedTreeSemanticAgent<TSchema extends ImplicitFieldSchema> {\n\tpublic static editFunctionName = \"editTree\";\n\t// Converted from ECMAScript private fields (#name) to TypeScript private members for easier debugger inspection.\n\tprivate readonly outerTree: Subtree<TSchema>;\n\t/**\n\t * Whether or not the outer tree has changed since the last query finished.\n\t */\n\tprivate outerTreeIsDirty = false;\n\n\tpublic constructor(\n\t\tprivate readonly client: SharedTreeChatModel,\n\t\ttree: TreeView<TSchema> | (ReadableField<TSchema> & TreeNode),\n\t\tprivate readonly options?: Readonly<SemanticAgentOptions<TSchema>>,\n\t) {\n\t\tif (tree instanceof TreeNode) {\n\t\t\tTree.on(tree, \"treeChanged\", () => (this.outerTreeIsDirty = true));\n\t\t} else {\n\t\t\ttree.events.on(\"changed\", () => (this.outerTreeIsDirty = true));\n\t\t}\n\n\t\tthis.outerTree = new Subtree(tree);\n\t\tconst prompt = getPrompt({\n\t\t\tsubtree: this.outerTree,\n\t\t\teditToolName: this.client.editToolName,\n\t\t\teditFunctionName: this.client.editFunctionName,\n\t\t\tdomainHints: this.options?.domainHints,\n\t\t});\n\t\tthis.options?.logger?.log(`# Fluid Framework SharedTree AI Agent Log\\n\\n`);\n\t\tconst now = new Date();\n\t\tconst formattedDate = now.toLocaleString(undefined, {\n\t\t\tweekday: \"long\",\n\t\t\tyear: \"numeric\",\n\t\t\tmonth: \"long\",\n\t\t\tday: \"numeric\",\n\t\t\thour: \"numeric\",\n\t\t\tminute: \"2-digit\",\n\t\t\tsecond: \"2-digit\",\n\t\t});\n\t\tthis.options?.logger?.log(`Agent created: **${formattedDate}**\\n\\n`);\n\t\tif (this.client.name !== undefined) {\n\t\t\tthis.options?.logger?.log(`Model: **${this.client.name}**\\n\\n`);\n\t\t}\n\t\tthis.client.appendContext?.(prompt);\n\t\tthis.options?.logger?.log(`## System Prompt\\n\\n${prompt}\\n\\n`);\n\t}\n\n\t/**\n\t * Given a user prompt, return a response.\n\t *\n\t * @param userPrompt - The prompt to send to the agent.\n\t * @returns The agent's response.\n\t */\n\tpublic async query(userPrompt: string): Promise<string> {\n\t\tthis.options?.logger?.log(`## User Query\\n\\n${userPrompt}\\n\\n`);\n\n\t\t// Notify the llm if the tree has changed since the last query, and if so, provide the new state of the tree.\n\t\tif (this.outerTreeIsDirty) {\n\t\t\tconst stringified = stringifyTree(this.outerTree.field);\n\t\t\tthis.client.appendContext?.(\n\t\t\t\t`The tree has changed since the last query. The new state of the tree is: \\n\\n\\`\\`\\`JSON\\n${stringified}\\n\\`\\`\\``,\n\t\t\t);\n\t\t\tthis.options?.logger?.log(\n\t\t\t\t`### Latest Tree State\\n\\nThe Tree was edited by a local or remote user since the previous query. The latest state is:\\n\\n\\`\\`\\`JSON\\n${stringified}\\n\\`\\`\\`\\n\\n`,\n\t\t\t);\n\t\t}\n\n\t\t// Fork a branch that will live for the lifetime of this query (which can be multiple LLM calls if the there are errors or the LLM decides to take multiple steps to accomplish a task).\n\t\t// The branch will be merged back into the outer branch if and only if the query succeeds.\n\t\tconst queryTree = this.outerTree.fork();\n\t\tconst maxEditCount = this.options?.maximumSequentialEdits ?? defaultMaxSequentialEdits;\n\t\tlet active = true;\n\t\tlet editCount = 0;\n\t\tlet editFailed = false;\n\t\tconst { editFunctionName } = this.client;\n\t\tconst edit = async (js: string): Promise<EditResult> => {\n\t\t\tif (editFunctionName === undefined) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"disabledError\",\n\t\t\t\t\tmessage: \"Editing is not enabled for this model.\",\n\t\t\t\t};\n\t\t\t}\n\t\t\tif (!active) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"expiredError\",\n\t\t\t\t\tmessage: `The query has already completed. Further edits are not allowed.`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif (++editCount > maxEditCount) {\n\t\t\t\teditFailed = true;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"tooManyEditsError\",\n\t\t\t\t\tmessage: `The maximum number of edits (${maxEditCount}) for this query has been exceeded.`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst editResult = await applyTreeFunction(\n\t\t\t\tqueryTree,\n\t\t\t\tjs,\n\t\t\t\teditFunctionName ?? fail(\"Expected edit function name to be defined\"),\n\t\t\t\tthis.options?.validator,\n\t\t\t\tthis.options?.logger,\n\t\t\t);\n\n\t\t\teditFailed ||= editResult.type !== \"success\";\n\t\t\treturn editResult;\n\t\t};\n\n\t\tconst responseMessage = await this.client.query({\n\t\t\ttext: userPrompt,\n\t\t\tedit,\n\t\t});\n\t\tactive = false;\n\n\t\tif (!editFailed) {\n\t\t\tthis.outerTree.branch.merge(queryTree.branch);\n\t\t\tthis.outerTreeIsDirty = false;\n\t\t}\n\t\tthis.options?.logger?.log(`## Response\\n\\n`);\n\t\tthis.options?.logger?.log(`${responseMessage}\\n\\n`);\n\t\treturn responseMessage;\n\t}\n}\n\n/**\n * Creates an unhydrated node of the given schema with the given value.\n * @remarks If the schema is an object with {@link llmDefault | default values}, this function populates the node with those defaults.\n */\nfunction constructTreeNode(schema: TreeNodeSchema, value: FactoryContentObject): TreeNode {\n\tif (schema instanceof ObjectNodeSchema) {\n\t\tconst inputWithDefaults: Record<string, InsertableContent | undefined> = {};\n\t\tfor (const [key, field] of schema.fields) {\n\t\t\tif (value[key] === undefined) {\n\t\t\t\tif (\n\t\t\t\t\ttypeof field.metadata.custom === \"object\" &&\n\t\t\t\t\tfield.metadata.custom !== null &&\n\t\t\t\t\tllmDefault in field.metadata.custom\n\t\t\t\t) {\n\t\t\t\t\tconst defaulter = field.metadata.custom[llmDefault];\n\t\t\t\t\tif (typeof defaulter === \"function\") {\n\t\t\t\t\t\tconst defaultValue: unknown = defaulter();\n\t\t\t\t\t\tif (defaultValue !== undefined) {\n\t\t\t\t\t\t\tinputWithDefaults[key] = defaultValue;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tinputWithDefaults[key] = value[key];\n\t\t\t}\n\t\t}\n\t\treturn constructNode(schema, inputWithDefaults);\n\t}\n\treturn constructNode(schema, value);\n}\n\n/**\n * Applies the given function (as a string of JavaScript code or an actual function) to the given tree.\n */\nasync function applyTreeFunction<TSchema extends ImplicitFieldSchema>(\n\ttree: Subtree<TSchema>,\n\tfn: string | EditFunction<TSchema>,\n\teditFunctionName: string,\n\tvalidator: ((js: string) => boolean) | undefined,\n\tlogger: Logger<ReadableField<TSchema>> | undefined,\n): Promise<EditResult> {\n\tlogger?.log(`### Editing Tool Invoked\\n\\n`);\n\tlogger?.log(`#### Generated Code\\n\\n\\`\\`\\`javascript\\n${fn}\\n\\`\\`\\`\\n\\n`);\n\n\tif (validator?.(fn.toString()) === false) {\n\t\tlogger?.log(`#### Code Validation Failed\\n\\n`);\n\t\treturn {\n\t\t\ttype: \"validationError\",\n\t\t\tmessage: \"The generated code did not pass validation. Please try again.\",\n\t\t};\n\t}\n\n\t// Stick the tree schema constructors on an object passed to the function so that the LLM can create new nodes.\n\tconst create: Record<string, (input: FactoryContentObject) => TreeNode> = {};\n\tfor (const schema of findNamedSchemas(tree.schema)) {\n\t\tconst name = getFriendlyName(schema);\n\t\tcreate[name] = (input: FactoryContentObject) => constructTreeNode(schema, input);\n\t}\n\n\t// Fork a branch to edit. If the edit fails or produces an error, we discard this branch, otherwise we merge it.\n\tconst editTree = tree.fork();\n\tconst params = {\n\t\tget root(): ReadableField<TSchema> {\n\t\t\treturn editTree.field;\n\t\t},\n\t\tset root(value: TreeFieldFromImplicitField<ReadSchema<TSchema>>) {\n\t\t\teditTree.field = value;\n\t\t},\n\t\tcreate,\n\t};\n\n\tlet editFunction: EditFunction<TSchema>;\n\tif (typeof fn === \"string\") {\n\t\ttry {\n\t\t\teditFunction = processLlmCode(fn, editFunctionName);\n\t\t} catch (error) {\n\t\t\tlogger?.log(`#### Error\\n\\n`);\n\t\t\tconst errorMessage = error instanceof Error ? error.message : JSON.stringify(error);\n\t\t\tlogger?.log(`\\`\\`\\`JSON\\n${errorMessage}\\n\\`\\`\\`\\n\\n`);\n\t\t\teditTree.branch.dispose();\n\t\t\treturn {\n\t\t\t\ttype: \"compileError\",\n\t\t\t\tmessage: `The supplied JavaScript function is not valid: ${errorMessage}`,\n\t\t\t};\n\t\t}\n\t} else {\n\t\teditFunction = fn;\n\t}\n\n\ttry {\n\t\tawait editFunction(params);\n\t} catch (error: unknown) {\n\t\tlogger?.log(`#### Error\\n\\n`);\n\t\tconst errorMessage = error instanceof Error ? error.message : JSON.stringify(error);\n\t\tlogger?.log(`\\`\\`\\`JSON\\n${errorMessage}\\n\\`\\`\\`\\n\\n`);\n\t\teditTree.branch.dispose();\n\t\treturn {\n\t\t\ttype: \"runtimeError\",\n\t\t\tmessage: `Running the function produced an error. The state of the tree will be reset to its previous state as it was before the function ran. Please try again. Here is the error: ${errorMessage}`,\n\t\t};\n\t}\n\n\ttree.branch.merge(editTree.branch);\n\tlogger?.log(`#### New Tree State\\n\\n`);\n\tlogger?.log(\n\t\t`${\n\t\t\tlogger?.treeToString?.(tree.field) ?? `\\`\\`\\`JSON\\n${stringifyTree(tree.field)}\\n\\`\\`\\``\n\t\t}\\n\\n`,\n\t);\n\treturn {\n\t\ttype: \"success\",\n\t\tmessage: `After running the function, the new state of the tree is:\\n\\n\\`\\`\\`JSON\\n${stringifyTree(tree.field)}\\n\\`\\`\\``,\n\t};\n}\n\nfunction processLlmCode<TSchema extends ImplicitFieldSchema>(\n\tcode: string,\n\teditFunctionName: string,\n): EditFunction<TSchema> {\n\t// TODO: use a library like Acorn to analyze the code more robustly\n\tconst regex = new RegExp(`function\\\\s+${editFunctionName}\\\\s*\\\\(`);\n\tif (!regex.test(code)) {\n\t\tthrow new Error(\n\t\t\t`Generated code does not contain a function named \\`${editFunctionName}\\``,\n\t\t);\n\t}\n\n\tconst executionCode = `${code}\\n\\n${editFunctionName}(${paramsName});`;\n\t// eslint-disable-next-line no-new-func, @typescript-eslint/no-implied-eval\n\tconst fn = new Function(paramsName, executionCode);\n\treturn fn as EditFunction<TSchema>;\n}\n"]}
1
+ {"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAOH,+CAAgD;AAOhD,sDAAoE;AAGpE,2CAAuD;AACvD,6CAAuC;AACvC,yCAOoB;AAEpB;;;GAGG;AACH,MAAM,yBAAyB,GAAG,EAAE,CAAC;AAErC;;;;GAIG;AACH,MAAa,uBAAuB;IAQnC,YACkB,MAA2B,EAC5C,IAA6D,EAC5C,OAAwC;QAFxC,WAAM,GAAN,MAAM,CAAqB;QAE3B,YAAO,GAAP,OAAO,CAAiC;QAR1D;;WAEG;QACK,qBAAgB,GAAG,KAAK,CAAC;QAOhC,IAAI,IAAI,YAAY,eAAQ,EAAE,CAAC;YAC9B,YAAI,CAAC,EAAE,CAAC,IAAI,EAAE,aAAa,EAAE,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAAC,CAAC;QACpE,CAAC;aAAM,CAAC;YACP,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAAC,CAAC;QACjE,CAAC;QAED,IAAI,CAAC,SAAS,GAAG,IAAI,oBAAO,CAAC,IAAI,CAAC,CAAC;QACnC,MAAM,MAAM,GAAG,IAAA,qBAAS,EAAC;YACxB,OAAO,EAAE,IAAI,CAAC,SAAS;YACvB,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC,YAAY;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,EAAE;IACH,2EAA2E;IAC3E,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACzC,MAAM,EAAE,CAAC,OAAO,CAAC,CAAC;AACnB,CAAC,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type {\n\tImplicitFieldSchema,\n\tTreeFieldFromImplicitField,\n\tTreeNodeSchema,\n} from \"@fluidframework/tree\";\nimport { TreeNode } from \"@fluidframework/tree\";\nimport type {\n\tReadableField,\n\tFactoryContentObject,\n\tInsertableContent,\n\tReadSchema,\n} from \"@fluidframework/tree/alpha\";\nimport { ObjectNodeSchema, Tree } from \"@fluidframework/tree/alpha\";\n\nimport type { SharedTreeChatModel, EditResult, SemanticAgentOptions, Logger } from \"./api.js\";\nimport { getPrompt, stringifyTree } from \"./prompt.js\";\nimport { Subtree } from \"./subtree.js\";\nimport {\n\tconstructNode,\n\tgetFriendlyName,\n\tllmDefault,\n\ttype TreeView,\n\tfindNamedSchemas,\n\ttoErrorString,\n} from \"./utils.js\";\n\n/**\n * The default maximum number of sequential edits the LLM can make before we assume it's stuck in a loop.\n * @remarks This can be overridden by passing {@link SemanticAgentOptions.maximumSequentialEdits | maximumSequentialEdits} to {@link createSemanticAgent}.\n */\nconst defaultMaxSequentialEdits = 20;\n\n/**\n * An agent that uses a {@link SharedTreeChatModel} to interact with a SharedTree.\n * @remarks This class forwards user queries to the chat model, and handles the application of any edits to the tree that the model requests.\n * @alpha @sealed\n */\nexport class SharedTreeSemanticAgent<TSchema extends ImplicitFieldSchema> {\n\t// Converted from ECMAScript private fields (#name) to TypeScript private members for easier debugger inspection.\n\tprivate readonly outerTree: Subtree<TSchema>;\n\t/**\n\t * Whether or not the outer tree has changed since the last query finished.\n\t */\n\tprivate outerTreeIsDirty = false;\n\n\tpublic constructor(\n\t\tprivate readonly client: SharedTreeChatModel,\n\t\ttree: TreeView<TSchema> | (ReadableField<TSchema> & TreeNode),\n\t\tprivate readonly options?: Readonly<SemanticAgentOptions>,\n\t) {\n\t\tif (tree instanceof TreeNode) {\n\t\t\tTree.on(tree, \"treeChanged\", () => (this.outerTreeIsDirty = true));\n\t\t} else {\n\t\t\ttree.events.on(\"changed\", () => (this.outerTreeIsDirty = true));\n\t\t}\n\n\t\tthis.outerTree = new Subtree(tree);\n\t\tconst prompt = getPrompt({\n\t\t\tsubtree: this.outerTree,\n\t\t\teditToolName: this.client.editToolName,\n\t\t\tdomainHints: this.options?.domainHints,\n\t\t});\n\t\tthis.options?.logger?.log(`# Fluid Framework SharedTree AI Agent Log\\n\\n`);\n\t\tconst now = new Date();\n\t\tconst formattedDate = now.toLocaleString(undefined, {\n\t\t\tweekday: \"long\",\n\t\t\tyear: \"numeric\",\n\t\t\tmonth: \"long\",\n\t\t\tday: \"numeric\",\n\t\t\thour: \"numeric\",\n\t\t\tminute: \"2-digit\",\n\t\t\tsecond: \"2-digit\",\n\t\t});\n\t\tthis.options?.logger?.log(`Agent created: **${formattedDate}**\\n\\n`);\n\t\tif (this.client.name !== undefined) {\n\t\t\tthis.options?.logger?.log(`Model: **${this.client.name}**\\n\\n`);\n\t\t}\n\t\tthis.client.appendContext?.(prompt);\n\t\tthis.options?.logger?.log(`## System Prompt\\n\\n${prompt}\\n\\n`);\n\t}\n\n\t/**\n\t * Given a user prompt, return a response.\n\t *\n\t * @param userPrompt - The prompt to send to the agent.\n\t * @returns The agent's response.\n\t */\n\tpublic async query(userPrompt: string): Promise<string> {\n\t\tthis.options?.logger?.log(`## User Query\\n\\n${userPrompt}\\n\\n`);\n\n\t\t// Notify the llm if the tree has changed since the last query, and if so, provide the new state of the tree.\n\t\tif (this.outerTreeIsDirty) {\n\t\t\tconst stringified = stringifyTree(this.outerTree.field);\n\t\t\tthis.client.appendContext?.(\n\t\t\t\t`The tree has changed since the last query. The new state of the tree is: \\n\\n\\`\\`\\`JSON\\n${stringified}\\n\\`\\`\\``,\n\t\t\t);\n\t\t\tthis.options?.logger?.log(\n\t\t\t\t`### Latest Tree State\\n\\nThe Tree was edited by a local or remote user since the previous query. The latest state is:\\n\\n\\`\\`\\`JSON\\n${stringified}\\n\\`\\`\\`\\n\\n`,\n\t\t\t);\n\t\t}\n\n\t\t// Fork a branch that will live for the lifetime of this query (which can be multiple LLM calls if the there are errors or the LLM decides to take multiple steps to accomplish a task).\n\t\t// The branch will be merged back into the outer branch if and only if the query succeeds.\n\t\tconst queryTree = this.outerTree.fork();\n\t\tconst maxEditCount = this.options?.maximumSequentialEdits ?? defaultMaxSequentialEdits;\n\t\tlet active = true;\n\t\tlet editCount = 0;\n\t\tlet editFailed = false;\n\t\tconst { editToolName } = this.client;\n\t\tconst edit = async (editCode: string): Promise<EditResult> => {\n\t\t\tif (editToolName === undefined) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"disabledError\",\n\t\t\t\t\tmessage: \"Editing is not enabled for this model.\",\n\t\t\t\t};\n\t\t\t}\n\t\t\tif (!active) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"expiredError\",\n\t\t\t\t\tmessage: `The query has already completed. Further edits are not allowed.`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif (++editCount > maxEditCount) {\n\t\t\t\teditFailed = true;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"tooManyEditsError\",\n\t\t\t\t\tmessage: `The maximum number of edits (${maxEditCount}) for this query has been exceeded.`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst editResult = await applyTreeFunction(\n\t\t\t\tqueryTree,\n\t\t\t\teditCode,\n\t\t\t\tthis.options?.validateEdit ?? defaultValidateEdit,\n\t\t\t\tthis.options?.executeEdit ?? defaultExecuteEdit,\n\t\t\t\tthis.options?.logger,\n\t\t\t);\n\n\t\t\teditFailed ||= editResult.type !== \"success\";\n\t\t\treturn editResult;\n\t\t};\n\n\t\tconst responseMessage = await this.client.query({\n\t\t\ttext: userPrompt,\n\t\t\tedit,\n\t\t});\n\t\tactive = false;\n\n\t\tif (!editFailed) {\n\t\t\tthis.outerTree.branch.merge(queryTree.branch);\n\t\t\tthis.outerTreeIsDirty = false;\n\t\t}\n\t\tthis.options?.logger?.log(`## Response\\n\\n`);\n\t\tthis.options?.logger?.log(`${responseMessage}\\n\\n`);\n\t\treturn responseMessage;\n\t}\n}\n\n/**\n * Creates an unhydrated node of the given schema with the given value.\n * @remarks If the schema is an object with {@link llmDefault | default values}, this function populates the node with those defaults.\n */\nfunction constructTreeNode(schema: TreeNodeSchema, value: FactoryContentObject): TreeNode {\n\tif (schema instanceof ObjectNodeSchema) {\n\t\tconst inputWithDefaults: Record<string, InsertableContent | undefined> = {};\n\t\tfor (const [key, field] of schema.fields) {\n\t\t\tif (value[key] === undefined) {\n\t\t\t\tif (\n\t\t\t\t\ttypeof field.metadata.custom === \"object\" &&\n\t\t\t\t\tfield.metadata.custom !== null &&\n\t\t\t\t\tllmDefault in field.metadata.custom\n\t\t\t\t) {\n\t\t\t\t\tconst defaulter = field.metadata.custom[llmDefault];\n\t\t\t\t\tif (typeof defaulter === \"function\") {\n\t\t\t\t\t\tconst defaultValue: unknown = defaulter();\n\t\t\t\t\t\tif (defaultValue !== undefined) {\n\t\t\t\t\t\t\tinputWithDefaults[key] = defaultValue;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tinputWithDefaults[key] = value[key];\n\t\t\t}\n\t\t}\n\t\treturn constructNode(schema, inputWithDefaults);\n\t}\n\treturn constructNode(schema, value);\n}\n\n/**\n * Applies the given function (as a string of JavaScript code or an actual function) to the given tree.\n */\nasync function applyTreeFunction<TSchema extends ImplicitFieldSchema>(\n\ttree: Subtree<TSchema>,\n\teditCode: string,\n\tvalidateEdit: Required<SemanticAgentOptions>[\"validateEdit\"],\n\texecuteEdit: Required<SemanticAgentOptions>[\"executeEdit\"],\n\tlogger: Logger | undefined,\n): Promise<EditResult> {\n\tlogger?.log(`### Editing Tool Invoked\\n\\n`);\n\tlogger?.log(`#### Generated Code\\n\\n\\`\\`\\`javascript\\n${editCode}\\n\\`\\`\\`\\n\\n`);\n\n\ttry {\n\t\tawait validateEdit(editCode);\n\t} catch (error: unknown) {\n\t\tlogger?.log(`#### Code Validation Failed\\n\\n`);\n\t\tlogger?.log(`\\`\\`\\`JSON\\n${toErrorString(error)}\\n\\`\\`\\`\\n\\n`);\n\t\treturn {\n\t\t\ttype: \"validationError\",\n\t\t\tmessage: `The generated code did not pass validation: ${toErrorString(error)}`,\n\t\t};\n\t}\n\n\t// Stick the tree schema constructors on an object passed to the function so that the LLM can create new nodes.\n\tconst create: Record<string, (input: FactoryContentObject) => TreeNode> = {};\n\tfor (const schema of findNamedSchemas(tree.schema)) {\n\t\tconst name = getFriendlyName(schema);\n\t\tcreate[name] = (input: FactoryContentObject) => constructTreeNode(schema, input);\n\t}\n\n\t// Fork a branch to edit. If the edit fails or produces an error, we discard this branch, otherwise we merge it.\n\tconst editTree = tree.fork();\n\tconst context = {\n\t\tget root(): ReadableField<TSchema> {\n\t\t\treturn editTree.field;\n\t\t},\n\t\tset root(value: TreeFieldFromImplicitField<ReadSchema<TSchema>>) {\n\t\t\teditTree.field = value;\n\t\t},\n\t\tcreate,\n\t};\n\n\ttry {\n\t\tawait executeEdit(context, editCode);\n\t} catch (error: unknown) {\n\t\tlogger?.log(`#### Error\\n\\n`);\n\t\tlogger?.log(`\\`\\`\\`JSON\\n${toErrorString(error)}\\n\\`\\`\\`\\n\\n`);\n\t\teditTree.branch.dispose();\n\t\treturn {\n\t\t\ttype: \"executionError\",\n\t\t\tmessage: `Running the generated code produced an error. The state of the tree will be reset to its previous state as it was before the code ran. Please try again. Here is the error: ${toErrorString(error)}`,\n\t\t};\n\t}\n\n\ttree.branch.merge(editTree.branch);\n\tlogger?.log(`#### New Tree State\\n\\n`);\n\tlogger?.log(`${`\\`\\`\\`JSON\\n${stringifyTree(tree.field)}\\n\\`\\`\\``}\\n\\n`);\n\treturn {\n\t\ttype: \"success\",\n\t\tmessage: `After running the code, the new state of the tree is:\\n\\n\\`\\`\\`JSON\\n${stringifyTree(tree.field)}\\n\\`\\`\\``,\n\t};\n}\n\nconst defaultValidateEdit: Required<SemanticAgentOptions>[\"validateEdit\"] = () => {};\n\nconst defaultExecuteEdit: Required<SemanticAgentOptions>[\"executeEdit\"] = async (\n\tcontext,\n\tcode,\n) => {\n\t// eslint-disable-next-line no-new-func, @typescript-eslint/no-implied-eval\n\tconst fn = new Function(\"context\", code);\n\tawait fn(context);\n};\n"]}
package/dist/alpha.d.ts CHANGED
@@ -35,6 +35,7 @@ export {
35
35
  TreeView,
36
36
  buildFunc,
37
37
  createSemanticAgent,
38
+ createSesEditEvaluator,
38
39
  exposeMethodsSymbol,
39
40
  llmDefault
40
41
  // #endregion
package/dist/api.d.ts CHANGED
@@ -8,43 +8,60 @@ import type { FactoryContentObject, ReadableField } from "@fluidframework/tree/a
8
8
  * Logger interface for logging events from a {@link SharedTreeSemanticAgent}.
9
9
  * @alpha
10
10
  */
11
- export interface Logger<TTree extends ReadableField<ImplicitFieldSchema> = ReadableField<ImplicitFieldSchema>> {
11
+ export interface Logger {
12
12
  /**
13
13
  * Log a message.
14
14
  */
15
15
  log(message: string): void;
16
- /**
17
- * Optional function to override the default tree stringification (JSON) when logging tree state.
18
- */
19
- treeToString?(tree: TTree): string;
20
16
  }
21
17
  /**
22
18
  * Options used to parameterize the creation of a {@link SharedTreeSemanticAgent}.
23
19
  * @alpha
24
20
  */
25
- export interface SemanticAgentOptions<TSchema extends ImplicitFieldSchema> {
21
+ export interface SemanticAgentOptions {
22
+ /**
23
+ * Additional information about the application domain that will be included in the context provided to the {@link SharedTreeChatModel | model}.
24
+ */
26
25
  domainHints?: string;
27
- validator?: (js: string) => boolean;
26
+ /**
27
+ * Validates any generated JavaScript created by the {@link SharedTreeChatModel.editToolName | model's editing tool}.
28
+ * @remarks This happens before the code is executed - execution can be intercepted by using the {@link SemanticAgentOptions.executeEdit | executeEdit} callback.
29
+ * @param code - The generated JavaScript code as a string.
30
+ * @throws If the code is invalid, this function should throw an error with a human-readable message describing why it is invalid.
31
+ */
32
+ validateEdit?: (code: string) => void | Promise<void>;
33
+ /**
34
+ * Evaluates (executes) any generated JavaScript created by the {@link SharedTreeChatModel.editToolName | model's editing tool}.
35
+ * @remarks This happens only after the code has been successfully validated by the optional {@link SemanticAgentOptions.validateEdit | validateEdit} function.
36
+ * @param context - An object that must be provided to the generated code as a variable named "context" in its top-level scope.
37
+ * @param code - The generated JavaScript code as a string.
38
+ * @throws If an error is thrown while evaluating the code, it will be caught and the message will be forwarded to the model for debugging.
39
+ * @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.
40
+ * Use a library such as SES to provide a more secure implementation - see {@link createSesEditEvaluator} for a drop-in option.
41
+ */
42
+ executeEdit?: (context: Record<string, unknown>, code: string) => void | Promise<void>;
28
43
  /**
29
44
  * The maximum number of sequential edits the LLM can make before we assume it's stuck in a loop.
30
45
  */
31
46
  maximumSequentialEdits?: number;
32
- logger?: Logger<ReadableField<TSchema>>;
47
+ /**
48
+ * If supplied, generates human-readable markdown text describing the actions taken by the {@link SharedTreeSemanticAgent | agent} as it performs queries.
49
+ */
50
+ logger?: Logger;
33
51
  }
34
52
  /**
35
53
  * A result from an edit attempt via the {@link SharedTreeChatQuery.edit} function.
36
54
  * @remarks
37
55
  * - `success`: The edit was successfully applied.
38
- * - `disabledError`: The model is not allowed to edit the tree (i.e. {@link SharedTreeChatModel.editFunctionName} was not provided).
39
- * - `validationError`: The provided JavaScript did not pass the optional {@link SemanticAgentOptions.validator} function.
40
- * - `compileError`: The provided JavaScript could not be parsed or compiled.
41
- * - `runtimeError`: An error was thrown while executing the provided JavaScript.
56
+ * - `disabledError`: The model is not allowed to edit the tree (i.e. {@link SharedTreeChatModel.editToolName} was not provided).
57
+ * - `validationError`: The provided JavaScript did not pass the optional {@link SemanticAgentOptions.validateEdit} function.
58
+ * - `executionError`: An error was thrown while parsing or executing the provided JavaScript.
42
59
  * - `tooManyEditsError`: The {@link SharedTreeChatQuery.edit} function has been called more than the number of times specified by {@link SemanticAgentOptions.maximumSequentialEdits} for the same message.
43
60
  * - `expiredError`: The {@link SharedTreeChatQuery.edit} function was called after the issuing query has already completed.
44
61
  * @alpha
45
62
  */
46
63
  export interface EditResult {
47
- type: "success" | "disabledError" | "validationError" | "compileError" | "runtimeError" | "tooManyEditsError" | "expiredError";
64
+ type: "success" | "disabledError" | "validationError" | "executionError" | "tooManyEditsError" | "expiredError";
48
65
  /**
49
66
  * A human-readable message describing the result of the edit attempt.
50
67
  * @remarks In the case of an error, this message is appropriate to include in a model's chat history.
@@ -73,6 +90,7 @@ export interface SharedTreeChatQuery {
73
90
  }
74
91
  /**
75
92
  * A plugin interface that handles queries from a {@link SharedTreeSemanticAgent}.
93
+ * @remarks This wraps an underlying communication with an LLM and receives all necessary {@link SharedTreeChatModel.appendContext | context} from the {@link SharedTreeSemanticAgent | agent} for the LLM to properly analyze and edit the tree.
76
94
  * @alpha
77
95
  */
78
96
  export interface SharedTreeChatModel {
@@ -83,18 +101,18 @@ export interface SharedTreeChatModel {
83
101
  */
84
102
  name?: string;
85
103
  /**
86
- * The name of the tool that the model should use to edit the tree, if any.
87
- * @remarks If supplied, this will be mentioned in the context provided to the model and the model will be encouraged to use it when a user query requires an edit.
104
+ * The name of the tool that the model should use to edit the tree.
105
+ * @remarks If supplied, this will be mentioned in the context provided to the model so that the underlying LLM will be encouraged to use it when a user query requires an edit.
106
+ * The model should "implement" the tool by registering it with the underlying LLM API.
107
+ * The tool should take an LLM-generated JavaScript function as input and supply it to the {@link SharedTreeChatQuery.edit | edit} function.
108
+ * Instructions for generating the proper function signature and implementation will be provided by the {@link SharedTreeSemanticAgent | agent} via {@link SharedTreeChatModel.appendContext | context}.
109
+ * If not supplied, the model will not be able to edit the tree (running the {@link SharedTreeChatQuery.edit | edit} function will fail).
88
110
  */
89
111
  editToolName?: string;
90
- /**
91
- * The name of the function that the model should generate to edit the tree.
92
- * @remarks If not supplied, the model will not be allowed to edit the tree.
93
- */
94
- editFunctionName?: string;
95
112
  /**
96
113
  * Add contextual information to the model that may be relevant to future queries.
97
114
  * @remarks In practice, this may be implemented by e.g. appending a "system" message to an LLM's chat/message history.
115
+ * This context must be present in the context window of every {@link SharedTreeChatModel.query | query} for e.g. {@link SharedTreeChatModel.editToolName | editing} to work.
98
116
  * @param text - The message or context to append.
99
117
  */
100
118
  appendContext?(text: string): void;
package/dist/api.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAC1E,OAAO,KAAK,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAEtF;;;GAGG;AACH,MAAM,WAAW,MAAM,CACtB,KAAK,SAAS,aAAa,CAAC,mBAAmB,CAAC,GAAG,aAAa,CAAC,mBAAmB,CAAC;IAErF;;OAEG;IACH,GAAG,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B;;OAEG;IACH,YAAY,CAAC,CAAC,IAAI,EAAE,KAAK,GAAG,MAAM,CAAC;CACnC;AAED;;;GAGG;AACH,MAAM,WAAW,oBAAoB,CAAC,OAAO,SAAS,mBAAmB;IACxE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC;IACpC;;OAEG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,MAAM,CAAC,EAAE,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC;CACxC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,UAAU;IAC1B,IAAI,EACD,SAAS,GACT,eAAe,GACf,iBAAiB,GACjB,cAAc,GACd,cAAc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AAG,IAAI,CAAC;IACnC;;;OAGG;IACH,KAAK,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACrD;AAED;;GAEG;AACH,MAAM,MAAM,YAAY,CAAC,OAAO,SAAS,mBAAmB,IAAI,CAAC,EAChE,IAAI,EACJ,MAAM,GACN,EAAE;IACF,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;IAC7B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,oBAAoB,KAAK,QAAQ,CAAC,CAAC;CAClE,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC"}
package/dist/api.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAiEH;;GAEG;AACH,SAAgB,YAAY,CAAC,KAAc;IAC1C,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACjD,OAAO,KAAK,CAAC;IACd,CAAC;IACD,OAAO,CACN,OAAQ,KAAoB,CAAC,IAAI,KAAK,QAAQ;QAC9C,OAAQ,KAAoB,CAAC,OAAO,KAAK,QAAQ,CACjD,CAAC;AACH,CAAC;AARD,oCAQC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type { ImplicitFieldSchema, TreeNode } from \"@fluidframework/tree\";\nimport type { FactoryContentObject, ReadableField } from \"@fluidframework/tree/alpha\";\n\n/**\n * Logger interface for logging events from a {@link SharedTreeSemanticAgent}.\n * @alpha\n */\nexport interface Logger<\n\tTTree extends ReadableField<ImplicitFieldSchema> = ReadableField<ImplicitFieldSchema>,\n> {\n\t/**\n\t * Log a message.\n\t */\n\tlog(message: string): void;\n\t/**\n\t * Optional function to override the default tree stringification (JSON) when logging tree state.\n\t */\n\ttreeToString?(tree: TTree): string;\n}\n\n/**\n * Options used to parameterize the creation of a {@link SharedTreeSemanticAgent}.\n * @alpha\n */\nexport interface SemanticAgentOptions<TSchema extends ImplicitFieldSchema> {\n\tdomainHints?: string;\n\tvalidator?: (js: string) => boolean;\n\t/**\n\t * The maximum number of sequential edits the LLM can make before we assume it's stuck in a loop.\n\t */\n\tmaximumSequentialEdits?: number;\n\tlogger?: Logger<ReadableField<TSchema>>;\n}\n\n/**\n * A result from an edit attempt via the {@link SharedTreeChatQuery.edit} function.\n * @remarks\n * - `success`: The edit was successfully applied.\n * - `disabledError`: The model is not allowed to edit the tree (i.e. {@link SharedTreeChatModel.editFunctionName} was not provided).\n * - `validationError`: The provided JavaScript did not pass the optional {@link SemanticAgentOptions.validator} function.\n * - `compileError`: The provided JavaScript could not be parsed or compiled.\n * - `runtimeError`: An error was thrown while executing the provided JavaScript.\n * - `tooManyEditsError`: The {@link SharedTreeChatQuery.edit} function has been called more than the number of times specified by {@link SemanticAgentOptions.maximumSequentialEdits} for the same message.\n * - `expiredError`: The {@link SharedTreeChatQuery.edit} function was called after the issuing query has already completed.\n * @alpha\n */\nexport interface EditResult {\n\ttype:\n\t\t| \"success\"\n\t\t| \"disabledError\"\n\t\t| \"validationError\"\n\t\t| \"compileError\"\n\t\t| \"runtimeError\"\n\t\t| \"tooManyEditsError\"\n\t\t| \"expiredError\";\n\n\t/**\n\t * A human-readable message describing the result of the edit attempt.\n\t * @remarks In the case of an error, this message is appropriate to include in a model's chat history.\n\t */\n\tmessage: string;\n}\n\n/**\n * Type guard for {@link EditResult}.\n */\nexport function isEditResult(value: unknown): value is EditResult {\n\tif (value === null || typeof value !== \"object\") {\n\t\treturn false;\n\t}\n\treturn (\n\t\ttypeof (value as EditResult).type === \"string\" &&\n\t\ttypeof (value as EditResult).message === \"string\"\n\t);\n}\n\n/**\n * A query from a user to a {@link SharedTreeSemanticAgent}.\n * @remarks Processing a query may involve editing the SharedTree via the provided {@link SharedTreeChatQuery.edit} function.\n * @alpha\n */\nexport interface SharedTreeChatQuery {\n\t/**\n\t * The user's query.\n\t */\n\ttext: string;\n\t/**\n\t * Edit the tree with the provided JavaScript function code.\n\t * @remarks Attempting an edit may fail for a variety of reasons which are captured in the {@link EditResult | returned object}.\n\t */\n\tedit(js: string): Promise<EditResult>;\n}\n\n/**\n * A plugin interface that handles queries from a {@link SharedTreeSemanticAgent}.\n * @alpha\n */\nexport interface SharedTreeChatModel {\n\t/**\n\t * A optional name of this chat model.\n\t * @remarks If supplied, this may be used in logging or debugging information.\n\t * @example \"gpt-5\"\n\t */\n\tname?: string;\n\t/**\n\t * The name of the tool that the model should use to edit the tree, if any.\n\t * @remarks If supplied, this will be mentioned in the context provided to the model and the model will be encouraged to use it when a user query requires an edit.\n\t */\n\teditToolName?: string;\n\t/**\n\t * The name of the function that the model should generate to edit the tree.\n\t * @remarks If not supplied, the model will not be allowed to edit the tree.\n\t */\n\teditFunctionName?: string;\n\t/**\n\t * Add contextual information to the model that may be relevant to future queries.\n\t * @remarks In practice, this may be implemented by e.g. appending a \"system\" message to an LLM's chat/message history.\n\t * @param text - The message or context to append.\n\t */\n\tappendContext?(text: string): void;\n\t/**\n\t * Queries the chat model with a request from the user.\n\t * @remarks This model may simply return a text response to the query, or it may first call the {@link SharedTreeChatQuery.edit} function (potentially multiple times) to modify the tree in response to the query.\n\t */\n\tquery(message: SharedTreeChatQuery): Promise<string>;\n}\n\n/**\n * A function that edits a SharedTree.\n */\nexport type EditFunction<TSchema extends ImplicitFieldSchema> = ({\n\troot,\n\tcreate,\n}: {\n\troot: ReadableField<TSchema>;\n\tcreate: Record<string, (input: FactoryContentObject) => TreeNode>;\n}) => void | Promise<void>;\n"]}
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAgFH;;GAEG;AACH,SAAgB,YAAY,CAAC,KAAc;IAC1C,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACjD,OAAO,KAAK,CAAC;IACd,CAAC;IACD,OAAO,CACN,OAAQ,KAAoB,CAAC,IAAI,KAAK,QAAQ;QAC9C,OAAQ,KAAoB,CAAC,OAAO,KAAK,QAAQ,CACjD,CAAC;AACH,CAAC;AARD,oCAQC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type { ImplicitFieldSchema, TreeNode } from \"@fluidframework/tree\";\n// This is used for doc links\nimport type { FactoryContentObject, ReadableField } from \"@fluidframework/tree/alpha\";\n\n/**\n * Logger interface for logging events from a {@link SharedTreeSemanticAgent}.\n * @alpha\n */\nexport interface Logger {\n\t/**\n\t * Log a message.\n\t */\n\tlog(message: string): void;\n}\n\n/**\n * Options used to parameterize the creation of a {@link SharedTreeSemanticAgent}.\n * @alpha\n */\nexport interface SemanticAgentOptions {\n\t/**\n\t * Additional information about the application domain that will be included in the context provided to the {@link SharedTreeChatModel | model}.\n\t */\n\tdomainHints?: string;\n\t/**\n\t * Validates any generated JavaScript created by the {@link SharedTreeChatModel.editToolName | model's editing tool}.\n\t * @remarks This happens before the code is executed - execution can be intercepted by using the {@link SemanticAgentOptions.executeEdit | executeEdit} callback.\n\t * @param code - The generated JavaScript code as a string.\n\t * @throws If the code is invalid, this function should throw an error with a human-readable message describing why it is invalid.\n\t */\n\tvalidateEdit?: (code: string) => void | Promise<void>;\n\t/**\n\t * Evaluates (executes) any generated JavaScript created by the {@link SharedTreeChatModel.editToolName | model's editing tool}.\n\t * @remarks This happens only after the code has been successfully validated by the optional {@link SemanticAgentOptions.validateEdit | validateEdit} function.\n\t * @param context - An object that must be provided to the generated code as a variable named \"context\" in its top-level scope.\n\t * @param code - The generated JavaScript code as a string.\n\t * @throws If an error is thrown while evaluating the code, it will be caught and the message will be forwarded to the model for debugging.\n\t * @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.\n\t * Use a library such as SES to provide a more secure implementation - see {@link createSesEditEvaluator} for a drop-in option.\n\t */\n\texecuteEdit?: (context: Record<string, unknown>, code: string) => void | Promise<void>;\n\t/**\n\t * The maximum number of sequential edits the LLM can make before we assume it's stuck in a loop.\n\t */\n\tmaximumSequentialEdits?: number;\n\t/**\n\t * If supplied, generates human-readable markdown text describing the actions taken by the {@link SharedTreeSemanticAgent | agent} as it performs queries.\n\t */\n\tlogger?: Logger;\n}\n\n/**\n * A result from an edit attempt via the {@link SharedTreeChatQuery.edit} function.\n * @remarks\n * - `success`: The edit was successfully applied.\n * - `disabledError`: The model is not allowed to edit the tree (i.e. {@link SharedTreeChatModel.editToolName} was not provided).\n * - `validationError`: The provided JavaScript did not pass the optional {@link SemanticAgentOptions.validateEdit} function.\n * - `executionError`: An error was thrown while parsing or executing the provided JavaScript.\n * - `tooManyEditsError`: The {@link SharedTreeChatQuery.edit} function has been called more than the number of times specified by {@link SemanticAgentOptions.maximumSequentialEdits} for the same message.\n * - `expiredError`: The {@link SharedTreeChatQuery.edit} function was called after the issuing query has already completed.\n * @alpha\n */\nexport interface EditResult {\n\ttype:\n\t\t| \"success\"\n\t\t| \"disabledError\"\n\t\t| \"validationError\"\n\t\t| \"executionError\"\n\t\t| \"tooManyEditsError\"\n\t\t| \"expiredError\";\n\n\t/**\n\t * A human-readable message describing the result of the edit attempt.\n\t * @remarks In the case of an error, this message is appropriate to include in a model's chat history.\n\t */\n\tmessage: string;\n}\n\n/**\n * Type guard for {@link EditResult}.\n */\nexport function isEditResult(value: unknown): value is EditResult {\n\tif (value === null || typeof value !== \"object\") {\n\t\treturn false;\n\t}\n\treturn (\n\t\ttypeof (value as EditResult).type === \"string\" &&\n\t\ttypeof (value as EditResult).message === \"string\"\n\t);\n}\n\n/**\n * A query from a user to a {@link SharedTreeSemanticAgent}.\n * @remarks Processing a query may involve editing the SharedTree via the provided {@link SharedTreeChatQuery.edit} function.\n * @alpha\n */\nexport interface SharedTreeChatQuery {\n\t/**\n\t * The user's query.\n\t */\n\ttext: string;\n\t/**\n\t * Edit the tree with the provided JavaScript function code.\n\t * @remarks Attempting an edit may fail for a variety of reasons which are captured in the {@link EditResult | returned object}.\n\t */\n\tedit(js: string): Promise<EditResult>;\n}\n\n/**\n * A plugin interface that handles queries from a {@link SharedTreeSemanticAgent}.\n * @remarks This wraps an underlying communication with an LLM and receives all necessary {@link SharedTreeChatModel.appendContext | context} from the {@link SharedTreeSemanticAgent | agent} for the LLM to properly analyze and edit the tree.\n * @alpha\n */\nexport interface SharedTreeChatModel {\n\t/**\n\t * A optional name of this chat model.\n\t * @remarks If supplied, this may be used in logging or debugging information.\n\t * @example \"gpt-5\"\n\t */\n\tname?: string;\n\t/**\n\t * The name of the tool that the model should use to edit the tree.\n\t * @remarks If supplied, this will be mentioned in the context provided to the model so that the underlying LLM will be encouraged to use it when a user query requires an edit.\n\t * The model should \"implement\" the tool by registering it with the underlying LLM API.\n\t * The tool should take an LLM-generated JavaScript function as input and supply it to the {@link SharedTreeChatQuery.edit | edit} function.\n\t * Instructions for generating the proper function signature and implementation will be provided by the {@link SharedTreeSemanticAgent | agent} via {@link SharedTreeChatModel.appendContext | context}.\n\t * If not supplied, the model will not be able to edit the tree (running the {@link SharedTreeChatQuery.edit | edit} function will fail).\n\t */\n\teditToolName?: string;\n\t/**\n\t * Add contextual information to the model that may be relevant to future queries.\n\t * @remarks In practice, this may be implemented by e.g. appending a \"system\" message to an LLM's chat/message history.\n\t * This context must be present in the context window of every {@link SharedTreeChatModel.query | query} for e.g. {@link SharedTreeChatModel.editToolName | editing} to work.\n\t * @param text - The message or context to append.\n\t */\n\tappendContext?(text: string): void;\n\t/**\n\t * Queries the chat model with a request from the user.\n\t * @remarks This model may simply return a text response to the query, or it may first call the {@link SharedTreeChatQuery.edit} function (potentially multiple times) to modify the tree in response to the query.\n\t */\n\tquery(message: SharedTreeChatQuery): Promise<string>;\n}\n\n/**\n * A function that edits a SharedTree.\n */\nexport type EditFunction<TSchema extends ImplicitFieldSchema> = ({\n\troot,\n\tcreate,\n}: {\n\troot: ReadableField<TSchema>;\n\tcreate: Record<string, (input: FactoryContentObject) => TreeNode>;\n}) => void | Promise<void>;\n"]}
package/dist/index.d.ts CHANGED
@@ -7,9 +7,10 @@
7
7
  *
8
8
  * @packageDocumentation
9
9
  */
10
- export type { SharedTreeSemanticAgent } from "./agent.js";
10
+ export { SharedTreeSemanticAgent } from "./agent.js";
11
11
  export type { EditResult, SharedTreeChatModel, SharedTreeChatQuery, Logger, SemanticAgentOptions, } from "./api.js";
12
12
  export { type TreeView, llmDefault } from "./utils.js";
13
13
  export { buildFunc, exposeMethodsSymbol, type ArgsTuple, type ExposedMethods, type Arg, type FunctionDef, type MethodKeys, type BindableSchema, type Ctor, type Infer, type IExposedMethods, } from "./methodBinding.js";
14
14
  export { LangchainChatModel, createSemanticAgent } from "./langchain.js";
15
+ export { createSesEditEvaluator } from "./ses.js";
15
16
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AAEH,YAAY,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AAC1D,YAAY,EACX,UAAU,EACV,mBAAmB,EACnB,mBAAmB,EACnB,MAAM,EACN,oBAAoB,GACpB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,KAAK,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACvD,OAAO,EACN,SAAS,EACT,mBAAmB,EACnB,KAAK,SAAS,EACd,KAAK,cAAc,EACnB,KAAK,GAAG,EACR,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,IAAI,EACT,KAAK,KAAK,EACV,KAAK,eAAe,GACpB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AAEH,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AACrD,YAAY,EACX,UAAU,EACV,mBAAmB,EACnB,mBAAmB,EACnB,MAAM,EACN,oBAAoB,GACpB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,KAAK,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACvD,OAAO,EACN,SAAS,EACT,mBAAmB,EACnB,KAAK,SAAS,EACd,KAAK,cAAc,EACnB,KAAK,GAAG,EACR,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,IAAI,EACT,KAAK,KAAK,EACV,KAAK,eAAe,GACpB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACzE,OAAO,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC"}
package/dist/index.js CHANGED
@@ -4,7 +4,14 @@
4
4
  * Licensed under the MIT License.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.createSemanticAgent = exports.LangchainChatModel = exports.exposeMethodsSymbol = exports.buildFunc = exports.llmDefault = void 0;
7
+ exports.createSesEditEvaluator = exports.createSemanticAgent = exports.LangchainChatModel = exports.exposeMethodsSymbol = exports.buildFunc = exports.llmDefault = exports.SharedTreeSemanticAgent = void 0;
8
+ /**
9
+ * A library for creating AI agents to interact with a {@link SharedTree | https://fluidframework.com/docs/data-structures/tree/}.
10
+ *
11
+ * @packageDocumentation
12
+ */
13
+ var agent_js_1 = require("./agent.js");
14
+ Object.defineProperty(exports, "SharedTreeSemanticAgent", { enumerable: true, get: function () { return agent_js_1.SharedTreeSemanticAgent; } });
8
15
  var utils_js_1 = require("./utils.js");
9
16
  Object.defineProperty(exports, "llmDefault", { enumerable: true, get: function () { return utils_js_1.llmDefault; } });
10
17
  var methodBinding_js_1 = require("./methodBinding.js");
@@ -13,4 +20,6 @@ Object.defineProperty(exports, "exposeMethodsSymbol", { enumerable: true, get: f
13
20
  var langchain_js_1 = require("./langchain.js");
14
21
  Object.defineProperty(exports, "LangchainChatModel", { enumerable: true, get: function () { return langchain_js_1.LangchainChatModel; } });
15
22
  Object.defineProperty(exports, "createSemanticAgent", { enumerable: true, get: function () { return langchain_js_1.createSemanticAgent; } });
23
+ var ses_js_1 = require("./ses.js");
24
+ Object.defineProperty(exports, "createSesEditEvaluator", { enumerable: true, get: function () { return ses_js_1.createSesEditEvaluator; } });
16
25
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAgBH,uCAAuD;AAA/B,sGAAA,UAAU,OAAA;AAClC,uDAY4B;AAX3B,6GAAA,SAAS,OAAA;AACT,uHAAA,mBAAmB,OAAA;AAWpB,+CAAyE;AAAhE,kHAAA,kBAAkB,OAAA;AAAE,mHAAA,mBAAmB,OAAA","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * A library for creating AI agents to interact with a {@link SharedTree | https://fluidframework.com/docs/data-structures/tree/}.\n *\n * @packageDocumentation\n */\n\nexport type { SharedTreeSemanticAgent } from \"./agent.js\";\nexport type {\n\tEditResult,\n\tSharedTreeChatModel,\n\tSharedTreeChatQuery,\n\tLogger,\n\tSemanticAgentOptions,\n} from \"./api.js\";\nexport { type TreeView, llmDefault } from \"./utils.js\";\nexport {\n\tbuildFunc,\n\texposeMethodsSymbol,\n\ttype ArgsTuple,\n\ttype ExposedMethods,\n\ttype Arg,\n\ttype FunctionDef,\n\ttype MethodKeys,\n\ttype BindableSchema,\n\ttype Ctor,\n\ttype Infer,\n\ttype IExposedMethods,\n} from \"./methodBinding.js\";\nexport { LangchainChatModel, createSemanticAgent } from \"./langchain.js\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH;;;;GAIG;AAEH,uCAAqD;AAA5C,mHAAA,uBAAuB,OAAA;AAQhC,uCAAuD;AAA/B,sGAAA,UAAU,OAAA;AAClC,uDAY4B;AAX3B,6GAAA,SAAS,OAAA;AACT,uHAAA,mBAAmB,OAAA;AAWpB,+CAAyE;AAAhE,kHAAA,kBAAkB,OAAA;AAAE,mHAAA,mBAAmB,OAAA;AAChD,mCAAkD;AAAzC,gHAAA,sBAAsB,OAAA","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * A library for creating AI agents to interact with a {@link SharedTree | https://fluidframework.com/docs/data-structures/tree/}.\n *\n * @packageDocumentation\n */\n\nexport { SharedTreeSemanticAgent } from \"./agent.js\";\nexport type {\n\tEditResult,\n\tSharedTreeChatModel,\n\tSharedTreeChatQuery,\n\tLogger,\n\tSemanticAgentOptions,\n} from \"./api.js\";\nexport { type TreeView, llmDefault } from \"./utils.js\";\nexport {\n\tbuildFunc,\n\texposeMethodsSymbol,\n\ttype ArgsTuple,\n\ttype ExposedMethods,\n\ttype Arg,\n\ttype FunctionDef,\n\ttype MethodKeys,\n\ttype BindableSchema,\n\ttype Ctor,\n\ttype Infer,\n\ttype IExposedMethods,\n} from \"./methodBinding.js\";\nexport { LangchainChatModel, createSemanticAgent } from \"./langchain.js\";\nexport { createSesEditEvaluator } from \"./ses.js\";\n"]}
@@ -17,7 +17,6 @@ export declare class LangchainChatModel implements SharedTreeChatModel {
17
17
  private readonly messages;
18
18
  constructor(model: BaseChatModel);
19
19
  readonly editToolName = "GenerateTreeEditingCode";
20
- readonly editFunctionName = "editTree";
21
20
  get name(): string | undefined;
22
21
  appendContext(text: string): void;
23
22
  query(query: SharedTreeChatQuery): Promise<string>;
@@ -28,17 +27,17 @@ export declare class LangchainChatModel implements SharedTreeChatModel {
28
27
  * @alpha
29
28
  * @deprecated Use {@link SharedTreeSemanticAgent} with a {@link LangchainChatModel} instead.
30
29
  */
31
- export declare function createSemanticAgent<TSchema extends ImplicitFieldSchema>(client: BaseChatModel, treeView: TreeView<TSchema>, options?: Readonly<SemanticAgentOptions<TSchema>>): SharedTreeSemanticAgent<TSchema>;
30
+ export declare function createSemanticAgent<TSchema extends ImplicitFieldSchema>(client: BaseChatModel, treeView: TreeView<TSchema>, options?: Readonly<SemanticAgentOptions>): SharedTreeSemanticAgent<TSchema>;
32
31
  /**
33
32
  * Create a {@link SharedTreeSemanticAgent} using a Langchain chat model.
34
33
  * @alpha
35
34
  * @deprecated Use {@link SharedTreeSemanticAgent} with a {@link LangchainChatModel} instead.
36
35
  */
37
- export declare function createSemanticAgent<TSchema extends ImplicitFieldSchema>(client: BaseChatModel, node: ReadableField<TSchema> & TreeNode, options?: Readonly<SemanticAgentOptions<TSchema>>): SharedTreeSemanticAgent<TSchema>;
36
+ export declare function createSemanticAgent<TSchema extends ImplicitFieldSchema>(client: BaseChatModel, node: ReadableField<TSchema> & TreeNode, options?: Readonly<SemanticAgentOptions>): SharedTreeSemanticAgent<TSchema>;
38
37
  /**
39
38
  * Create a {@link SharedTreeSemanticAgent} using a Langchain chat model.
40
39
  * @alpha
41
40
  * @deprecated Use {@link SharedTreeSemanticAgent} with a {@link LangchainChatModel} instead.
42
41
  */
43
- export declare function createSemanticAgent<TSchema extends ImplicitFieldSchema>(client: BaseChatModel, treeView: TreeView<TSchema> | (ReadableField<TSchema> & TreeNode), options?: Readonly<SemanticAgentOptions<TSchema>>): SharedTreeSemanticAgent<TSchema>;
42
+ export declare function createSemanticAgent<TSchema extends ImplicitFieldSchema>(client: BaseChatModel, treeView: TreeView<TSchema> | (ReadableField<TSchema> & TreeNode), options?: Readonly<SemanticAgentOptions>): SharedTreeSemanticAgent<TSchema>;
44
43
  //# sourceMappingURL=langchain.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"langchain.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