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
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { createMiddleware, toolErrorMiddleware, ToolInvocationError, } from 'langchain';
|
|
2
|
+
import { unknownTool } from '../errors/messages.js';
|
|
3
|
+
import { reportToolError } from '../logging/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* Turns a failed tool call into the text the model receives, LangChain's own,
|
|
6
|
+
* without the stack (and its server paths) LangChain puts in a schema failure.
|
|
7
|
+
* Every error is answered: past a middleware, LangChain's tool node rethrows
|
|
8
|
+
* any error left unhandled and fails the run. See #176.
|
|
9
|
+
*
|
|
10
|
+
* Also the only place that sees arguments the schema rejects, which never
|
|
11
|
+
* reach the `@Tool` wrapper: they are reported here. See #177.
|
|
12
|
+
*
|
|
13
|
+
* Once langchain-ai/langchainjs#11830 ships, LangChain's own text has no stack
|
|
14
|
+
* and `tool-errors.middleware.spec.ts` fails: the schema text can be left to
|
|
15
|
+
* LangChain, the reporting stays.
|
|
16
|
+
*/
|
|
17
|
+
export const toolErrors = (reporter) => toolErrorMiddleware({
|
|
18
|
+
onError: (error) => {
|
|
19
|
+
if (!ToolInvocationError.isInstance(error)) {
|
|
20
|
+
// LangChain's default for a method that throws.
|
|
21
|
+
return `${String(error)}\n Please fix your mistakes.`;
|
|
22
|
+
}
|
|
23
|
+
const { toolCall, toolError } = error;
|
|
24
|
+
reportToolError(reporter, {
|
|
25
|
+
tool: toolCall.name,
|
|
26
|
+
callId: toolCall.id ?? '',
|
|
27
|
+
reason: 'invalid-arguments',
|
|
28
|
+
error: toolError,
|
|
29
|
+
durationMs: 0,
|
|
30
|
+
});
|
|
31
|
+
return (`Error invoking tool '${toolCall.name}' with kwargs ` +
|
|
32
|
+
`${JSON.stringify(toolCall.args)} with error: ${String(toolError)}\n` +
|
|
33
|
+
' Please fix the error and try again.');
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
/**
|
|
37
|
+
* Reports a call to a tool the agent does not have. LangChain's tool node
|
|
38
|
+
* answers the model itself, without throwing, so `toolErrors` never sees it.
|
|
39
|
+
*/
|
|
40
|
+
export const unknownTools = (reporter) => createMiddleware({
|
|
41
|
+
name: 'unknownTools',
|
|
42
|
+
wrapToolCall: (request, handler) => {
|
|
43
|
+
if (!request.tool) {
|
|
44
|
+
reportToolError(reporter, {
|
|
45
|
+
tool: request.toolCall.name,
|
|
46
|
+
callId: request.toolCall.id ?? '',
|
|
47
|
+
reason: 'unknown-tool',
|
|
48
|
+
error: new Error(unknownTool()),
|
|
49
|
+
durationMs: 0,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return handler(request);
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
//# sourceMappingURL=tool-errors.middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-errors.middleware.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,CAAC;YAClB,eAAe,CAAC,QAAQ,EAAE;gBACxB,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC,IAAI;gBAC3B,MAAM,EAAE,OAAO,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE;gBACjC,MAAM,EAAE,cAAc;gBACtB,KAAK,EAAE,IAAI,KAAK,CAAC,WAAW,EAAE,CAAC;gBAC/B,UAAU,EAAE,CAAC;aACd,CAAC,CAAC;QACL,CAAC;QAED,OAAO,OAAO,CAAC,OAAO,CAAC,CAAC;IAC1B,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ToolOptions, ToolParamMetadata } from '../decorators/tool.decorator.js';
|
|
2
|
+
/** A provider method decorated with `@Tool()`, and what its decorators recorded. */
|
|
3
|
+
export interface ToolMethod {
|
|
4
|
+
/** `MathService.add`, to name the method in error messages. */
|
|
5
|
+
where: string;
|
|
6
|
+
/** The method name, the tool's name when `options` gives none. */
|
|
7
|
+
method: string;
|
|
8
|
+
options: ToolOptions;
|
|
9
|
+
/** The `@ToolParam()` metadata, in declaration order. */
|
|
10
|
+
params: ToolParamMetadata[];
|
|
11
|
+
/** The declared parameter types, from TypeScript's `design:paramtypes`. */
|
|
12
|
+
paramTypes: unknown[];
|
|
13
|
+
/** Calls the method on its provider, with its arguments by position. */
|
|
14
|
+
call: (args: unknown[]) => unknown;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Reads `method` of a provider instance, or `undefined` if it is not a
|
|
18
|
+
* `@Tool()`. Throws when it carries a `@ToolParam()` but no `@Tool()`.
|
|
19
|
+
*/
|
|
20
|
+
export declare const readToolMethod: (instance: object, method: string) => ToolMethod | undefined;
|
|
21
|
+
//# sourceMappingURL=tool-method.util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-method.util.d.ts","sourceRoot":"","sources":["../../lib/tools/tool-method.util.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,WAAW,EACX,iBAAiB,EAClB,MAAM,iCAAiC,CAAC;AAKzC,oFAAoF;AACpF,MAAM,WAAW,UAAU;IACzB,+DAA+D;IAC/D,KAAK,EAAE,MAAM,CAAC;IACd,kEAAkE;IAClE,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,WAAW,CAAC;IACrB,yDAAyD;IACzD,MAAM,EAAE,iBAAiB,EAAE,CAAC;IAC5B,2EAA2E;IAC3E,UAAU,EAAE,OAAO,EAAE,CAAC;IACtB,wEAAwE;IACxE,IAAI,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC;CACpC;AAiBD;;;GAGG;AACH,eAAO,MAAM,cAAc,GACzB,UAAU,MAAM,EAChB,QAAQ,MAAM,KACb,UAAU,GAAG,SA8Bf,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { PARAM_TYPES_METADATA, TOOL_METADATA, TOOL_PARAMS_METADATA, } from '../constants.js';
|
|
2
|
+
import { toolParamWithoutTool } from '../errors/messages.js';
|
|
3
|
+
// The prototype that defines `method`: its metadata belongs to the function
|
|
4
|
+
// `instance[method]` resolves to, the one carrying `@Tool()` or not.
|
|
5
|
+
const definingPrototype = (instance, method) => {
|
|
6
|
+
let prototype = Object.getPrototypeOf(instance);
|
|
7
|
+
while (prototype &&
|
|
8
|
+
!Object.prototype.hasOwnProperty.call(prototype, method)) {
|
|
9
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
10
|
+
}
|
|
11
|
+
return prototype ?? instance;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Reads `method` of a provider instance, or `undefined` if it is not a
|
|
15
|
+
* `@Tool()`. Throws when it carries a `@ToolParam()` but no `@Tool()`.
|
|
16
|
+
*/
|
|
17
|
+
export const readToolMethod = (instance, method) => {
|
|
18
|
+
const provider = instance;
|
|
19
|
+
const options = Reflect.getMetadata(TOOL_METADATA, provider[method]);
|
|
20
|
+
const owner = definingPrototype(instance, method);
|
|
21
|
+
const declared = Reflect.getOwnMetadata(TOOL_PARAMS_METADATA, owner, method) ?? [];
|
|
22
|
+
const where = `${instance.constructor.name}.${method}`;
|
|
23
|
+
if (!options) {
|
|
24
|
+
if (declared.length > 0) {
|
|
25
|
+
throw new Error(toolParamWithoutTool(where));
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
where,
|
|
31
|
+
method,
|
|
32
|
+
options,
|
|
33
|
+
// Parameter decorators run right to left.
|
|
34
|
+
params: [...declared].sort((a, b) => a.index - b.index),
|
|
35
|
+
paramTypes: Reflect.getOwnMetadata(PARAM_TYPES_METADATA, owner, method) ?? [],
|
|
36
|
+
call: (args) => provider[method](...args),
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=tool-method.util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-method.util.js","sourceRoot":"","sources":["../../lib/tools/tool-method.util.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,aAAa,EACb,oBAAoB,GACrB,MAAM,iBAAiB,CAAC;AAKzB,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAmB7D,4EAA4E;AAC5E,qEAAqE;AACrE,MAAM,iBAAiB,GAAG,CAAC,QAAgB,EAAE,MAAc,EAAU,EAAE;IACrE,IAAI,SAAS,GAAkB,MAAM,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;IAE/D,OACE,SAAS;QACT,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,EACxD,CAAC;QACD,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,SAAS,IAAI,QAAQ,CAAC;AAC/B,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,QAAgB,EAChB,MAAc,EACU,EAAE;IAC1B,MAAM,QAAQ,GAAG,QAAoB,CAAC;IACtC,MAAM,OAAO,GAA4B,OAAO,CAAC,WAAW,CAC1D,aAAa,EACb,QAAQ,CAAC,MAAM,CAAC,CACjB,CAAC;IAEF,MAAM,KAAK,GAAG,iBAAiB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAClD,MAAM,QAAQ,GACZ,OAAO,CAAC,cAAc,CAAC,oBAAoB,EAAE,KAAK,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;IACpE,MAAM,KAAK,GAAG,GAAG,QAAQ,CAAC,WAAW,CAAC,IAAI,IAAI,MAAM,EAAE,CAAC;IAEvD,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAC,CAAC;QAC/C,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,OAAO;QACL,KAAK;QACL,MAAM;QACN,OAAO;QACP,0CAA0C;QAC1C,MAAM,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC;QACvD,UAAU,EACR,OAAO,CAAC,cAAc,CAAC,oBAAoB,EAAE,KAAK,EAAE,MAAM,CAAC,IAAI,EAAE;QACnE,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC;KAC1C,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
2
|
+
import { ToolModule } from '../interfaces/langchain-module-options.interface.js';
|
|
3
|
+
interface ToolModules {
|
|
4
|
+
modules: Set<Type>;
|
|
5
|
+
problems: string[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Split the `tools` entries into the modules discovery can match and the
|
|
9
|
+
* problems to report. `inContext` counts the modules Nest registered per
|
|
10
|
+
* class: one per plain import, one per dynamic module.
|
|
11
|
+
*/
|
|
12
|
+
export declare const resolveToolModules: (entries: readonly ToolModule[], inContext: ReadonlyMap<Type, number>) => Promise<ToolModules>;
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=tool-modules.util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-modules.util.d.ts","sourceRoot":"","sources":["../../lib/tools/tool-modules.util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmC,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAOvE,OAAO,EAAE,UAAU,EAAE,MAAM,qDAAqD,CAAC;AAqDjF,UAAU,WAAW;IACnB,OAAO,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC;IACnB,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAED;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,GAC7B,SAAS,SAAS,UAAU,EAAE,EAC9B,WAAW,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC,KACnC,OAAO,CAAC,WAAW,CA8BrB,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { MODULE_METADATA_KEYS } from '../constants.js';
|
|
2
|
+
import { notAToolModule, severalToolModuleInstances, toolModuleNotImported, } from '../errors/messages.js';
|
|
3
|
+
// `@Module({})` writes no metadata at all, so an unimported empty module and a
|
|
4
|
+
// plain class are indistinguishable. Only used to sharpen the message.
|
|
5
|
+
const looksLikeAModule = (entry) => MODULE_METADATA_KEYS.some((key) => Reflect.getMetadata(key, entry) !== undefined);
|
|
6
|
+
/**
|
|
7
|
+
* Reduce an entry to the class Nest registered it under, the way the scanner
|
|
8
|
+
* resolves `imports`: await it, call a forward reference, take `module` off a
|
|
9
|
+
* dynamic module.
|
|
10
|
+
*/
|
|
11
|
+
const toModuleClass = async (entry) => {
|
|
12
|
+
const awaited = await entry;
|
|
13
|
+
if (typeof awaited === 'function') {
|
|
14
|
+
return { type: awaited, declaresItself: false };
|
|
15
|
+
}
|
|
16
|
+
if (typeof awaited !== 'object' || awaited === null) {
|
|
17
|
+
return { declaresItself: false };
|
|
18
|
+
}
|
|
19
|
+
const reference = awaited;
|
|
20
|
+
if (typeof reference.forwardRef === 'function') {
|
|
21
|
+
return toModuleClass(reference.forwardRef());
|
|
22
|
+
}
|
|
23
|
+
const dynamic = awaited;
|
|
24
|
+
if (!dynamic.module) {
|
|
25
|
+
return { declaresItself: false };
|
|
26
|
+
}
|
|
27
|
+
return { ...(await toModuleClass(dynamic.module)), declaresItself: true };
|
|
28
|
+
};
|
|
29
|
+
// Only reached when the entry resolved to no class at all.
|
|
30
|
+
const describeEntry = (entry) => entry === null || entry === undefined
|
|
31
|
+
? String(entry)
|
|
32
|
+
: `an entry of type ${typeof entry}`;
|
|
33
|
+
/**
|
|
34
|
+
* Split the `tools` entries into the modules discovery can match and the
|
|
35
|
+
* problems to report. `inContext` counts the modules Nest registered per
|
|
36
|
+
* class: one per plain import, one per dynamic module.
|
|
37
|
+
*/
|
|
38
|
+
export const resolveToolModules = async (entries, inContext) => {
|
|
39
|
+
const modules = new Set();
|
|
40
|
+
const problems = [];
|
|
41
|
+
for (const entry of entries) {
|
|
42
|
+
// A circular import resolves to `undefined` here rather than to a class.
|
|
43
|
+
const { type, declaresItself } = await toModuleClass(entry);
|
|
44
|
+
const instances = type ? (inContext.get(type) ?? 0) : 0;
|
|
45
|
+
if (type && instances === 1) {
|
|
46
|
+
modules.add(type);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
// Tools are matched by class, which would merge the instances' tools.
|
|
50
|
+
// See #234.
|
|
51
|
+
if (type && instances > 1) {
|
|
52
|
+
problems.push(severalToolModuleInstances(type.name, instances));
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const name = type?.name ?? describeEntry(entry);
|
|
56
|
+
const isModule = type && (declaresItself || looksLikeAModule(type));
|
|
57
|
+
problems.push(isModule ? toolModuleNotImported(name) : notAToolModule(name));
|
|
58
|
+
}
|
|
59
|
+
return { modules, problems };
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=tool-modules.util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-modules.util.js","sourceRoot":"","sources":["../../lib/tools/tool-modules.util.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,EACL,cAAc,EACd,0BAA0B,EAC1B,qBAAqB,GACtB,MAAM,uBAAuB,CAAC;AAG/B,+EAA+E;AAC/E,uEAAuE;AACvE,MAAM,gBAAgB,GAAG,CAAC,KAAW,EAAW,EAAE,CAChD,oBAAoB,CAAC,IAAI,CACvB,CAAC,GAAG,EAAE,EAAE,CAAC,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,KAAK,CAAC,KAAK,SAAS,CACvD,CAAC;AASJ;;;;GAIG;AACH,MAAM,aAAa,GAAG,KAAK,EAAE,KAAiB,EAAqB,EAAE;IACnE,MAAM,OAAO,GAAY,MAAM,KAAK,CAAC;IAErC,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;QAClC,OAAO,EAAE,IAAI,EAAE,OAAe,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC;IAC1D,CAAC;IAED,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACpD,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC;IACnC,CAAC;IAED,MAAM,SAAS,GAAG,OAAsD,CAAC;IAEzE,IAAI,OAAO,SAAS,CAAC,UAAU,KAAK,UAAU,EAAE,CAAC;QAC/C,OAAO,aAAa,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC,CAAC;IAC/C,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EAAE,SAAS,CAAC,CAAC,CAAC;YAChE,SAAS;QACX,CAAC;QAED,MAAM,IAAI,GAAG,IAAI,EAAE,IAAI,IAAI,aAAa,CAAC,KAAK,CAAC,CAAC;QAChD,MAAM,QAAQ,GAAG,IAAI,IAAI,CAAC,cAAc,IAAI,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC;QAEpE,QAAQ,CAAC,IAAI,CACX,QAAQ,CAAC,CAAC,CAAC,qBAAqB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,IAAI,CAAC,CAC9D,CAAC;IACJ,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC;AAC/B,CAAC,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const resolveToolName: (declared: string | undefined, method: string, where: string) => string;
|
|
2
|
+
/**
|
|
3
|
+
* One problem per tool whose name an earlier tool already took, naming both
|
|
4
|
+
* methods. `where` is the method each tool was built from.
|
|
5
|
+
*/
|
|
6
|
+
export declare const findDuplicateToolNames: (tools: {
|
|
7
|
+
name: string;
|
|
8
|
+
where: string;
|
|
9
|
+
}[]) => string[];
|
|
10
|
+
//# sourceMappingURL=tool-name.util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-name.util.d.ts","sourceRoot":"","sources":["../../lib/tools/tool-name.util.ts"],"names":[],"mappings":"AAcA,eAAO,MAAM,eAAe,GAC1B,UAAU,MAAM,GAAG,SAAS,EAC5B,QAAQ,MAAM,EACd,OAAO,MAAM,KACZ,MAcF,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,sBAAsB,GACjC,OAAO;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,EAAE,KACvC,MAAM,EAgBR,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { duplicateToolName, invalidToolName, reservedToolName, } from '../errors/messages.js';
|
|
2
|
+
// OpenAI's limit, the strictest published, restated: no provider SDK exports
|
|
3
|
+
// it. See #90.
|
|
4
|
+
const TOOL_NAME = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
5
|
+
// The agent routes a call with this prefix to structured output, never to the
|
|
6
|
+
// tool. Restated: LangChain does not export it. See #213.
|
|
7
|
+
const STRUCTURED_OUTPUT_PREFIX = 'extract-';
|
|
8
|
+
export const resolveToolName = (declared, method, where) => {
|
|
9
|
+
const name = declared ?? method;
|
|
10
|
+
if (!TOOL_NAME.test(name)) {
|
|
11
|
+
throw new Error(invalidToolName(where, name, String(TOOL_NAME), declared === undefined));
|
|
12
|
+
}
|
|
13
|
+
if (name.startsWith(STRUCTURED_OUTPUT_PREFIX)) {
|
|
14
|
+
throw new Error(reservedToolName(where, name, STRUCTURED_OUTPUT_PREFIX));
|
|
15
|
+
}
|
|
16
|
+
return name;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* One problem per tool whose name an earlier tool already took, naming both
|
|
20
|
+
* methods. `where` is the method each tool was built from.
|
|
21
|
+
*/
|
|
22
|
+
export const findDuplicateToolNames = (tools) => {
|
|
23
|
+
const seen = new Map();
|
|
24
|
+
const problems = [];
|
|
25
|
+
tools.forEach(({ name, where }) => {
|
|
26
|
+
const first = seen.get(name);
|
|
27
|
+
if (first) {
|
|
28
|
+
problems.push(duplicateToolName(name, first, where));
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
seen.set(name, where);
|
|
32
|
+
});
|
|
33
|
+
return problems;
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=tool-name.util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-name.util.js","sourceRoot":"","sources":["../../lib/tools/tool-name.util.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,eAAe,EACf,gBAAgB,GACjB,MAAM,uBAAuB,CAAC;AAE/B,6EAA6E;AAC7E,eAAe;AACf,MAAM,SAAS,G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}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ZodObject } from 'zod';
|
|
2
|
+
import { ToolParamMetadata } from '../decorators/tool.decorator.js';
|
|
3
|
+
export declare const buildToolSchema: (params: ToolParamMetadata[], paramTypes: unknown[], where: string) => ZodObject;
|
|
4
|
+
//# sourceMappingURL=tool-schema.factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-schema.factory.d.ts","sourceRoot":"","sources":["../../lib/tools/tool-schema.factory.ts"],"names":[],"mappings":"AAEA,OAAU,EAAE,SAAS,EAAW,MAAM,KAAK,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAwGpE,eAAO,MAAM,eAAe,GAC1B,QAAQ,iBAAiB,EAAE,EAC3B,YAAY,OAAO,EAAE,EACrB,OAAO,MAAM,KACZ,SA8BF,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { toJsonSchema } from '@langchain/core/utils/json_schema';
|
|
2
|
+
import z from 'zod';
|
|
3
|
+
import { cannotInferSchema, duplicateParamName, noJsonSchemaForm, schemaContradictsSignature, } from '../errors/messages.js';
|
|
4
|
+
import { messageOf } from '../logging/index.js';
|
|
5
|
+
const INFERRED = new Map([
|
|
6
|
+
[String, () => z.string()],
|
|
7
|
+
[Number, () => z.number()],
|
|
8
|
+
[Boolean, () => z.boolean()],
|
|
9
|
+
]);
|
|
10
|
+
// The JSON Schema types each primitive signature accepts, the first naming it.
|
|
11
|
+
// `integer` is a subset of `number`.
|
|
12
|
+
const JSON_SCHEMA_TYPES = new Map([
|
|
13
|
+
[String, ['string']],
|
|
14
|
+
[Number, ['number', 'integer']],
|
|
15
|
+
[Boolean, ['boolean']],
|
|
16
|
+
]);
|
|
17
|
+
const resolveSchema = (param, paramType, where) => {
|
|
18
|
+
if (param.schema) {
|
|
19
|
+
return param.schema;
|
|
20
|
+
}
|
|
21
|
+
const inferred = INFERRED.get(paramType);
|
|
22
|
+
if (!inferred) {
|
|
23
|
+
const declared = paramType?.name ?? 'unknown';
|
|
24
|
+
throw new Error(cannotInferSchema(where, param.name, declared));
|
|
25
|
+
}
|
|
26
|
+
return inferred();
|
|
27
|
+
};
|
|
28
|
+
// The JSON Schema types a schema allows, from its `type` or from the branches
|
|
29
|
+
// of its `anyOf`: zod 4 gives a union either form (see #137). `undefined` when
|
|
30
|
+
// it has no JSON Schema form, or a branch names no type.
|
|
31
|
+
const typesOf = (schema) => {
|
|
32
|
+
let node;
|
|
33
|
+
try {
|
|
34
|
+
node = z.toJSONSchema(schema);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
const branches = node.type === undefined && node.anyOf ? node.anyOf : [node];
|
|
40
|
+
if (branches.some((branch) => branch.type === undefined)) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
return branches.flatMap((branch) => branch.type ?? []);
|
|
44
|
+
};
|
|
45
|
+
// A declared schema LangChain cannot convert to the JSON Schema a provider
|
|
46
|
+
// receives. Wrapped in an object, as LangChain converts a tool's schema: only
|
|
47
|
+
// there does it read the input side of a `.transform()`.
|
|
48
|
+
const checkJsonSchemaForm = (param, schema, where) => {
|
|
49
|
+
try {
|
|
50
|
+
toJsonSchema(z.object({ [param.name]: schema }));
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
throw new Error(noJsonSchemaForm(where, param.name, messageOf(error)));
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
// A declared schema that contradicts a primitive signature. Skipped for any
|
|
57
|
+
// other signature, where the schema is the only source of truth.
|
|
58
|
+
const checkAgainstSignature = (param, paramType, schema, where) => {
|
|
59
|
+
const accepted = JSON_SCHEMA_TYPES.get(paramType);
|
|
60
|
+
const types = accepted && typesOf(schema);
|
|
61
|
+
if (!accepted || !types || types.some((type) => accepted.includes(type))) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
throw new Error(schemaContradictsSignature(where, param.name, types, accepted[0]));
|
|
65
|
+
};
|
|
66
|
+
export const buildToolSchema = (params, paramTypes, where) => {
|
|
67
|
+
const shape = {};
|
|
68
|
+
const indexes = new Map();
|
|
69
|
+
params.forEach((param) => {
|
|
70
|
+
const first = indexes.get(param.name);
|
|
71
|
+
if (first !== undefined) {
|
|
72
|
+
throw new Error(duplicateParamName(where, param.name, first, param.index));
|
|
73
|
+
}
|
|
74
|
+
indexes.set(param.name, param.index);
|
|
75
|
+
const resolved = resolveSchema(param, paramTypes[param.index], where);
|
|
76
|
+
if (param.schema) {
|
|
77
|
+
checkJsonSchemaForm(param, resolved, where);
|
|
78
|
+
checkAgainstSignature(param, paramTypes[param.index], resolved, where);
|
|
79
|
+
}
|
|
80
|
+
const described = param.description
|
|
81
|
+
? resolved.describe(param.description)
|
|
82
|
+
: resolved;
|
|
83
|
+
shape[param.name] = param.optional ? described.optional() : described;
|
|
84
|
+
});
|
|
85
|
+
return z.object(shape);
|
|
86
|
+
};
|
|
87
|
+
//# sourceMappingURL=tool-schema.factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-schema.factory.js","sourceRoot":"","sources":["../../lib/tools/tool-schema.factory.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AACjE,OAAO,CAAyB,MAAM,KAAK,CAAC;AAE5C,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,0BAA0B,GAC3B,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhD,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAyB;IAC/C,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IAC1B,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IAC1B,CAA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CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;IACxE,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACzB,CAAC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DynamicStructuredTool } from 'langchain';
|
|
2
|
+
import { RunReporter } from '../logging/index.js';
|
|
3
|
+
import { ToolMethod } from './tool-method.util.js';
|
|
4
|
+
/**
|
|
5
|
+
* Builds the LangChain tool that calls a `@Tool()` method and reports each
|
|
6
|
+
* call to `reporter`. Throws when the name or a parameter schema is invalid.
|
|
7
|
+
*/
|
|
8
|
+
export declare const buildTool: ({ where, method, options, params, paramTypes, call }: ToolMethod, reporter: RunReporter) => DynamicStructuredTool;
|
|
9
|
+
//# sourceMappingURL=tool.factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.factory.d.ts","sourceRoot":"","sources":["../../lib/tools/tool.factory.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAElD,OAAO,EAAmB,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGnE,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAsBnD;;;GAGG;AACH,eAAO,MAAM,SAAS,GACpB,sDAAsD,UAAU,EAChE,UAAU,WAAW,KACpB,qBAmBF,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// From `langchain`, not `@langchain/core`: core's declaration gives an
|
|
2
|
+
// identity `createAgent` rejects. See #52.
|
|
3
|
+
import { DynamicStructuredTool } from 'langchain';
|
|
4
|
+
import { observeToolCall } from '../logging/index.js';
|
|
5
|
+
import { buildToolSchema } from './tool-schema.factory.js';
|
|
6
|
+
import { resolveToolName } from './tool-name.util.js';
|
|
7
|
+
// Placed by index: an omitted optional leaves a hole rather than shifting the
|
|
8
|
+
// arguments after it.
|
|
9
|
+
const toPositional = (params, values) => {
|
|
10
|
+
const args = [];
|
|
11
|
+
params.forEach((param) => {
|
|
12
|
+
args[param.index] = values[param.name];
|
|
13
|
+
});
|
|
14
|
+
return args;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Builds the LangChain tool that calls a `@Tool()` method and reports each
|
|
18
|
+
* call to `reporter`. Throws when the name or a parameter schema is invalid.
|
|
19
|
+
*/
|
|
20
|
+
export const buildTool = ({ where, method, options, params, paramTypes, call }, reporter) => {
|
|
21
|
+
const name = resolveToolName(options.name, method, where);
|
|
22
|
+
return new DynamicStructuredTool({
|
|
23
|
+
name,
|
|
24
|
+
description: options.description,
|
|
25
|
+
schema: buildToolSchema(params, paramTypes, where),
|
|
26
|
+
func: (values, _runManager, config) => observeToolCall(reporter, {
|
|
27
|
+
tool: name,
|
|
28
|
+
// Empty only for a tool invoked outside an agent.
|
|
29
|
+
callId: config?.toolCall?.id ?? '',
|
|
30
|
+
args: values,
|
|
31
|
+
}, () => call(toPositional(params, values))),
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=tool.factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.factory.js","sourceRoot":"","sources":["../../lib/tools/tool.factory.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,2CAA2C;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAElD,OAAO,EAAE,eAAe,EAAe,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAQtD,8EAA8E;AAC9E,sBAAsB;AACtB,MAAM,YAAY,GAAG,CACnB,MAA2B,EAC3B,MAA+B,EACpB,EAAE;IACb,MAAM,IAAI,GAAc,EAAE,CAAC;IAE3B,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,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