@fluidframework/tree-agent 2.63.0-359461 → 2.63.0-359962
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 +3 -24
- package/dist/agent.d.ts +0 -3
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +21 -49
- package/dist/agent.js.map +1 -1
- package/dist/alpha.d.ts +0 -2
- package/dist/api.d.ts +19 -7
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -4
- package/dist/index.js.map +1 -1
- package/dist/prompt.d.ts.map +1 -1
- package/dist/prompt.js +15 -17
- package/dist/prompt.js.map +1 -1
- package/dist/utils.d.ts +4 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +16 -1
- package/dist/utils.js.map +1 -1
- package/lib/agent.d.ts +0 -3
- package/lib/agent.d.ts.map +1 -1
- package/lib/agent.js +22 -50
- package/lib/agent.js.map +1 -1
- package/lib/alpha.d.ts +0 -2
- package/lib/api.d.ts +19 -7
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js.map +1 -1
- package/lib/index.d.ts +0 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/lib/prompt.d.ts.map +1 -1
- package/lib/prompt.js +15 -17
- package/lib/prompt.js.map +1 -1
- package/lib/utils.d.ts +4 -0
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +14 -0
- package/lib/utils.js.map +1 -1
- package/package.json +11 -12
- package/src/agent.ts +29 -61
- package/src/api.ts +20 -12
- package/src/index.ts +0 -1
- package/src/prompt.ts +15 -17
- package/src/utils.ts +14 -0
- package/dist/functionParsing.d.ts +0 -13
- package/dist/functionParsing.d.ts.map +0 -1
- package/dist/functionParsing.js +0 -215
- package/dist/functionParsing.js.map +0 -1
- package/dist/langchain.d.ts +0 -43
- package/dist/langchain.d.ts.map +0 -1
- package/dist/langchain.js +0 -90
- package/dist/langchain.js.map +0 -1
- package/lib/functionParsing.d.ts +0 -13
- package/lib/functionParsing.d.ts.map +0 -1
- package/lib/functionParsing.js +0 -210
- package/lib/functionParsing.js.map +0 -1
- package/lib/langchain.d.ts +0 -43
- package/lib/langchain.d.ts.map +0 -1
- package/lib/langchain.js +0 -84
- package/lib/langchain.js.map +0 -1
- package/src/functionParsing.ts +0 -268
- package/src/langchain.ts +0 -146
package/src/functionParsing.ts
DELETED
|
@@ -1,268 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
-
* Licensed under the MIT License.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/* eslint-disable @rushstack/no-new-null */
|
|
7
|
-
|
|
8
|
-
import { parse } from "acorn";
|
|
9
|
-
import type {
|
|
10
|
-
Program,
|
|
11
|
-
Statement,
|
|
12
|
-
ModuleDeclaration,
|
|
13
|
-
VariableDeclaration,
|
|
14
|
-
Expression,
|
|
15
|
-
AssignmentExpression,
|
|
16
|
-
Pattern,
|
|
17
|
-
Identifier,
|
|
18
|
-
Literal,
|
|
19
|
-
ExportNamedDeclaration,
|
|
20
|
-
ExportDefaultDeclaration,
|
|
21
|
-
FunctionDeclaration,
|
|
22
|
-
FunctionExpression,
|
|
23
|
-
ArrowFunctionExpression,
|
|
24
|
-
} from "acorn";
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Finds the name of the first invocable function in the given code string.
|
|
28
|
-
*/
|
|
29
|
-
export function findInvocableFunctionName(code: string): string | undefined {
|
|
30
|
-
const program = parseProgram(code);
|
|
31
|
-
if (program === undefined) {
|
|
32
|
-
return undefined;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
for (const node of program.body) {
|
|
36
|
-
const name = getNameFromTopLevelNode(node);
|
|
37
|
-
if (name !== undefined) {
|
|
38
|
-
return name;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return undefined;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Removes top-level export syntax so that the provided code can execute in a classic script context.
|
|
47
|
-
*/
|
|
48
|
-
export function stripExportSyntax(code: string): string {
|
|
49
|
-
const program = parseProgram(code);
|
|
50
|
-
if (program === undefined) {
|
|
51
|
-
return code;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const replacements: { start: number; end: number; replacement: string }[] = [];
|
|
55
|
-
for (const node of program.body) {
|
|
56
|
-
switch (node.type) {
|
|
57
|
-
case "ExportNamedDeclaration": {
|
|
58
|
-
if (node.declaration !== undefined && node.declaration !== null) {
|
|
59
|
-
replacements.push({
|
|
60
|
-
start: node.start,
|
|
61
|
-
end: node.declaration.start,
|
|
62
|
-
replacement: "",
|
|
63
|
-
});
|
|
64
|
-
} else {
|
|
65
|
-
replacements.push({ start: node.start, end: node.end, replacement: "" });
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
break;
|
|
69
|
-
}
|
|
70
|
-
case "ExportDefaultDeclaration": {
|
|
71
|
-
const { declaration, start, end } = node;
|
|
72
|
-
if (
|
|
73
|
-
declaration.type === "FunctionDeclaration" ||
|
|
74
|
-
declaration.type === "FunctionExpression"
|
|
75
|
-
) {
|
|
76
|
-
replacements.push({
|
|
77
|
-
start,
|
|
78
|
-
end: declaration.start,
|
|
79
|
-
replacement: "",
|
|
80
|
-
});
|
|
81
|
-
} else {
|
|
82
|
-
replacements.push({ start, end, replacement: "" });
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
case "ExportAllDeclaration": {
|
|
88
|
-
replacements.push({ start: node.start, end: node.end, replacement: "" });
|
|
89
|
-
|
|
90
|
-
break;
|
|
91
|
-
}
|
|
92
|
-
// No default
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (replacements.length === 0) {
|
|
97
|
-
return code;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
replacements.sort((a, b) => b.start - a.start);
|
|
101
|
-
let sanitized = code;
|
|
102
|
-
for (const { start, end, replacement } of replacements) {
|
|
103
|
-
sanitized = `${sanitized.slice(0, start)}${replacement}${sanitized.slice(end)}`;
|
|
104
|
-
}
|
|
105
|
-
return sanitized;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
type TopLevelNode = Statement | ModuleDeclaration;
|
|
109
|
-
|
|
110
|
-
function parseProgram(code: string): Program | undefined {
|
|
111
|
-
try {
|
|
112
|
-
return parse(code, {
|
|
113
|
-
ecmaVersion: "latest",
|
|
114
|
-
sourceType: "module",
|
|
115
|
-
});
|
|
116
|
-
} catch {
|
|
117
|
-
try {
|
|
118
|
-
return parse(code, {
|
|
119
|
-
ecmaVersion: "latest",
|
|
120
|
-
sourceType: "script",
|
|
121
|
-
allowReturnOutsideFunction: true,
|
|
122
|
-
allowAwaitOutsideFunction: true,
|
|
123
|
-
allowSuperOutsideMethod: true,
|
|
124
|
-
});
|
|
125
|
-
} catch {
|
|
126
|
-
return undefined;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function getNameFromTopLevelNode(node: TopLevelNode): string | undefined {
|
|
132
|
-
switch (node.type) {
|
|
133
|
-
case "FunctionDeclaration": {
|
|
134
|
-
return getFunctionIdentifier(node);
|
|
135
|
-
}
|
|
136
|
-
case "VariableDeclaration": {
|
|
137
|
-
return getNameFromVariableDeclaration(node);
|
|
138
|
-
}
|
|
139
|
-
case "ExpressionStatement": {
|
|
140
|
-
return getNameFromExpression(node.expression);
|
|
141
|
-
}
|
|
142
|
-
case "ExportNamedDeclaration": {
|
|
143
|
-
return getNameFromExportNamed(node);
|
|
144
|
-
}
|
|
145
|
-
case "ExportDefaultDeclaration": {
|
|
146
|
-
return getNameFromExportDefault(node);
|
|
147
|
-
}
|
|
148
|
-
default: {
|
|
149
|
-
return undefined;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function getNameFromVariableDeclaration(node: VariableDeclaration): string | undefined {
|
|
155
|
-
for (const declarator of node.declarations) {
|
|
156
|
-
const name = getIdentifierFromPattern(declarator.id);
|
|
157
|
-
if (name === undefined) {
|
|
158
|
-
continue;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if (isFunctionLikeExpression(declarator.init)) {
|
|
162
|
-
return name;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return undefined;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function getNameFromExpression(
|
|
170
|
-
expression: Expression | Literal | undefined,
|
|
171
|
-
): string | undefined {
|
|
172
|
-
if (!isAssignmentExpression(expression)) {
|
|
173
|
-
return undefined;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (expression.operator !== "=") {
|
|
177
|
-
return undefined;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const target = getIdentifierFromPattern(expression.left);
|
|
181
|
-
if (target === undefined) {
|
|
182
|
-
return undefined;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return isFunctionLikeExpression(expression.right) ? target : undefined;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
function getNameFromExportNamed(node: ExportNamedDeclaration): string | undefined {
|
|
189
|
-
const declaration = node.declaration;
|
|
190
|
-
if (declaration !== undefined && declaration !== null) {
|
|
191
|
-
if (declaration.type === "FunctionDeclaration") {
|
|
192
|
-
const name = getFunctionIdentifier(declaration);
|
|
193
|
-
if (name !== undefined) {
|
|
194
|
-
return name;
|
|
195
|
-
}
|
|
196
|
-
} else if (declaration.type === "VariableDeclaration") {
|
|
197
|
-
const name = getNameFromVariableDeclaration(declaration);
|
|
198
|
-
if (name !== undefined) {
|
|
199
|
-
return name;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (node.source === undefined || node.source === null) {
|
|
205
|
-
for (const specifier of node.specifiers) {
|
|
206
|
-
const localName = getIdentifierName(specifier.local);
|
|
207
|
-
if (localName !== undefined) {
|
|
208
|
-
return localName;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return undefined;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
function getNameFromExportDefault(node: ExportDefaultDeclaration): string | undefined {
|
|
217
|
-
const declaration = node.declaration;
|
|
218
|
-
if (declaration.type === "Identifier") {
|
|
219
|
-
return declaration.name;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
if (
|
|
223
|
-
declaration.type === "FunctionDeclaration" ||
|
|
224
|
-
declaration.type === "FunctionExpression"
|
|
225
|
-
) {
|
|
226
|
-
return getFunctionIdentifier(declaration);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
return undefined;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
function isAssignmentExpression(
|
|
233
|
-
expression: Expression | Literal | undefined,
|
|
234
|
-
): expression is AssignmentExpression {
|
|
235
|
-
return expression?.type === "AssignmentExpression";
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function getIdentifierFromPattern(pattern: Pattern): string | undefined {
|
|
239
|
-
if (pattern.type === "Identifier") {
|
|
240
|
-
return pattern.name;
|
|
241
|
-
}
|
|
242
|
-
return undefined;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
function isFunctionLikeExpression(
|
|
246
|
-
expression: Expression | null | undefined,
|
|
247
|
-
): expression is FunctionExpression | ArrowFunctionExpression {
|
|
248
|
-
return (
|
|
249
|
-
expression?.type === "FunctionExpression" || expression?.type === "ArrowFunctionExpression"
|
|
250
|
-
);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function getFunctionIdentifier(
|
|
254
|
-
fn: FunctionDeclaration | FunctionExpression | { id?: Identifier | null },
|
|
255
|
-
): string | undefined {
|
|
256
|
-
const id = fn.id;
|
|
257
|
-
if (id === undefined || id === null) {
|
|
258
|
-
return undefined;
|
|
259
|
-
}
|
|
260
|
-
return id.name;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function getIdentifierName(node: Identifier | Literal): string | undefined {
|
|
264
|
-
if (node.type === "Identifier") {
|
|
265
|
-
return node.name;
|
|
266
|
-
}
|
|
267
|
-
return undefined;
|
|
268
|
-
}
|
package/src/langchain.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
-
* Licensed under the MIT License.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { UsageError } from "@fluidframework/telemetry-utils/internal";
|
|
7
|
-
import type { ImplicitFieldSchema, ReadableField, TreeNode } from "@fluidframework/tree/alpha";
|
|
8
|
-
// eslint-disable-next-line import/no-internal-modules
|
|
9
|
-
import type { BaseChatModel } from "@langchain/core/language_models/chat_models";
|
|
10
|
-
// eslint-disable-next-line import/no-internal-modules
|
|
11
|
-
import type { AIMessage, ToolMessage } from "@langchain/core/messages";
|
|
12
|
-
import {
|
|
13
|
-
HumanMessage,
|
|
14
|
-
SystemMessage,
|
|
15
|
-
// eslint-disable-next-line import/no-internal-modules
|
|
16
|
-
} from "@langchain/core/messages";
|
|
17
|
-
// eslint-disable-next-line import/no-internal-modules
|
|
18
|
-
import { tool } from "@langchain/core/tools";
|
|
19
|
-
import z from "zod";
|
|
20
|
-
|
|
21
|
-
import { SharedTreeSemanticAgent } from "./agent.js";
|
|
22
|
-
import {
|
|
23
|
-
isEditResult,
|
|
24
|
-
type SemanticAgentOptions,
|
|
25
|
-
type SharedTreeChatModel,
|
|
26
|
-
type SharedTreeChatQuery,
|
|
27
|
-
} from "./api.js";
|
|
28
|
-
import type { TreeView } from "./utils.js";
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* An implementation of {@link SharedTreeChatModel} that wraps a Langchain chat model.
|
|
32
|
-
* @remarks This class is responsible for managing the conversation history and interacting with the Langchain model (e.g. via Tool use for editing).
|
|
33
|
-
* @alpha
|
|
34
|
-
*/
|
|
35
|
-
export class LangchainChatModel implements SharedTreeChatModel {
|
|
36
|
-
private readonly messages: (HumanMessage | AIMessage | ToolMessage)[] = [];
|
|
37
|
-
public constructor(private readonly model: BaseChatModel) {}
|
|
38
|
-
|
|
39
|
-
public readonly editToolName = "GenerateTreeEditingCode";
|
|
40
|
-
|
|
41
|
-
public get name(): string | undefined {
|
|
42
|
-
const name = this.model.metadata?.modelName;
|
|
43
|
-
return typeof name === "string" ? name : undefined;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public appendContext(text: string): void {
|
|
47
|
-
this.messages.push(new SystemMessage(text));
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public async query(query: SharedTreeChatQuery): Promise<string> {
|
|
51
|
-
this.messages.push(new HumanMessage(query.text));
|
|
52
|
-
return this.queryEdit(async (js) => query.edit(js));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
private async queryEdit(edit: SharedTreeChatQuery["edit"]): Promise<string> {
|
|
56
|
-
const editingTool = tool(
|
|
57
|
-
async ({ functionCode }) => {
|
|
58
|
-
return edit(functionCode);
|
|
59
|
-
},
|
|
60
|
-
{
|
|
61
|
-
name: this.editToolName,
|
|
62
|
-
description: `Invokes a JavaScript function to edit a user's tree`,
|
|
63
|
-
schema: z.object({
|
|
64
|
-
functionCode: z.string().describe(`The code of the JavaScript function.
|
|
65
|
-
For example: "function editTree({ root, create }) { /* your code here */ }"`),
|
|
66
|
-
}),
|
|
67
|
-
},
|
|
68
|
-
);
|
|
69
|
-
const runnable = this.model.bindTools?.([editingTool], { tool_choice: "auto" });
|
|
70
|
-
if (runnable === undefined) {
|
|
71
|
-
throw new UsageError("LLM client must support function calling or tool use.");
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const responseMessage = await runnable.invoke(this.messages);
|
|
75
|
-
this.messages.push(responseMessage);
|
|
76
|
-
|
|
77
|
-
if (responseMessage.tool_calls !== undefined && responseMessage.tool_calls.length > 0) {
|
|
78
|
-
for (const toolCall of responseMessage.tool_calls) {
|
|
79
|
-
switch (toolCall.name) {
|
|
80
|
-
case editingTool.name: {
|
|
81
|
-
const toolResult = await editingTool.invoke(toolCall);
|
|
82
|
-
this.messages.push(toolResult);
|
|
83
|
-
const editResult: unknown = JSON.parse(toolResult.text);
|
|
84
|
-
if (isEditResult(editResult) && editResult.type === "tooManyEditsError") {
|
|
85
|
-
return editResult.message;
|
|
86
|
-
}
|
|
87
|
-
// This call will either terminate the edit chain (if the LLM decides not to edit further) or continue it if more edits are required.
|
|
88
|
-
return this.queryEdit(edit);
|
|
89
|
-
}
|
|
90
|
-
default: {
|
|
91
|
-
this.messages.push(new HumanMessage(`Unrecognized tool call: ${toolCall.name}`));
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return responseMessage.text;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// #region Legacy APIs
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Create a {@link SharedTreeSemanticAgent} using a Langchain chat model.
|
|
105
|
-
* @alpha
|
|
106
|
-
* @deprecated Use {@link SharedTreeSemanticAgent} with a {@link LangchainChatModel} instead.
|
|
107
|
-
*/
|
|
108
|
-
export function createSemanticAgent<TSchema extends ImplicitFieldSchema>(
|
|
109
|
-
client: BaseChatModel,
|
|
110
|
-
treeView: TreeView<TSchema>,
|
|
111
|
-
options?: Readonly<SemanticAgentOptions>,
|
|
112
|
-
): SharedTreeSemanticAgent<TSchema>;
|
|
113
|
-
/**
|
|
114
|
-
* Create a {@link SharedTreeSemanticAgent} using a Langchain chat model.
|
|
115
|
-
* @alpha
|
|
116
|
-
* @deprecated Use {@link SharedTreeSemanticAgent} with a {@link LangchainChatModel} instead.
|
|
117
|
-
*/
|
|
118
|
-
export function createSemanticAgent<TSchema extends ImplicitFieldSchema>(
|
|
119
|
-
client: BaseChatModel,
|
|
120
|
-
node: ReadableField<TSchema> & TreeNode,
|
|
121
|
-
options?: Readonly<SemanticAgentOptions>,
|
|
122
|
-
): SharedTreeSemanticAgent<TSchema>;
|
|
123
|
-
/**
|
|
124
|
-
* Create a {@link SharedTreeSemanticAgent} using a Langchain chat model.
|
|
125
|
-
* @alpha
|
|
126
|
-
* @deprecated Use {@link SharedTreeSemanticAgent} with a {@link LangchainChatModel} instead.
|
|
127
|
-
*/
|
|
128
|
-
export function createSemanticAgent<TSchema extends ImplicitFieldSchema>(
|
|
129
|
-
client: BaseChatModel,
|
|
130
|
-
treeView: TreeView<TSchema> | (ReadableField<TSchema> & TreeNode),
|
|
131
|
-
options?: Readonly<SemanticAgentOptions>,
|
|
132
|
-
): SharedTreeSemanticAgent<TSchema>;
|
|
133
|
-
/**
|
|
134
|
-
* Create a {@link SharedTreeSemanticAgent} using a Langchain chat model.
|
|
135
|
-
* @alpha
|
|
136
|
-
* @deprecated Use {@link SharedTreeSemanticAgent} with a {@link LangchainChatModel} instead.
|
|
137
|
-
*/
|
|
138
|
-
export function createSemanticAgent<TSchema extends ImplicitFieldSchema>(
|
|
139
|
-
client: BaseChatModel,
|
|
140
|
-
treeView: TreeView<TSchema> | (ReadableField<TSchema> & TreeNode),
|
|
141
|
-
options?: Readonly<SemanticAgentOptions>,
|
|
142
|
-
): SharedTreeSemanticAgent<TSchema> {
|
|
143
|
-
return new SharedTreeSemanticAgent(new LangchainChatModel(client), treeView, options);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// #endregion Legacy APIs
|