@fluidframework/tree-agent-langchain 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.
Files changed (47) hide show
  1. package/.eslintrc.cjs +56 -0
  2. package/.mocharc.cjs +12 -0
  3. package/.vscode/extensions.json +3 -0
  4. package/.vscode/settings.json +21 -0
  5. package/.vscode/tree-agent-langchain.code-workspace +22 -0
  6. package/CHANGELOG.md +5 -0
  7. package/LICENSE +21 -0
  8. package/README.md +25 -0
  9. package/alpha.d.ts +11 -0
  10. package/api-extractor/api-extractor-lint-alpha.cjs.json +5 -0
  11. package/api-extractor/api-extractor-lint-alpha.esm.json +5 -0
  12. package/api-extractor/api-extractor-lint-bundle.json +5 -0
  13. package/api-extractor/api-extractor-lint-index.cjs.json +5 -0
  14. package/api-extractor/api-extractor-lint-index.esm.json +5 -0
  15. package/api-extractor/api-extractor-lint-public.cjs.json +5 -0
  16. package/api-extractor/api-extractor-lint-public.esm.json +5 -0
  17. package/api-extractor-lint.json +4 -0
  18. package/api-extractor.json +4 -0
  19. package/biome.jsonc +4 -0
  20. package/dist/alpha.d.ts +21 -0
  21. package/dist/chatModel.d.ts +13 -0
  22. package/dist/chatModel.d.ts.map +1 -0
  23. package/dist/chatModel.js +89 -0
  24. package/dist/chatModel.js.map +1 -0
  25. package/dist/index.d.ts +11 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +15 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/package.json +4 -0
  30. package/dist/public.d.ts +16 -0
  31. package/internal.d.ts +11 -0
  32. package/lib/alpha.d.ts +21 -0
  33. package/lib/chatModel.d.ts +13 -0
  34. package/lib/chatModel.d.ts.map +1 -0
  35. package/lib/chatModel.js +82 -0
  36. package/lib/chatModel.js.map +1 -0
  37. package/lib/index.d.ts +11 -0
  38. package/lib/index.d.ts.map +1 -0
  39. package/lib/index.js +11 -0
  40. package/lib/index.js.map +1 -0
  41. package/lib/public.d.ts +16 -0
  42. package/package.json +164 -0
  43. package/src/chatModel.ts +105 -0
  44. package/src/index.ts +12 -0
  45. package/tsconfig.cjs.json +7 -0
  46. package/tsconfig.json +14 -0
  47. package/tsdoc.json +4 -0
package/.eslintrc.cjs ADDED
@@ -0,0 +1,56 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ module.exports = {
7
+ extends: [require.resolve("@fluidframework/eslint-config-fluid/strict"), "prettier"],
8
+ parserOptions: {
9
+ project: ["./tsconfig.json"],
10
+ },
11
+ rules: {
12
+ // Allow reaching into FluidFramework package paths that end with alpha, beta, legacy, or internal
13
+ "import/no-internal-modules": [
14
+ "error",
15
+ {
16
+ allow: [
17
+ "@fluidframework/*/alpha",
18
+ "@fluidframework/*/beta",
19
+ "@fluidframework/*/legacy",
20
+ "@fluidframework/*/internal",
21
+ ],
22
+ },
23
+ ],
24
+ },
25
+ overrides: [
26
+ {
27
+ files: ["src/test/**/*"],
28
+ parserOptions: {
29
+ project: ["./src/test/tsconfig.json"],
30
+ },
31
+ rules: {
32
+ // Test files can import from submodules for testing purposes
33
+ "import/no-internal-modules": [
34
+ "error",
35
+ {
36
+ allow: [
37
+ "*/index.js",
38
+ "@fluidframework/*/alpha",
39
+ "@fluidframework/*/beta",
40
+ "@fluidframework/*/legacy",
41
+ "@fluidframework/*/internal",
42
+ ],
43
+ },
44
+ ],
45
+ // Allow unresolved for intentionally reaching into alpha/internal of other packages during integration tests
46
+ "import/no-unresolved": "off",
47
+ "@typescript-eslint/no-unsafe-assignment": "off",
48
+ "@typescript-eslint/no-unsafe-call": "off",
49
+ "@typescript-eslint/no-unsafe-member-access": "off",
50
+ "@typescript-eslint/no-unsafe-return": "off",
51
+ "@typescript-eslint/no-unsafe-argument": "off",
52
+ "@typescript-eslint/strict-boolean-expressions": "off",
53
+ },
54
+ },
55
+ ],
56
+ };
package/.mocharc.cjs ADDED
@@ -0,0 +1,12 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const getFluidTestMochaConfig = require("@fluid-internal/mocha-test-setup/mocharc-common");
9
+
10
+ // Reuse shared Fluid test mocha config for this package
11
+ const config = getFluidTestMochaConfig(__dirname);
12
+ module.exports = config;
@@ -0,0 +1,3 @@
1
+ {
2
+ "recommendations": ["hbenl.vscode-mocha-test-adapter"],
3
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ "search.exclude": {
3
+ "**/node_modules": true,
4
+ "**/dist": true,
5
+ "**/lib": true,
6
+ },
7
+ "mochaExplorer.files": ["dist/test/**/*.*js"],
8
+ "mochaExplorer.require": [
9
+ "node_modules/@fluid-internal/mocha-test-setup",
10
+ "source-map-support/register",
11
+ ],
12
+ "mochaExplorer.configFile": ".mocharc.cjs",
13
+ "mochaExplorer.timeout": 999999,
14
+ "mochaExplorer.nodeArgv": ["--conditions", "allow-ff-test-exports"],
15
+ "cSpell.words": ["fluidframework"],
16
+ "editor.defaultFormatter": "biomejs.biome",
17
+ "editor.insertSpaces": false,
18
+ "[json]": { "editor.defaultFormatter": "biomejs.biome" },
19
+ "[javascript]": { "editor.defaultFormatter": "biomejs.biome" },
20
+ "[typescript]": { "editor.defaultFormatter": "biomejs.biome" },
21
+ }
@@ -0,0 +1,22 @@
1
+ {
2
+ "folders": [
3
+ {
4
+ "name": "FluidFramework",
5
+ "path": "../../../../"
6
+ },
7
+ {
8
+ "name": "@fluidframework/tree-agent-langchain",
9
+ "path": ".."
10
+ }
11
+ ],
12
+ "settings": {
13
+ "search.followSymlinks": false,
14
+ "typescript.preferences.autoImportFileExcludePatterns": [
15
+ "**/node_modules/**/@fluid*/*-previous",
16
+ "**/node_modules/**/@fluid*/*-previous/*"
17
+ ],
18
+ "typescript.preferences.importModuleSpecifier": "project-relative",
19
+ "typescript.preferences.preferTypeOnlyAutoImports": true,
20
+ "typescript.tsdk": "FluidFramework\\node_modules\\typescript\\lib"
21
+ }
22
+ }
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # @fluidframework/tree-agent-langchain
2
+
3
+ ## 2.63.0
4
+
5
+ Initial release.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) Microsoft Corporation and contributors. All rights reserved.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # @fluidframework/tree-agent-langchain
2
+
3
+ Utilities for connecting LangChain chat models to the Fluid Framework `@fluidframework/tree-agent` package.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @fluidframework/tree-agent-langchain
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { createLangchainChatModel } from "@fluidframework/tree-agent-langchain";
15
+ import { ChatOpenAI } from "@langchain/openai";
16
+
17
+ const chatModel = new ChatOpenAI({ model: "gpt-4.1" });
18
+ const sharedTreeChatModel = createLangchainChatModel(chatModel);
19
+ ```
20
+
21
+ The returned `SharedTreeChatModel` can be provided to `SharedTreeSemanticAgent` from `@fluidframework/tree-agent`.
22
+
23
+ ## Licensing
24
+
25
+ This project is licensed under the [MIT License](./LICENSE).
package/alpha.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /*
7
+ * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
8
+ * Generated by "flub generate entrypoints" in @fluid-tools/build-cli.
9
+ */
10
+
11
+ export * from "./lib/alpha.js";
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3
+ "extends": "<projectFolder>/../../../common/build/build-common/api-extractor-lint.entrypoint.json",
4
+ "mainEntryPointFilePath": "<projectFolder>/dist/alpha.d.ts"
5
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3
+ "extends": "<projectFolder>/../../../common/build/build-common/api-extractor-lint.entrypoint.json",
4
+ "mainEntryPointFilePath": "<projectFolder>/lib/alpha.d.ts"
5
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3
+ "extends": "<projectFolder>/../../../common/build/build-common/api-extractor-lint.json",
4
+ "mainEntryPointFilePath": "<projectFolder>/lib/index.d.ts"
5
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3
+ "extends": "<projectFolder>/../../../common/build/build-common/api-extractor-lint.entrypoint.json",
4
+ "mainEntryPointFilePath": "<projectFolder>/dist/index.d.ts"
5
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3
+ "extends": "<projectFolder>/../../../common/build/build-common/api-extractor-lint.entrypoint.json",
4
+ "mainEntryPointFilePath": "<projectFolder>/lib/index.d.ts"
5
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3
+ "extends": "<projectFolder>/../../../common/build/build-common/api-extractor-lint.entrypoint.json",
4
+ "mainEntryPointFilePath": "<projectFolder>/dist/public.d.ts"
5
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3
+ "extends": "<projectFolder>/../../../common/build/build-common/api-extractor-lint.entrypoint.json",
4
+ "mainEntryPointFilePath": "<projectFolder>/lib/public.d.ts"
5
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3
+ "extends": "../../../common/build/build-common/api-extractor-lint.json"
4
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3
+ "extends": "../../../common/build/build-common/api-extractor-model.esm.json"
4
+ }
package/biome.jsonc ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
3
+ "extends": ["../../../biome.jsonc"]
4
+ }
@@ -0,0 +1,21 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /*
7
+ * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
8
+ * Generated by "flub generate entrypoints" in @fluid-tools/build-cli.
9
+ */
10
+
11
+ /**
12
+ * LangChain integration helpers for the {@link @fluidframework/tree-agent#SharedTreeSemanticAgent | SharedTreeSemanticAgent}.
13
+ *
14
+ * @packageDocumentation
15
+ */
16
+
17
+ export {
18
+ // #region @alpha APIs
19
+ createLangchainChatModel
20
+ // #endregion
21
+ } from "./index.js";
@@ -0,0 +1,13 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import type { SharedTreeChatModel } from "@fluidframework/tree-agent/alpha";
6
+ import type { BaseChatModel } from "@langchain/core/language_models/chat_models";
7
+ /**
8
+ * Creates a `SharedTreeChatModel` that uses the LangChain library to connect to the underlying LLM.
9
+ * @param langchainModel - The LangChain chat model to use.
10
+ * @alpha
11
+ */
12
+ export declare function createLangchainChatModel(langchainModel: BaseChatModel): SharedTreeChatModel;
13
+ //# sourceMappingURL=chatModel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chatModel.d.ts","sourceRoot":"","sources":["../src/chatModel.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,KAAK,EAEX,mBAAmB,EAEnB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6CAA6C,CAAC;AAMjF;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,cAAc,EAAE,aAAa,GAAG,mBAAmB,CAE3F"}
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ var __importDefault = (this && this.__importDefault) || function (mod) {
7
+ return (mod && mod.__esModule) ? mod : { "default": mod };
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.createLangchainChatModel = void 0;
11
+ /* eslint-disable import/no-internal-modules */
12
+ const internal_1 = require("@fluidframework/telemetry-utils/internal");
13
+ const messages_1 = require("@langchain/core/messages");
14
+ const tools_1 = require("@langchain/core/tools");
15
+ const zod_1 = __importDefault(require("zod"));
16
+ /**
17
+ * Creates a `SharedTreeChatModel` that uses the LangChain library to connect to the underlying LLM.
18
+ * @param langchainModel - The LangChain chat model to use.
19
+ * @alpha
20
+ */
21
+ function createLangchainChatModel(langchainModel) {
22
+ return new LangchainChatModel(langchainModel);
23
+ }
24
+ exports.createLangchainChatModel = createLangchainChatModel;
25
+ class LangchainChatModel {
26
+ constructor(model) {
27
+ this.model = model;
28
+ this.messages = [];
29
+ this.editToolName = "GenerateTreeEditingCode";
30
+ }
31
+ get name() {
32
+ const name = this.model.metadata?.modelName;
33
+ return typeof name === "string" ? name : undefined;
34
+ }
35
+ appendContext(text) {
36
+ this.messages.push(new messages_1.SystemMessage(text));
37
+ }
38
+ async query(query) {
39
+ this.messages.push(new messages_1.HumanMessage(query.text));
40
+ return this.queryEdit(async (js) => query.edit(js));
41
+ }
42
+ async queryEdit(edit) {
43
+ const editingTool = (0, tools_1.tool)(async ({ functionCode }) => {
44
+ return edit(functionCode);
45
+ }, {
46
+ name: this.editToolName,
47
+ description: "Invokes a JavaScript code snippet to edit a tree of application data.",
48
+ schema: zod_1.default.object({
49
+ functionCode: zod_1.default.string().describe("The JavaScript snippet code."),
50
+ }),
51
+ });
52
+ const runnable = this.model.bindTools?.([editingTool], { tool_choice: "auto" });
53
+ if (runnable === undefined) {
54
+ throw new internal_1.UsageError("LLM client must support function calling or tool use.");
55
+ }
56
+ const responseMessage = await runnable.invoke(this.messages);
57
+ this.messages.push(responseMessage);
58
+ if (responseMessage.tool_calls !== undefined && responseMessage.tool_calls.length > 0) {
59
+ for (const toolCall of responseMessage.tool_calls) {
60
+ switch (toolCall.name) {
61
+ case editingTool.name: {
62
+ const toolResult = await editingTool.invoke(toolCall);
63
+ this.messages.push(toolResult);
64
+ const editResult = JSON.parse(toolResult.text);
65
+ if (isEditResult(editResult) && editResult.type === "tooManyEditsError") {
66
+ return editResult.message;
67
+ }
68
+ return this.queryEdit(edit);
69
+ }
70
+ default: {
71
+ this.messages.push(new messages_1.HumanMessage(`Unrecognized tool call: ${toolCall.name}`));
72
+ }
73
+ }
74
+ }
75
+ }
76
+ return responseMessage.text;
77
+ }
78
+ }
79
+ /**
80
+ * Type guard for {@link EditResult}.
81
+ */
82
+ function isEditResult(value) {
83
+ if (value === null || typeof value !== "object") {
84
+ return false;
85
+ }
86
+ return (typeof value.type === "string" &&
87
+ typeof value.message === "string");
88
+ }
89
+ //# sourceMappingURL=chatModel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chatModel.js","sourceRoot":"","sources":["../src/chatModel.ts"],"names":[],"mappings":"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","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable import/no-internal-modules */\n\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport type {\n\tEditResult,\n\tSharedTreeChatModel,\n\tSharedTreeChatQuery,\n} from \"@fluidframework/tree-agent/alpha\";\nimport type { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport { HumanMessage, SystemMessage } from \"@langchain/core/messages\";\nimport { tool } from \"@langchain/core/tools\";\nimport z from \"zod\";\n\n/**\n * Creates a `SharedTreeChatModel` that uses the LangChain library to connect to the underlying LLM.\n * @param langchainModel - The LangChain chat model to use.\n * @alpha\n */\nexport function createLangchainChatModel(langchainModel: BaseChatModel): SharedTreeChatModel {\n\treturn new LangchainChatModel(langchainModel);\n}\n\nclass LangchainChatModel implements SharedTreeChatModel {\n\tprivate readonly messages: BaseMessage[] = [];\n\n\tpublic constructor(private readonly model: BaseChatModel) {}\n\n\tpublic readonly editToolName = \"GenerateTreeEditingCode\";\n\n\tpublic get name(): string | undefined {\n\t\tconst name = this.model.metadata?.modelName;\n\t\treturn typeof name === \"string\" ? name : undefined;\n\t}\n\n\tpublic appendContext(text: string): void {\n\t\tthis.messages.push(new SystemMessage(text));\n\t}\n\n\tpublic async query(query: SharedTreeChatQuery): Promise<string> {\n\t\tthis.messages.push(new HumanMessage(query.text));\n\t\treturn this.queryEdit(async (js: string) => query.edit(js));\n\t}\n\n\tprivate async queryEdit(edit: SharedTreeChatQuery[\"edit\"]): Promise<string> {\n\t\tconst editingTool = tool(\n\t\t\tasync ({ functionCode }: { functionCode: string }) => {\n\t\t\t\treturn edit(functionCode);\n\t\t\t},\n\t\t\t{\n\t\t\t\tname: this.editToolName,\n\t\t\t\tdescription: \"Invokes a JavaScript code snippet to edit a tree of application data.\",\n\t\t\t\tschema: z.object({\n\t\t\t\t\tfunctionCode: z.string().describe(\"The JavaScript snippet code.\"),\n\t\t\t\t}),\n\t\t\t},\n\t\t);\n\t\tconst runnable = this.model.bindTools?.([editingTool], { tool_choice: \"auto\" });\n\t\tif (runnable === undefined) {\n\t\t\tthrow new UsageError(\"LLM client must support function calling or tool use.\");\n\t\t}\n\n\t\tconst responseMessage = await runnable.invoke(this.messages);\n\t\tthis.messages.push(responseMessage);\n\n\t\tif (responseMessage.tool_calls !== undefined && responseMessage.tool_calls.length > 0) {\n\t\t\tfor (const toolCall of responseMessage.tool_calls) {\n\t\t\t\tswitch (toolCall.name) {\n\t\t\t\t\tcase editingTool.name: {\n\t\t\t\t\t\tconst toolResult = await editingTool.invoke(toolCall);\n\t\t\t\t\t\tthis.messages.push(toolResult);\n\t\t\t\t\t\tconst editResult: unknown = JSON.parse(toolResult.text);\n\t\t\t\t\t\tif (isEditResult(editResult) && editResult.type === \"tooManyEditsError\") {\n\t\t\t\t\t\t\treturn editResult.message;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn this.queryEdit(edit);\n\t\t\t\t\t}\n\t\t\t\t\tdefault: {\n\t\t\t\t\t\tthis.messages.push(new HumanMessage(`Unrecognized tool call: ${toolCall.name}`));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn responseMessage.text;\n\t}\n}\n\n/**\n * Type guard for {@link EditResult}.\n */\nfunction 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"]}
@@ -0,0 +1,11 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * LangChain integration helpers for the {@link @fluidframework/tree-agent#SharedTreeSemanticAgent | SharedTreeSemanticAgent}.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+ export { createLangchainChatModel } from "./chatModel.js";
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AAEH,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.createLangchainChatModel = void 0;
8
+ /**
9
+ * LangChain integration helpers for the {@link @fluidframework/tree-agent#SharedTreeSemanticAgent | SharedTreeSemanticAgent}.
10
+ *
11
+ * @packageDocumentation
12
+ */
13
+ var chatModel_js_1 = require("./chatModel.js");
14
+ Object.defineProperty(exports, "createLangchainChatModel", { enumerable: true, get: function () { return chatModel_js_1.createLangchainChatModel; } });
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH;;;;GAIG;AAEH,+CAA0D;AAAjD,wHAAA,wBAAwB,OAAA","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * LangChain integration helpers for the {@link @fluidframework/tree-agent#SharedTreeSemanticAgent | SharedTreeSemanticAgent}.\n *\n * @packageDocumentation\n */\n\nexport { createLangchainChatModel } from \"./chatModel.js\";\n"]}
@@ -0,0 +1,4 @@
1
+ {
2
+ "type": "commonjs",
3
+ "sideEffects": false
4
+ }
@@ -0,0 +1,16 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /*
7
+ * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
8
+ * Generated by "flub generate entrypoints" in @fluid-tools/build-cli.
9
+ */
10
+
11
+ /**
12
+ * LangChain integration helpers for the {@link @fluidframework/tree-agent#SharedTreeSemanticAgent | SharedTreeSemanticAgent}.
13
+ *
14
+ * @packageDocumentation
15
+ */export {}
16
+
package/internal.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /*
7
+ * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
8
+ * Generated by "flub generate entrypoints" in @fluid-tools/build-cli.
9
+ */
10
+
11
+ export * from "./lib/index.js";
package/lib/alpha.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /*
7
+ * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
8
+ * Generated by "flub generate entrypoints" in @fluid-tools/build-cli.
9
+ */
10
+
11
+ /**
12
+ * LangChain integration helpers for the {@link @fluidframework/tree-agent#SharedTreeSemanticAgent | SharedTreeSemanticAgent}.
13
+ *
14
+ * @packageDocumentation
15
+ */
16
+
17
+ export {
18
+ // #region @alpha APIs
19
+ createLangchainChatModel
20
+ // #endregion
21
+ } from "./index.js";
@@ -0,0 +1,13 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import type { SharedTreeChatModel } from "@fluidframework/tree-agent/alpha";
6
+ import type { BaseChatModel } from "@langchain/core/language_models/chat_models";
7
+ /**
8
+ * Creates a `SharedTreeChatModel` that uses the LangChain library to connect to the underlying LLM.
9
+ * @param langchainModel - The LangChain chat model to use.
10
+ * @alpha
11
+ */
12
+ export declare function createLangchainChatModel(langchainModel: BaseChatModel): SharedTreeChatModel;
13
+ //# sourceMappingURL=chatModel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chatModel.d.ts","sourceRoot":"","sources":["../src/chatModel.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,KAAK,EAEX,mBAAmB,EAEnB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6CAA6C,CAAC;AAMjF;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,cAAc,EAAE,aAAa,GAAG,mBAAmB,CAE3F"}
@@ -0,0 +1,82 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /* eslint-disable import/no-internal-modules */
6
+ import { UsageError } from "@fluidframework/telemetry-utils/internal";
7
+ import { HumanMessage, SystemMessage } from "@langchain/core/messages";
8
+ import { tool } from "@langchain/core/tools";
9
+ import z from "zod";
10
+ /**
11
+ * Creates a `SharedTreeChatModel` that uses the LangChain library to connect to the underlying LLM.
12
+ * @param langchainModel - The LangChain chat model to use.
13
+ * @alpha
14
+ */
15
+ export function createLangchainChatModel(langchainModel) {
16
+ return new LangchainChatModel(langchainModel);
17
+ }
18
+ class LangchainChatModel {
19
+ constructor(model) {
20
+ this.model = model;
21
+ this.messages = [];
22
+ this.editToolName = "GenerateTreeEditingCode";
23
+ }
24
+ get name() {
25
+ const name = this.model.metadata?.modelName;
26
+ return typeof name === "string" ? name : undefined;
27
+ }
28
+ appendContext(text) {
29
+ this.messages.push(new SystemMessage(text));
30
+ }
31
+ async query(query) {
32
+ this.messages.push(new HumanMessage(query.text));
33
+ return this.queryEdit(async (js) => query.edit(js));
34
+ }
35
+ async queryEdit(edit) {
36
+ const editingTool = tool(async ({ functionCode }) => {
37
+ return edit(functionCode);
38
+ }, {
39
+ name: this.editToolName,
40
+ description: "Invokes a JavaScript code snippet to edit a tree of application data.",
41
+ schema: z.object({
42
+ functionCode: z.string().describe("The JavaScript snippet code."),
43
+ }),
44
+ });
45
+ const runnable = this.model.bindTools?.([editingTool], { tool_choice: "auto" });
46
+ if (runnable === undefined) {
47
+ throw new UsageError("LLM client must support function calling or tool use.");
48
+ }
49
+ const responseMessage = await runnable.invoke(this.messages);
50
+ this.messages.push(responseMessage);
51
+ if (responseMessage.tool_calls !== undefined && responseMessage.tool_calls.length > 0) {
52
+ for (const toolCall of responseMessage.tool_calls) {
53
+ switch (toolCall.name) {
54
+ case editingTool.name: {
55
+ const toolResult = await editingTool.invoke(toolCall);
56
+ this.messages.push(toolResult);
57
+ const editResult = JSON.parse(toolResult.text);
58
+ if (isEditResult(editResult) && editResult.type === "tooManyEditsError") {
59
+ return editResult.message;
60
+ }
61
+ return this.queryEdit(edit);
62
+ }
63
+ default: {
64
+ this.messages.push(new HumanMessage(`Unrecognized tool call: ${toolCall.name}`));
65
+ }
66
+ }
67
+ }
68
+ }
69
+ return responseMessage.text;
70
+ }
71
+ }
72
+ /**
73
+ * Type guard for {@link EditResult}.
74
+ */
75
+ function isEditResult(value) {
76
+ if (value === null || typeof value !== "object") {
77
+ return false;
78
+ }
79
+ return (typeof value.type === "string" &&
80
+ typeof value.message === "string");
81
+ }
82
+ //# sourceMappingURL=chatModel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chatModel.js","sourceRoot":"","sources":["../src/chatModel.ts"],"names":[],"mappings":"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","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable import/no-internal-modules */\n\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport type {\n\tEditResult,\n\tSharedTreeChatModel,\n\tSharedTreeChatQuery,\n} from \"@fluidframework/tree-agent/alpha\";\nimport type { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport type { BaseMessage } from \"@langchain/core/messages\";\nimport { HumanMessage, SystemMessage } from \"@langchain/core/messages\";\nimport { tool } from \"@langchain/core/tools\";\nimport z from \"zod\";\n\n/**\n * Creates a `SharedTreeChatModel` that uses the LangChain library to connect to the underlying LLM.\n * @param langchainModel - The LangChain chat model to use.\n * @alpha\n */\nexport function createLangchainChatModel(langchainModel: BaseChatModel): SharedTreeChatModel {\n\treturn new LangchainChatModel(langchainModel);\n}\n\nclass LangchainChatModel implements SharedTreeChatModel {\n\tprivate readonly messages: BaseMessage[] = [];\n\n\tpublic constructor(private readonly model: BaseChatModel) {}\n\n\tpublic readonly editToolName = \"GenerateTreeEditingCode\";\n\n\tpublic get name(): string | undefined {\n\t\tconst name = this.model.metadata?.modelName;\n\t\treturn typeof name === \"string\" ? name : undefined;\n\t}\n\n\tpublic appendContext(text: string): void {\n\t\tthis.messages.push(new SystemMessage(text));\n\t}\n\n\tpublic async query(query: SharedTreeChatQuery): Promise<string> {\n\t\tthis.messages.push(new HumanMessage(query.text));\n\t\treturn this.queryEdit(async (js: string) => query.edit(js));\n\t}\n\n\tprivate async queryEdit(edit: SharedTreeChatQuery[\"edit\"]): Promise<string> {\n\t\tconst editingTool = tool(\n\t\t\tasync ({ functionCode }: { functionCode: string }) => {\n\t\t\t\treturn edit(functionCode);\n\t\t\t},\n\t\t\t{\n\t\t\t\tname: this.editToolName,\n\t\t\t\tdescription: \"Invokes a JavaScript code snippet to edit a tree of application data.\",\n\t\t\t\tschema: z.object({\n\t\t\t\t\tfunctionCode: z.string().describe(\"The JavaScript snippet code.\"),\n\t\t\t\t}),\n\t\t\t},\n\t\t);\n\t\tconst runnable = this.model.bindTools?.([editingTool], { tool_choice: \"auto\" });\n\t\tif (runnable === undefined) {\n\t\t\tthrow new UsageError(\"LLM client must support function calling or tool use.\");\n\t\t}\n\n\t\tconst responseMessage = await runnable.invoke(this.messages);\n\t\tthis.messages.push(responseMessage);\n\n\t\tif (responseMessage.tool_calls !== undefined && responseMessage.tool_calls.length > 0) {\n\t\t\tfor (const toolCall of responseMessage.tool_calls) {\n\t\t\t\tswitch (toolCall.name) {\n\t\t\t\t\tcase editingTool.name: {\n\t\t\t\t\t\tconst toolResult = await editingTool.invoke(toolCall);\n\t\t\t\t\t\tthis.messages.push(toolResult);\n\t\t\t\t\t\tconst editResult: unknown = JSON.parse(toolResult.text);\n\t\t\t\t\t\tif (isEditResult(editResult) && editResult.type === \"tooManyEditsError\") {\n\t\t\t\t\t\t\treturn editResult.message;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn this.queryEdit(edit);\n\t\t\t\t\t}\n\t\t\t\t\tdefault: {\n\t\t\t\t\t\tthis.messages.push(new HumanMessage(`Unrecognized tool call: ${toolCall.name}`));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn responseMessage.text;\n\t}\n}\n\n/**\n * Type guard for {@link EditResult}.\n */\nfunction 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"]}
package/lib/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * LangChain integration helpers for the {@link @fluidframework/tree-agent#SharedTreeSemanticAgent | SharedTreeSemanticAgent}.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+ export { createLangchainChatModel } from "./chatModel.js";
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AAEH,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,11 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * LangChain integration helpers for the {@link @fluidframework/tree-agent#SharedTreeSemanticAgent | SharedTreeSemanticAgent}.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+ export { createLangchainChatModel } from "./chatModel.js";
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AAEH,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * LangChain integration helpers for the {@link @fluidframework/tree-agent#SharedTreeSemanticAgent | SharedTreeSemanticAgent}.\n *\n * @packageDocumentation\n */\n\nexport { createLangchainChatModel } from \"./chatModel.js\";\n"]}
@@ -0,0 +1,16 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /*
7
+ * THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
8
+ * Generated by "flub generate entrypoints" in @fluid-tools/build-cli.
9
+ */
10
+
11
+ /**
12
+ * LangChain integration helpers for the {@link @fluidframework/tree-agent#SharedTreeSemanticAgent | SharedTreeSemanticAgent}.
13
+ *
14
+ * @packageDocumentation
15
+ */export {}
16
+
package/package.json ADDED
@@ -0,0 +1,164 @@
1
+ {
2
+ "name": "@fluidframework/tree-agent-langchain",
3
+ "version": "2.63.0-359962",
4
+ "description": "LangChain integration helpers for @fluidframework/tree-agent",
5
+ "homepage": "https://fluidframework.com",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/microsoft/FluidFramework.git",
9
+ "directory": "packages/framework/tree-agent-langchain"
10
+ },
11
+ "license": "MIT",
12
+ "author": "Microsoft and contributors",
13
+ "sideEffects": false,
14
+ "type": "module",
15
+ "exports": {
16
+ ".": {
17
+ "import": {
18
+ "types": "./lib/public.d.ts",
19
+ "default": "./lib/index.js"
20
+ },
21
+ "require": {
22
+ "types": "./dist/public.d.ts",
23
+ "default": "./dist/index.js"
24
+ }
25
+ },
26
+ "./alpha": {
27
+ "import": {
28
+ "types": "./lib/alpha.d.ts",
29
+ "default": "./lib/index.js"
30
+ },
31
+ "require": {
32
+ "types": "./dist/alpha.d.ts",
33
+ "default": "./dist/index.js"
34
+ }
35
+ },
36
+ "./internal": {
37
+ "import": {
38
+ "types": "./lib/index.d.ts",
39
+ "default": "./lib/index.js"
40
+ },
41
+ "require": {
42
+ "types": "./dist/index.d.ts",
43
+ "default": "./dist/index.js"
44
+ }
45
+ }
46
+ },
47
+ "main": "lib/index.js",
48
+ "types": "lib/public.d.ts",
49
+ "c8": {
50
+ "all": true,
51
+ "cache-dir": "nyc/.cache",
52
+ "exclude": [
53
+ "src/test/**/*.*ts",
54
+ "dist/test/**/*.*js",
55
+ "lib/test/**/*.*js"
56
+ ],
57
+ "exclude-after-remap": false,
58
+ "include": [
59
+ "src/**/*.*ts",
60
+ "dist/**/*.*js",
61
+ "lib/**/*.*js"
62
+ ],
63
+ "report-dir": "nyc/report",
64
+ "reporter": [
65
+ "cobertura",
66
+ "html",
67
+ "text"
68
+ ],
69
+ "temp-directory": "nyc/.nyc_output"
70
+ },
71
+ "dependencies": {
72
+ "@fluidframework/telemetry-utils": "2.63.0-359962",
73
+ "@fluidframework/tree-agent": "2.63.0-359962",
74
+ "@langchain/core": "^0.3.78",
75
+ "zod": "^3.25.32"
76
+ },
77
+ "devDependencies": {
78
+ "@arethetypeswrong/cli": "^0.17.1",
79
+ "@biomejs/biome": "~1.9.3",
80
+ "@fluid-internal/mocha-test-setup": "2.63.0-359962",
81
+ "@fluid-tools/build-cli": "^0.58.3",
82
+ "@fluidframework/build-common": "^2.0.3",
83
+ "@fluidframework/build-tools": "^0.58.3",
84
+ "@fluidframework/core-utils": "2.63.0-359962",
85
+ "@fluidframework/eslint-config-fluid": "^6.0.0",
86
+ "@fluidframework/runtime-utils": "2.63.0-359962",
87
+ "@fluidframework/tree": "2.63.0-359962",
88
+ "@langchain/anthropic": "^0.3.24",
89
+ "@langchain/google-genai": "^0.2.16",
90
+ "@langchain/openai": "^0.6.12",
91
+ "@microsoft/api-extractor": "7.52.11",
92
+ "@types/mocha": "^10.0.10",
93
+ "@types/node": "^18.19.0",
94
+ "c8": "^10.1.3",
95
+ "concurrently": "^8.2.1",
96
+ "copyfiles": "^2.4.1",
97
+ "cross-env": "^7.0.3",
98
+ "eslint": "~8.55.0",
99
+ "eslint-config-prettier": "~9.0.0",
100
+ "mocha": "^10.8.2",
101
+ "mocha-multi-reporters": "^1.5.1",
102
+ "prettier": "~3.0.3",
103
+ "rimraf": "^4.4.0",
104
+ "typescript": "~5.4.5"
105
+ },
106
+ "fluidBuild": {
107
+ "tasks": {
108
+ "build:esnext": [
109
+ "...",
110
+ "typetests:gen"
111
+ ],
112
+ "tsc": [
113
+ "...",
114
+ "typetests:gen"
115
+ ]
116
+ }
117
+ },
118
+ "typeValidation": {
119
+ "disabled": true,
120
+ "broken": {},
121
+ "entrypoint": "internal"
122
+ },
123
+ "scripts": {
124
+ "api": "fluid-build . --task api",
125
+ "api-extractor:commonjs": "flub generate entrypoints --outFileLegacyBeta legacy --outDir ./dist",
126
+ "api-extractor:esnext": "flub generate entrypoints --outFileLegacyBeta legacy --outDir ./lib --node10TypeCompat",
127
+ "build": "fluid-build . --task build",
128
+ "build:commonjs": "fluid-build . --task commonjs",
129
+ "build:compile": "fluid-build . --task compile",
130
+ "build:docs": "api-extractor run --local",
131
+ "build:esnext": "tsc --project ./tsconfig.json",
132
+ "build:test": "npm run build:test:esm && npm run build:test:cjs",
133
+ "build:test:cjs": "fluid-tsc commonjs --project ./src/test/tsconfig.cjs.json",
134
+ "build:test:esm": "tsc --project ./src/test/tsconfig.json",
135
+ "check:are-the-types-wrong": "attw --pack . --profile node16",
136
+ "check:biome": "biome check .",
137
+ "check:exports": "concurrently \"npm:check:exports:*\"",
138
+ "check:exports:bundle-release-tags": "api-extractor run --config api-extractor/api-extractor-lint-bundle.json",
139
+ "check:exports:cjs:alpha": "api-extractor run --config api-extractor/api-extractor-lint-alpha.cjs.json",
140
+ "check:exports:cjs:index": "api-extractor run --config api-extractor/api-extractor-lint-index.cjs.json",
141
+ "check:exports:cjs:public": "api-extractor run --config api-extractor/api-extractor-lint-public.cjs.json",
142
+ "check:exports:esm:alpha": "api-extractor run --config api-extractor/api-extractor-lint-alpha.esm.json",
143
+ "check:exports:esm:index": "api-extractor run --config api-extractor/api-extractor-lint-index.esm.json",
144
+ "check:exports:esm:public": "api-extractor run --config api-extractor/api-extractor-lint-public.esm.json",
145
+ "check:format": "npm run check:biome",
146
+ "ci:build:docs": "api-extractor run",
147
+ "clean": "rimraf --glob dist lib {alpha,beta,internal,legacy}.d.ts \"**/*.tsbuildinfo\" \"**/*.build.log\" _api-extractor-temp nyc",
148
+ "eslint": "eslint --format stylish src",
149
+ "eslint:fix": "eslint --format stylish src --fix --fix-type problem,suggestion,layout",
150
+ "format": "npm run format:biome",
151
+ "format:biome": "biome check . --write",
152
+ "lint": "fluid-build . --task lint",
153
+ "lint:fix": "fluid-build . --task eslint:fix --task format",
154
+ "test": "npm run test:mocha",
155
+ "test:coverage": "c8 npm test",
156
+ "test:mocha": "npm run test:mocha:esm && echo skipping cjs to avoid overhead - npm run test:mocha:cjs",
157
+ "test:mocha:cjs": "cross-env FLUID_TEST_MODULE_SYSTEM=CJS mocha",
158
+ "test:mocha:esm": "mocha",
159
+ "test:mocha:verbose": "cross-env FLUID_TEST_VERBOSE=1 npm run test:mocha",
160
+ "tsc": "fluid-tsc commonjs --project ./tsconfig.cjs.json && copyfiles -f ../../../common/build/build-common/src/cjs/package.json ./dist",
161
+ "typetests:gen": "flub generate typetests --dir . -v",
162
+ "typetests:prepare": "flub typetests --dir . --reset --previous --normalize"
163
+ }
164
+ }
@@ -0,0 +1,105 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /* eslint-disable import/no-internal-modules */
7
+
8
+ import { UsageError } from "@fluidframework/telemetry-utils/internal";
9
+ import type {
10
+ EditResult,
11
+ SharedTreeChatModel,
12
+ SharedTreeChatQuery,
13
+ } from "@fluidframework/tree-agent/alpha";
14
+ import type { BaseChatModel } from "@langchain/core/language_models/chat_models";
15
+ import type { BaseMessage } from "@langchain/core/messages";
16
+ import { HumanMessage, SystemMessage } from "@langchain/core/messages";
17
+ import { tool } from "@langchain/core/tools";
18
+ import z from "zod";
19
+
20
+ /**
21
+ * Creates a `SharedTreeChatModel` that uses the LangChain library to connect to the underlying LLM.
22
+ * @param langchainModel - The LangChain chat model to use.
23
+ * @alpha
24
+ */
25
+ export function createLangchainChatModel(langchainModel: BaseChatModel): SharedTreeChatModel {
26
+ return new LangchainChatModel(langchainModel);
27
+ }
28
+
29
+ class LangchainChatModel implements SharedTreeChatModel {
30
+ private readonly messages: BaseMessage[] = [];
31
+
32
+ public constructor(private readonly model: BaseChatModel) {}
33
+
34
+ public readonly editToolName = "GenerateTreeEditingCode";
35
+
36
+ public get name(): string | undefined {
37
+ const name = this.model.metadata?.modelName;
38
+ return typeof name === "string" ? name : undefined;
39
+ }
40
+
41
+ public appendContext(text: string): void {
42
+ this.messages.push(new SystemMessage(text));
43
+ }
44
+
45
+ public async query(query: SharedTreeChatQuery): Promise<string> {
46
+ this.messages.push(new HumanMessage(query.text));
47
+ return this.queryEdit(async (js: string) => query.edit(js));
48
+ }
49
+
50
+ private async queryEdit(edit: SharedTreeChatQuery["edit"]): Promise<string> {
51
+ const editingTool = tool(
52
+ async ({ functionCode }: { functionCode: string }) => {
53
+ return edit(functionCode);
54
+ },
55
+ {
56
+ name: this.editToolName,
57
+ description: "Invokes a JavaScript code snippet to edit a tree of application data.",
58
+ schema: z.object({
59
+ functionCode: z.string().describe("The JavaScript snippet code."),
60
+ }),
61
+ },
62
+ );
63
+ const runnable = this.model.bindTools?.([editingTool], { tool_choice: "auto" });
64
+ if (runnable === undefined) {
65
+ throw new UsageError("LLM client must support function calling or tool use.");
66
+ }
67
+
68
+ const responseMessage = await runnable.invoke(this.messages);
69
+ this.messages.push(responseMessage);
70
+
71
+ if (responseMessage.tool_calls !== undefined && responseMessage.tool_calls.length > 0) {
72
+ for (const toolCall of responseMessage.tool_calls) {
73
+ switch (toolCall.name) {
74
+ case editingTool.name: {
75
+ const toolResult = await editingTool.invoke(toolCall);
76
+ this.messages.push(toolResult);
77
+ const editResult: unknown = JSON.parse(toolResult.text);
78
+ if (isEditResult(editResult) && editResult.type === "tooManyEditsError") {
79
+ return editResult.message;
80
+ }
81
+ return this.queryEdit(edit);
82
+ }
83
+ default: {
84
+ this.messages.push(new HumanMessage(`Unrecognized tool call: ${toolCall.name}`));
85
+ }
86
+ }
87
+ }
88
+ }
89
+
90
+ return responseMessage.text;
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Type guard for {@link EditResult}.
96
+ */
97
+ function isEditResult(value: unknown): value is EditResult {
98
+ if (value === null || typeof value !== "object") {
99
+ return false;
100
+ }
101
+ return (
102
+ typeof (value as EditResult).type === "string" &&
103
+ typeof (value as EditResult).message === "string"
104
+ );
105
+ }
package/src/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * LangChain integration helpers for the {@link @fluidframework/tree-agent#SharedTreeSemanticAgent | SharedTreeSemanticAgent}.
8
+ *
9
+ * @packageDocumentation
10
+ */
11
+
12
+ export { createLangchainChatModel } from "./chatModel.js";
@@ -0,0 +1,7 @@
1
+ {
2
+ // This config must be used in a "type": "commonjs" environment. (Use `fluid-tsc commonjs`.)
3
+ "extends": "./tsconfig.json",
4
+ "compilerOptions": {
5
+ "outDir": "./dist",
6
+ },
7
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": "../../../common/build/build-common/tsconfig.node16.json",
3
+ "include": ["src/**/*"],
4
+ "exclude": ["src/test/**/*"],
5
+ "compilerOptions": {
6
+ "rootDir": "./src",
7
+ "outDir": "./lib",
8
+ "noImplicitAny": true,
9
+ "preserveConstEnums": true,
10
+ "exactOptionalPropertyTypes": false,
11
+ "lib": ["ES2020", "DOM", "DOM.Iterable"],
12
+ "skipLibCheck": true,
13
+ },
14
+ }
package/tsdoc.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
3
+ "extends": ["../../../common/build/build-common/tsdoc-base.json"]
4
+ }