nestjs-langchain 1.0.2 → 2.0.0
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/LICENSE +21 -21
- package/README.md +344 -254
- package/dist/agent.d.ts +63 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +189 -0
- package/dist/agent.js.map +1 -0
- package/dist/constants.d.ts +5 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +14 -0
- package/dist/constants.js.map +1 -0
- package/dist/decorators/index.d.ts +4 -2
- package/dist/decorators/index.d.ts.map +1 -0
- package/dist/decorators/index.js +3 -18
- package/dist/decorators/index.js.map +1 -0
- package/dist/decorators/inject-agent.decorator.d.ts +12 -0
- package/dist/decorators/inject-agent.decorator.d.ts.map +1 -0
- package/dist/decorators/inject-agent.decorator.js +15 -7
- package/dist/decorators/inject-agent.decorator.js.map +1 -0
- package/dist/decorators/tool.decorator.d.ts +54 -6
- package/dist/decorators/tool.decorator.d.ts.map +1 -0
- package/dist/decorators/tool.decorator.js +40 -16
- package/dist/decorators/tool.decorator.js.map +1 -0
- package/dist/errors/agent-run.error.d.ts +27 -0
- package/dist/errors/agent-run.error.d.ts.map +1 -0
- package/dist/errors/agent-run.error.js +26 -0
- package/dist/errors/agent-run.error.js.map +1 -0
- package/dist/errors/index.d.ts +3 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +3 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/errors/messages.d.ts +26 -0
- package/dist/errors/messages.d.ts.map +1 -0
- package/dist/errors/messages.js +67 -0
- package/dist/errors/messages.js.map +1 -0
- package/dist/errors/tool-configuration.error.d.ts +23 -0
- package/dist/errors/tool-configuration.error.d.ts.map +1 -0
- package/dist/errors/tool-configuration.error.js +27 -0
- package/dist/errors/tool-configuration.error.js.map +1 -0
- package/dist/index.d.ts +10 -3
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -19
- package/dist/index.js.map +1 -0
- package/dist/interfaces/agent-observer.interface.d.ts +107 -0
- package/dist/interfaces/agent-observer.interface.d.ts.map +1 -0
- package/dist/interfaces/agent-observer.interface.js +2 -0
- package/dist/interfaces/agent-observer.interface.js.map +1 -0
- package/dist/interfaces/completed-run.interface.d.ts +14 -0
- package/dist/interfaces/completed-run.interface.d.ts.map +1 -0
- package/dist/interfaces/completed-run.interface.js +2 -0
- package/dist/interfaces/completed-run.interface.js.map +1 -0
- package/dist/interfaces/langchain-module-options.interface.d.ts +73 -5
- package/dist/interfaces/langchain-module-options.interface.d.ts.map +1 -0
- package/dist/interfaces/langchain-module-options.interface.js +2 -2
- package/dist/interfaces/langchain-module-options.interface.js.map +1 -0
- package/dist/langchain.module-definition.d.ts +26 -6
- package/dist/langchain.module-definition.d.ts.map +1 -0
- package/dist/langchain.module-definition.js +26 -13
- package/dist/langchain.module-definition.js.map +1 -0
- package/dist/langchain.module.d.ts +60 -3
- package/dist/langchain.module.d.ts.map +1 -0
- package/dist/langchain.module.js +87 -24
- package/dist/langchain.module.js.map +1 -0
- package/dist/logging/index.d.ts +3 -0
- package/dist/logging/index.d.ts.map +1 -0
- package/dist/logging/index.js +3 -0
- package/dist/logging/index.js.map +1 -0
- package/dist/logging/observe-tool-call.util.d.ts +13 -0
- package/dist/logging/observe-tool-call.util.d.ts.map +1 -0
- package/dist/logging/observe-tool-call.util.js +34 -0
- package/dist/logging/observe-tool-call.util.js.map +1 -0
- package/dist/logging/run-reporter.d.ts +26 -0
- package/dist/logging/run-reporter.d.ts.map +1 -0
- package/dist/logging/run-reporter.js +39 -0
- package/dist/logging/run-reporter.js.map +1 -0
- package/dist/run/index.d.ts +3 -0
- package/dist/run/index.d.ts.map +1 -0
- package/dist/run/index.js +2 -0
- package/dist/run/index.js.map +1 -0
- package/dist/run/run-summary.util.d.ts +31 -0
- package/dist/run/run-summary.util.d.ts.map +1 -0
- package/dist/run/run-summary.util.js +28 -0
- package/dist/run/run-summary.util.js.map +1 -0
- package/dist/tools/index.d.ts +3 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +3 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/tool-discovery.service.d.ts +31 -0
- package/dist/tools/tool-discovery.service.d.ts.map +1 -0
- package/dist/tools/tool-discovery.service.js +132 -0
- package/dist/tools/tool-discovery.service.js.map +1 -0
- package/dist/tools/tool-errors.middleware.d.ts +21 -0
- package/dist/tools/tool-errors.middleware.d.ts.map +1 -0
- package/dist/tools/tool-errors.middleware.js +55 -0
- package/dist/tools/tool-errors.middleware.js.map +1 -0
- package/dist/tools/tool-method.util.d.ts +21 -0
- package/dist/tools/tool-method.util.d.ts.map +1 -0
- package/dist/tools/tool-method.util.js +39 -0
- package/dist/tools/tool-method.util.js.map +1 -0
- package/dist/tools/tool-modules.util.d.ts +14 -0
- package/dist/tools/tool-modules.util.d.ts.map +1 -0
- package/dist/tools/tool-modules.util.js +61 -0
- package/dist/tools/tool-modules.util.js.map +1 -0
- package/dist/tools/tool-name.util.d.ts +10 -0
- package/dist/tools/tool-name.util.d.ts.map +1 -0
- package/dist/tools/tool-name.util.js +35 -0
- package/dist/tools/tool-name.util.js.map +1 -0
- package/dist/tools/tool-schema.factory.d.ts +4 -0
- package/dist/tools/tool-schema.factory.d.ts.map +1 -0
- package/dist/tools/tool-schema.factory.js +87 -0
- package/dist/tools/tool-schema.factory.js.map +1 -0
- package/dist/tools/tool.factory.d.ts +9 -0
- package/dist/tools/tool.factory.d.ts.map +1 -0
- package/dist/tools/tool.factory.js +34 -0
- package/dist/tools/tool.factory.js.map +1 -0
- package/lib/agent.ts +257 -0
- package/lib/constants.ts +15 -0
- package/lib/decorators/index.ts +3 -0
- package/lib/decorators/inject-agent.decorator.ts +15 -0
- package/lib/decorators/tool.decorator.ts +86 -0
- package/lib/errors/agent-run.error.ts +29 -0
- package/lib/errors/index.ts +2 -0
- package/lib/errors/messages.ts +158 -0
- package/lib/errors/tool-configuration.error.ts +24 -0
- package/lib/index.ts +26 -0
- package/lib/interfaces/agent-observer.interface.ts +113 -0
- package/lib/interfaces/completed-run.interface.ts +14 -0
- package/lib/interfaces/langchain-module-options.interface.ts +91 -0
- package/lib/langchain.module-definition.ts +36 -0
- package/lib/langchain.module.ts +138 -0
- package/lib/logging/index.ts +2 -0
- package/lib/logging/observe-tool-call.util.ts +59 -0
- package/lib/logging/run-reporter.ts +56 -0
- package/lib/run/index.ts +2 -0
- package/lib/run/run-summary.util.ts +51 -0
- package/lib/tools/index.ts +2 -0
- package/lib/tools/tool-discovery.service.ts +191 -0
- package/lib/tools/tool-errors.middleware.ts +68 -0
- package/lib/tools/tool-method.util.ts +81 -0
- package/lib/tools/tool-modules.util.ts +104 -0
- package/lib/tools/tool-name.util.ts +57 -0
- package/lib/tools/tool-schema.factory.ts +142 -0
- package/lib/tools/tool.factory.ts +56 -0
- package/package.json +112 -91
- package/dist/factories/message.factory.d.ts +0 -7
- package/dist/factories/message.factory.js +0 -28
- package/dist/langchain.service.d.ts +0 -18
- package/dist/langchain.service.js +0 -69
- package/dist/tool-discovery.service.d.ts +0 -11
- package/dist/tool-discovery.service.js +0 -79
- package/dist/types/message.d.ts +0 -69
- package/dist/types/message.js +0 -104
package/package.json
CHANGED
|
@@ -1,91 +1,112 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "nestjs-langchain",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
},
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
"
|
|
81
|
-
"
|
|
82
|
-
"
|
|
83
|
-
"
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
"
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "nestjs-langchain",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "NestJS library to build agents with LangChain",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"nestjs",
|
|
8
|
+
"langchain",
|
|
9
|
+
"agents",
|
|
10
|
+
"ai"
|
|
11
|
+
],
|
|
12
|
+
"author": "Martin Bruel",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/Martin-Bruel/nestjs-langchain.git"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/Martin-Bruel/nestjs-langchain#readme",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/Martin-Bruel/nestjs-langchain/issues"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=22.12"
|
|
24
|
+
},
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"default": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist",
|
|
40
|
+
"lib",
|
|
41
|
+
"!lib/**/*.spec.ts",
|
|
42
|
+
"README.md"
|
|
43
|
+
],
|
|
44
|
+
"workspaces": [
|
|
45
|
+
".",
|
|
46
|
+
"samples/*"
|
|
47
|
+
],
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "rimraf dist && tsc -p tsconfig.build.json",
|
|
50
|
+
"build:watch": "tsc -p tsconfig.build.json -w",
|
|
51
|
+
"format": "prettier --write \"{lib,tests,scripts}/**/*.{ts,mjs}\" vitest.config.ts",
|
|
52
|
+
"lint": "oxlint --type-aware lib/ tests/ scripts/ vitest.config.ts --fix",
|
|
53
|
+
"lint:check": "oxlint --type-aware lib/ tests/ scripts/ vitest.config.ts",
|
|
54
|
+
"test": "vitest run --typecheck",
|
|
55
|
+
"test:watch": "vitest",
|
|
56
|
+
"release": "release-it",
|
|
57
|
+
"release:rc": "release-it --preRelease=rc --no-plugins.@release-it/conventional-changelog.infile",
|
|
58
|
+
"prepublishOnly": "npm run build",
|
|
59
|
+
"prepare": "husky",
|
|
60
|
+
"test:cov": "vitest run --coverage",
|
|
61
|
+
"format:check": "prettier --check \"{lib,tests,scripts}/**/*.{ts,mjs}\" vitest.config.ts",
|
|
62
|
+
"verify:package": "node scripts/verify-package.mjs",
|
|
63
|
+
"verify:exports": "publint && attw --pack --ignore-rules cjs-resolves-to-esm"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
67
|
+
"@commitlint/cli": "21.2.3",
|
|
68
|
+
"@commitlint/config-conventional": "^21.2.3",
|
|
69
|
+
"@langchain/core": "^1.2.3",
|
|
70
|
+
"@langchain/openai": "^1.2.10",
|
|
71
|
+
"@nestjs/common": "^11.1.18",
|
|
72
|
+
"@nestjs/core": "^11.1.18",
|
|
73
|
+
"@nestjs/testing": "^11.1.18",
|
|
74
|
+
"@release-it/conventional-changelog": "12.0.2",
|
|
75
|
+
"@types/node": "^25.1.0",
|
|
76
|
+
"@vitest/coverage-v8": "^4.1.11",
|
|
77
|
+
"eslint-plugin-jsdoc": "^65.2.0",
|
|
78
|
+
"husky": "9.1.7",
|
|
79
|
+
"langchain": "^1.5.4",
|
|
80
|
+
"lint-staged": "16.4.0",
|
|
81
|
+
"oxlint": "^1.83.0",
|
|
82
|
+
"oxlint-tsgolint": "^7.0.2001",
|
|
83
|
+
"prettier": "^3.8.1",
|
|
84
|
+
"publint": "^0.3.24",
|
|
85
|
+
"reflect-metadata": "^0.1.12 || ^0.2.0",
|
|
86
|
+
"release-it": "21.1.1",
|
|
87
|
+
"rimraf": "6.1.3",
|
|
88
|
+
"rxjs": "^7.8.2",
|
|
89
|
+
"semver": "^7.8.5",
|
|
90
|
+
"typescript": "^6.0.3",
|
|
91
|
+
"vitest": "^4.1.11",
|
|
92
|
+
"zod": "^4.0.0"
|
|
93
|
+
},
|
|
94
|
+
"peerDependencies": {
|
|
95
|
+
"@langchain/core": "^1.2.3",
|
|
96
|
+
"@nestjs/common": "^11.1.18 || ^12.0.1",
|
|
97
|
+
"@nestjs/core": "^11.1.18 || ^12.0.1",
|
|
98
|
+
"langchain": "^1.5.4",
|
|
99
|
+
"reflect-metadata": "^0.1.12 || ^0.2.0",
|
|
100
|
+
"zod": "^4.0.0"
|
|
101
|
+
},
|
|
102
|
+
"lint-staged": {
|
|
103
|
+
"{lib,tests,scripts}/**/*.{ts,mjs}": [
|
|
104
|
+
"oxlint --type-aware --fix",
|
|
105
|
+
"prettier --write"
|
|
106
|
+
],
|
|
107
|
+
"vitest.config.ts": [
|
|
108
|
+
"oxlint --type-aware --fix",
|
|
109
|
+
"prettier --write"
|
|
110
|
+
]
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { AIMessage, Message, ToolMessage } from '../types/message';
|
|
2
|
-
export declare class MessageFactory {
|
|
3
|
-
static fromLangChain(lcMessage: any): Message | AIMessage | ToolMessage;
|
|
4
|
-
private static mapBaseMessage;
|
|
5
|
-
private static mapAIMessage;
|
|
6
|
-
private static mapToolMessage;
|
|
7
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MessageFactory = void 0;
|
|
4
|
-
const message_1 = require("../types/message");
|
|
5
|
-
class MessageFactory {
|
|
6
|
-
static fromLangChain(lcMessage) {
|
|
7
|
-
if (lcMessage.name === 'model') {
|
|
8
|
-
return this.mapAIMessage(lcMessage);
|
|
9
|
-
}
|
|
10
|
-
else if (lcMessage.tool_call_id) {
|
|
11
|
-
return this.mapToolMessage(lcMessage);
|
|
12
|
-
}
|
|
13
|
-
return this.mapBaseMessage(lcMessage);
|
|
14
|
-
}
|
|
15
|
-
static mapBaseMessage(lcMessage) {
|
|
16
|
-
const message = new message_1.Message(lcMessage.id, lcMessage.name, lcMessage.content, message_1.MessageType.UNKNOWN);
|
|
17
|
-
return message;
|
|
18
|
-
}
|
|
19
|
-
static mapAIMessage(lcMessage) {
|
|
20
|
-
return new message_1.AIMessage(lcMessage.id, lcMessage.name, lcMessage.content, lcMessage.response_metadata, lcMessage.usage_metadata, lcMessage.tool_calls.map((tc) => {
|
|
21
|
-
return new message_1.ToolCall(tc.id, tc.name, tc.args);
|
|
22
|
-
}), lcMessage.invalid_tool_calls);
|
|
23
|
-
}
|
|
24
|
-
static mapToolMessage(lcMessage) {
|
|
25
|
-
return new message_1.ToolMessage(lcMessage.id, lcMessage.name, lcMessage.content, lcMessage.tool_call_id);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
exports.MessageFactory = MessageFactory;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { OnModuleInit } from '@nestjs/common';
|
|
2
|
-
import { LangChainModuleOptions } from './interfaces/langchain-module-options.interface';
|
|
3
|
-
import { ToolDiscoveryService } from './tool-discovery.service';
|
|
4
|
-
export declare class LangChainService implements OnModuleInit {
|
|
5
|
-
private readonly options;
|
|
6
|
-
private readonly toolDiscovery;
|
|
7
|
-
private agent;
|
|
8
|
-
private readonly logger;
|
|
9
|
-
constructor(options: LangChainModuleOptions, toolDiscovery: ToolDiscoveryService);
|
|
10
|
-
/**
|
|
11
|
-
* Discover tools and initialize the agent
|
|
12
|
-
*/
|
|
13
|
-
onModuleInit(): Promise<void>;
|
|
14
|
-
/**
|
|
15
|
-
* Run the agent with the given input and stream the response
|
|
16
|
-
*/
|
|
17
|
-
run(input: string): Promise<string>;
|
|
18
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
-
};
|
|
14
|
-
var LangChainService_1;
|
|
15
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.LangChainService = void 0;
|
|
17
|
-
const common_1 = require("@nestjs/common");
|
|
18
|
-
const langchain_1 = require("langchain");
|
|
19
|
-
const message_1 = require("./types/message");
|
|
20
|
-
const message_factory_1 = require("./factories/message.factory");
|
|
21
|
-
const langchain_module_definition_1 = require("./langchain.module-definition");
|
|
22
|
-
const tool_discovery_service_1 = require("./tool-discovery.service");
|
|
23
|
-
let LangChainService = LangChainService_1 = class LangChainService {
|
|
24
|
-
constructor(options, toolDiscovery) {
|
|
25
|
-
this.options = options;
|
|
26
|
-
this.toolDiscovery = toolDiscovery;
|
|
27
|
-
this.logger = new common_1.Logger(LangChainService_1.name);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Discover tools and initialize the agent
|
|
31
|
-
*/
|
|
32
|
-
async onModuleInit() {
|
|
33
|
-
const tools = this.toolDiscovery.getToolsFromModules(this.options.tools || []);
|
|
34
|
-
const modelName = this.options.model.model;
|
|
35
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
36
|
-
const { model: _, ...modelFields } = this.options.model;
|
|
37
|
-
const model = await (0, langchain_1.initChatModel)(modelName, modelFields);
|
|
38
|
-
this.agent = (0, langchain_1.createAgent)({
|
|
39
|
-
model,
|
|
40
|
-
tools,
|
|
41
|
-
systemPrompt: this.options.systemPrompt,
|
|
42
|
-
});
|
|
43
|
-
this.logger.log(`Agent initialized with ${tools.length} tools.`);
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Run the agent with the given input and stream the response
|
|
47
|
-
*/
|
|
48
|
-
async run(input) {
|
|
49
|
-
const humanMessage = new message_1.HumanMessage('no_id', 'user', input);
|
|
50
|
-
humanMessage.content = input;
|
|
51
|
-
common_1.Logger.verbose(humanMessage.getLogString());
|
|
52
|
-
for await (const chunk of await this.agent.stream({
|
|
53
|
-
messages: [{ role: 'user', content: input }],
|
|
54
|
-
}, { streamMode: 'updates' })) {
|
|
55
|
-
const message = message_factory_1.MessageFactory.fromLangChain(Object.values(chunk)[0].messages[0]);
|
|
56
|
-
common_1.Logger.verbose(message.getLogString());
|
|
57
|
-
if (message.isFinished()) {
|
|
58
|
-
return message.content;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
throw new Error('No response from agent.');
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
exports.LangChainService = LangChainService;
|
|
65
|
-
exports.LangChainService = LangChainService = LangChainService_1 = __decorate([
|
|
66
|
-
(0, common_1.Injectable)(),
|
|
67
|
-
__param(0, (0, common_1.Inject)(langchain_module_definition_1.MODULE_OPTIONS_TOKEN)),
|
|
68
|
-
__metadata("design:paramtypes", [Object, tool_discovery_service_1.ToolDiscoveryService])
|
|
69
|
-
], LangChainService);
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Type } from '@nestjs/common';
|
|
2
|
-
import { DynamicStructuredTool } from '@langchain/core/tools';
|
|
3
|
-
import { DiscoveryService } from '@nestjs/core/discovery/discovery-service';
|
|
4
|
-
import { MetadataScanner } from '@nestjs/core';
|
|
5
|
-
export declare class ToolDiscoveryService {
|
|
6
|
-
private readonly discoveryService;
|
|
7
|
-
private readonly metadataScanner;
|
|
8
|
-
constructor(discoveryService: DiscoveryService, metadataScanner: MetadataScanner);
|
|
9
|
-
private generateZodSchema;
|
|
10
|
-
getToolsFromModules(allowedModules: Type<any>[]): DynamicStructuredTool[];
|
|
11
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.ToolDiscoveryService = void 0;
|
|
16
|
-
const common_1 = require("@nestjs/common");
|
|
17
|
-
const tools_1 = require("@langchain/core/tools");
|
|
18
|
-
const tool_decorator_1 = require("./decorators/tool.decorator");
|
|
19
|
-
const zod_1 = __importDefault(require("zod"));
|
|
20
|
-
const discovery_service_1 = require("@nestjs/core/discovery/discovery-service");
|
|
21
|
-
const core_1 = require("@nestjs/core");
|
|
22
|
-
let ToolDiscoveryService = class ToolDiscoveryService {
|
|
23
|
-
constructor(discoveryService, metadataScanner) {
|
|
24
|
-
this.discoveryService = discoveryService;
|
|
25
|
-
this.metadataScanner = metadataScanner;
|
|
26
|
-
}
|
|
27
|
-
generateZodSchema(params) {
|
|
28
|
-
const schemaObject = {};
|
|
29
|
-
params.forEach((param) => {
|
|
30
|
-
switch (param.type) {
|
|
31
|
-
case 'number':
|
|
32
|
-
schemaObject[param.name] = zod_1.default.number().describe(param.description);
|
|
33
|
-
break;
|
|
34
|
-
case 'boolean':
|
|
35
|
-
schemaObject[param.name] = zod_1.default.boolean().describe(param.description);
|
|
36
|
-
break;
|
|
37
|
-
default:
|
|
38
|
-
schemaObject[param.name] = zod_1.default.string().describe(param.description);
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
return zod_1.default.object(schemaObject);
|
|
42
|
-
}
|
|
43
|
-
getToolsFromModules(allowedModules) {
|
|
44
|
-
const providers = this.discoveryService.getProviders();
|
|
45
|
-
const allowedModuleNames = allowedModules.map((m) => m.name);
|
|
46
|
-
const tools = [];
|
|
47
|
-
providers.forEach((wrapper) => {
|
|
48
|
-
const { instance, host } = wrapper;
|
|
49
|
-
if (!instance || !host || !allowedModuleNames.includes(host.name)) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
const methodNames = this.metadataScanner.getAllMethodNames(Object.getPrototypeOf(instance));
|
|
53
|
-
methodNames.forEach((name) => {
|
|
54
|
-
const metadata = Reflect.getMetadata(tool_decorator_1.TOOL_METADATA, instance[name]);
|
|
55
|
-
const paramsMeta = Reflect.getMetadata(tool_decorator_1.TOOL_PARAMS_METADATA, instance, name) || [];
|
|
56
|
-
const zodSchema = this.generateZodSchema(paramsMeta);
|
|
57
|
-
if (metadata) {
|
|
58
|
-
tools.push(new tools_1.DynamicStructuredTool({
|
|
59
|
-
name: name,
|
|
60
|
-
description: metadata.description,
|
|
61
|
-
schema: zodSchema,
|
|
62
|
-
func: (args) => {
|
|
63
|
-
const sortedParams = paramsMeta.sort((a, b) => a.index - b.index);
|
|
64
|
-
const orderedArgs = sortedParams.map((p) => args[p.name]);
|
|
65
|
-
return instance[name](...orderedArgs);
|
|
66
|
-
},
|
|
67
|
-
}));
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
return tools;
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
exports.ToolDiscoveryService = ToolDiscoveryService;
|
|
75
|
-
exports.ToolDiscoveryService = ToolDiscoveryService = __decorate([
|
|
76
|
-
(0, common_1.Injectable)(),
|
|
77
|
-
__metadata("design:paramtypes", [discovery_service_1.DiscoveryService,
|
|
78
|
-
core_1.MetadataScanner])
|
|
79
|
-
], ToolDiscoveryService);
|
package/dist/types/message.d.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
export declare class Message {
|
|
2
|
-
id: string;
|
|
3
|
-
name: string;
|
|
4
|
-
content: string;
|
|
5
|
-
type: MessageType;
|
|
6
|
-
constructor(id: string, name: string, content: string, type: MessageType);
|
|
7
|
-
getLogString(): string;
|
|
8
|
-
isFinished(): boolean;
|
|
9
|
-
}
|
|
10
|
-
export declare class HumanMessage extends Message {
|
|
11
|
-
constructor(id: string, name: string, content: string);
|
|
12
|
-
getLogString(): string;
|
|
13
|
-
isFinished(): boolean;
|
|
14
|
-
}
|
|
15
|
-
export declare class AIMessage extends Message {
|
|
16
|
-
response_metadata: ResponseMetadata;
|
|
17
|
-
usage_metadata: UsageMetadata;
|
|
18
|
-
tool_calls: ToolCall[];
|
|
19
|
-
invalid_tool_calls: any[];
|
|
20
|
-
constructor(id: string, name: string, content: string, response_metadata: ResponseMetadata, usage_metadata: UsageMetadata, tool_calls: ToolCall[], invalid_tool_calls: any[]);
|
|
21
|
-
getLogString(): string;
|
|
22
|
-
isFinished(): boolean;
|
|
23
|
-
}
|
|
24
|
-
export declare class ToolMessage extends Message {
|
|
25
|
-
constructor(id: string, name: string, content: string, tool_call_id: string);
|
|
26
|
-
tool_call_id: string;
|
|
27
|
-
getLogString(): string;
|
|
28
|
-
}
|
|
29
|
-
export declare class ToolCall {
|
|
30
|
-
name: string;
|
|
31
|
-
args: any;
|
|
32
|
-
id: string;
|
|
33
|
-
constructor(id: string, name: string, args: any);
|
|
34
|
-
toString(): string;
|
|
35
|
-
}
|
|
36
|
-
export declare class ResponseMetadata {
|
|
37
|
-
tokenUsage: {
|
|
38
|
-
promptTokens: number;
|
|
39
|
-
completionTokens: number;
|
|
40
|
-
totalTokens: number;
|
|
41
|
-
};
|
|
42
|
-
finish_reason: string;
|
|
43
|
-
model_provider: string;
|
|
44
|
-
model_name: string;
|
|
45
|
-
constructor(tokenUsage: {
|
|
46
|
-
promptTokens: number;
|
|
47
|
-
completionTokens: number;
|
|
48
|
-
totalTokens: number;
|
|
49
|
-
}, finish_reason: string, model_provider: string, model_name: string);
|
|
50
|
-
}
|
|
51
|
-
export declare enum FinishReason {
|
|
52
|
-
STOP = "stop",
|
|
53
|
-
TOOL_CALLS = "tool_calls",
|
|
54
|
-
UNKNOWN = "unknown"
|
|
55
|
-
}
|
|
56
|
-
export declare class UsageMetadata {
|
|
57
|
-
constructor(output_tokens: number, input_tokens: number, total_tokens: number, input_token_details: any, output_token_details: any);
|
|
58
|
-
output_tokens: number;
|
|
59
|
-
input_tokens: number;
|
|
60
|
-
total_tokens: number;
|
|
61
|
-
input_token_details: any;
|
|
62
|
-
output_token_details: any;
|
|
63
|
-
}
|
|
64
|
-
export declare enum MessageType {
|
|
65
|
-
AI = "ai",
|
|
66
|
-
HUMAN = "human",
|
|
67
|
-
TOOL = "tool",
|
|
68
|
-
UNKNOWN = "unknown"
|
|
69
|
-
}
|
package/dist/types/message.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MessageType = exports.UsageMetadata = exports.FinishReason = exports.ResponseMetadata = exports.ToolCall = exports.ToolMessage = exports.AIMessage = exports.HumanMessage = exports.Message = void 0;
|
|
4
|
-
class Message {
|
|
5
|
-
constructor(id, name, content, type) {
|
|
6
|
-
this.id = id;
|
|
7
|
-
this.name = name;
|
|
8
|
-
this.content = content;
|
|
9
|
-
this.type = type;
|
|
10
|
-
}
|
|
11
|
-
getLogString() {
|
|
12
|
-
return `Message [ID: ${this.id}, Name: ${this.name}, Type: ${this.type}, Content: ${this.content}]`;
|
|
13
|
-
}
|
|
14
|
-
isFinished() {
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.Message = Message;
|
|
19
|
-
class HumanMessage extends Message {
|
|
20
|
-
constructor(id, name, content) {
|
|
21
|
-
super(id, name, content, MessageType.HUMAN);
|
|
22
|
-
}
|
|
23
|
-
getLogString() {
|
|
24
|
-
return `HumanMessage: ${this.content}`;
|
|
25
|
-
}
|
|
26
|
-
isFinished() {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.HumanMessage = HumanMessage;
|
|
31
|
-
class AIMessage extends Message {
|
|
32
|
-
constructor(id, name, content, response_metadata, usage_metadata, tool_calls, invalid_tool_calls) {
|
|
33
|
-
super(id, name, content, MessageType.AI);
|
|
34
|
-
this.response_metadata = response_metadata;
|
|
35
|
-
this.usage_metadata = usage_metadata;
|
|
36
|
-
this.tool_calls = tool_calls;
|
|
37
|
-
this.invalid_tool_calls = invalid_tool_calls;
|
|
38
|
-
}
|
|
39
|
-
getLogString() {
|
|
40
|
-
if (this.response_metadata?.finish_reason === FinishReason.TOOL_CALLS) {
|
|
41
|
-
return `AIMessage: Call tools: ${this.tool_calls
|
|
42
|
-
.map((tc) => tc.toString())
|
|
43
|
-
.join(', ')}`;
|
|
44
|
-
}
|
|
45
|
-
return `AIMessage: ${this.content}`;
|
|
46
|
-
}
|
|
47
|
-
isFinished() {
|
|
48
|
-
return this.response_metadata?.finish_reason === FinishReason.STOP;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
exports.AIMessage = AIMessage;
|
|
52
|
-
class ToolMessage extends Message {
|
|
53
|
-
constructor(id, name, content, tool_call_id) {
|
|
54
|
-
super(id, name, content, MessageType.TOOL);
|
|
55
|
-
this.tool_call_id = tool_call_id;
|
|
56
|
-
}
|
|
57
|
-
getLogString() {
|
|
58
|
-
return `ToolMessage call tool ${this.name} with args: ${this.content}`;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
exports.ToolMessage = ToolMessage;
|
|
62
|
-
class ToolCall {
|
|
63
|
-
constructor(id, name, args) {
|
|
64
|
-
this.name = name;
|
|
65
|
-
this.args = args;
|
|
66
|
-
this.id = id;
|
|
67
|
-
}
|
|
68
|
-
toString() {
|
|
69
|
-
return `${this.name} with args ${JSON.stringify(this.args)}`;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
exports.ToolCall = ToolCall;
|
|
73
|
-
class ResponseMetadata {
|
|
74
|
-
constructor(tokenUsage, finish_reason, model_provider, model_name) {
|
|
75
|
-
this.tokenUsage = tokenUsage;
|
|
76
|
-
this.finish_reason = finish_reason;
|
|
77
|
-
this.model_provider = model_provider;
|
|
78
|
-
this.model_name = model_name;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
exports.ResponseMetadata = ResponseMetadata;
|
|
82
|
-
var FinishReason;
|
|
83
|
-
(function (FinishReason) {
|
|
84
|
-
FinishReason["STOP"] = "stop";
|
|
85
|
-
FinishReason["TOOL_CALLS"] = "tool_calls";
|
|
86
|
-
FinishReason["UNKNOWN"] = "unknown";
|
|
87
|
-
})(FinishReason || (exports.FinishReason = FinishReason = {}));
|
|
88
|
-
class UsageMetadata {
|
|
89
|
-
constructor(output_tokens, input_tokens, total_tokens, input_token_details, output_token_details) {
|
|
90
|
-
this.output_tokens = output_tokens;
|
|
91
|
-
this.input_tokens = input_tokens;
|
|
92
|
-
this.total_tokens = total_tokens;
|
|
93
|
-
this.input_token_details = input_token_details;
|
|
94
|
-
this.output_token_details = output_token_details;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
exports.UsageMetadata = UsageMetadata;
|
|
98
|
-
var MessageType;
|
|
99
|
-
(function (MessageType) {
|
|
100
|
-
MessageType["AI"] = "ai";
|
|
101
|
-
MessageType["HUMAN"] = "human";
|
|
102
|
-
MessageType["TOOL"] = "tool";
|
|
103
|
-
MessageType["UNKNOWN"] = "unknown";
|
|
104
|
-
})(MessageType || (exports.MessageType = MessageType = {}));
|