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.
Files changed (151) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +344 -254
  3. package/dist/agent.d.ts +63 -0
  4. package/dist/agent.d.ts.map +1 -0
  5. package/dist/agent.js +189 -0
  6. package/dist/agent.js.map +1 -0
  7. package/dist/constants.d.ts +5 -0
  8. package/dist/constants.d.ts.map +1 -0
  9. package/dist/constants.js +14 -0
  10. package/dist/constants.js.map +1 -0
  11. package/dist/decorators/index.d.ts +4 -2
  12. package/dist/decorators/index.d.ts.map +1 -0
  13. package/dist/decorators/index.js +3 -18
  14. package/dist/decorators/index.js.map +1 -0
  15. package/dist/decorators/inject-agent.decorator.d.ts +12 -0
  16. package/dist/decorators/inject-agent.decorator.d.ts.map +1 -0
  17. package/dist/decorators/inject-agent.decorator.js +15 -7
  18. package/dist/decorators/inject-agent.decorator.js.map +1 -0
  19. package/dist/decorators/tool.decorator.d.ts +54 -6
  20. package/dist/decorators/tool.decorator.d.ts.map +1 -0
  21. package/dist/decorators/tool.decorator.js +40 -16
  22. package/dist/decorators/tool.decorator.js.map +1 -0
  23. package/dist/errors/agent-run.error.d.ts +27 -0
  24. package/dist/errors/agent-run.error.d.ts.map +1 -0
  25. package/dist/errors/agent-run.error.js +26 -0
  26. package/dist/errors/agent-run.error.js.map +1 -0
  27. package/dist/errors/index.d.ts +3 -0
  28. package/dist/errors/index.d.ts.map +1 -0
  29. package/dist/errors/index.js +3 -0
  30. package/dist/errors/index.js.map +1 -0
  31. package/dist/errors/messages.d.ts +26 -0
  32. package/dist/errors/messages.d.ts.map +1 -0
  33. package/dist/errors/messages.js +67 -0
  34. package/dist/errors/messages.js.map +1 -0
  35. package/dist/errors/tool-configuration.error.d.ts +23 -0
  36. package/dist/errors/tool-configuration.error.d.ts.map +1 -0
  37. package/dist/errors/tool-configuration.error.js +27 -0
  38. package/dist/errors/tool-configuration.error.js.map +1 -0
  39. package/dist/index.d.ts +10 -3
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +8 -19
  42. package/dist/index.js.map +1 -0
  43. package/dist/interfaces/agent-observer.interface.d.ts +107 -0
  44. package/dist/interfaces/agent-observer.interface.d.ts.map +1 -0
  45. package/dist/interfaces/agent-observer.interface.js +2 -0
  46. package/dist/interfaces/agent-observer.interface.js.map +1 -0
  47. package/dist/interfaces/completed-run.interface.d.ts +14 -0
  48. package/dist/interfaces/completed-run.interface.d.ts.map +1 -0
  49. package/dist/interfaces/completed-run.interface.js +2 -0
  50. package/dist/interfaces/completed-run.interface.js.map +1 -0
  51. package/dist/interfaces/langchain-module-options.interface.d.ts +73 -5
  52. package/dist/interfaces/langchain-module-options.interface.d.ts.map +1 -0
  53. package/dist/interfaces/langchain-module-options.interface.js +2 -2
  54. package/dist/interfaces/langchain-module-options.interface.js.map +1 -0
  55. package/dist/langchain.module-definition.d.ts +26 -6
  56. package/dist/langchain.module-definition.d.ts.map +1 -0
  57. package/dist/langchain.module-definition.js +26 -13
  58. package/dist/langchain.module-definition.js.map +1 -0
  59. package/dist/langchain.module.d.ts +60 -3
  60. package/dist/langchain.module.d.ts.map +1 -0
  61. package/dist/langchain.module.js +87 -24
  62. package/dist/langchain.module.js.map +1 -0
  63. package/dist/logging/index.d.ts +3 -0
  64. package/dist/logging/index.d.ts.map +1 -0
  65. package/dist/logging/index.js +3 -0
  66. package/dist/logging/index.js.map +1 -0
  67. package/dist/logging/observe-tool-call.util.d.ts +13 -0
  68. package/dist/logging/observe-tool-call.util.d.ts.map +1 -0
  69. package/dist/logging/observe-tool-call.util.js +34 -0
  70. package/dist/logging/observe-tool-call.util.js.map +1 -0
  71. package/dist/logging/run-reporter.d.ts +26 -0
  72. package/dist/logging/run-reporter.d.ts.map +1 -0
  73. package/dist/logging/run-reporter.js +39 -0
  74. package/dist/logging/run-reporter.js.map +1 -0
  75. package/dist/run/index.d.ts +3 -0
  76. package/dist/run/index.d.ts.map +1 -0
  77. package/dist/run/index.js +2 -0
  78. package/dist/run/index.js.map +1 -0
  79. package/dist/run/run-summary.util.d.ts +31 -0
  80. package/dist/run/run-summary.util.d.ts.map +1 -0
  81. package/dist/run/run-summary.util.js +28 -0
  82. package/dist/run/run-summary.util.js.map +1 -0
  83. package/dist/tools/index.d.ts +3 -0
  84. package/dist/tools/index.d.ts.map +1 -0
  85. package/dist/tools/index.js +3 -0
  86. package/dist/tools/index.js.map +1 -0
  87. package/dist/tools/tool-discovery.service.d.ts +31 -0
  88. package/dist/tools/tool-discovery.service.d.ts.map +1 -0
  89. package/dist/tools/tool-discovery.service.js +132 -0
  90. package/dist/tools/tool-discovery.service.js.map +1 -0
  91. package/dist/tools/tool-errors.middleware.d.ts +21 -0
  92. package/dist/tools/tool-errors.middleware.d.ts.map +1 -0
  93. package/dist/tools/tool-errors.middleware.js +55 -0
  94. package/dist/tools/tool-errors.middleware.js.map +1 -0
  95. package/dist/tools/tool-method.util.d.ts +21 -0
  96. package/dist/tools/tool-method.util.d.ts.map +1 -0
  97. package/dist/tools/tool-method.util.js +39 -0
  98. package/dist/tools/tool-method.util.js.map +1 -0
  99. package/dist/tools/tool-modules.util.d.ts +14 -0
  100. package/dist/tools/tool-modules.util.d.ts.map +1 -0
  101. package/dist/tools/tool-modules.util.js +61 -0
  102. package/dist/tools/tool-modules.util.js.map +1 -0
  103. package/dist/tools/tool-name.util.d.ts +10 -0
  104. package/dist/tools/tool-name.util.d.ts.map +1 -0
  105. package/dist/tools/tool-name.util.js +35 -0
  106. package/dist/tools/tool-name.util.js.map +1 -0
  107. package/dist/tools/tool-schema.factory.d.ts +4 -0
  108. package/dist/tools/tool-schema.factory.d.ts.map +1 -0
  109. package/dist/tools/tool-schema.factory.js +87 -0
  110. package/dist/tools/tool-schema.factory.js.map +1 -0
  111. package/dist/tools/tool.factory.d.ts +9 -0
  112. package/dist/tools/tool.factory.d.ts.map +1 -0
  113. package/dist/tools/tool.factory.js +34 -0
  114. package/dist/tools/tool.factory.js.map +1 -0
  115. package/lib/agent.ts +257 -0
  116. package/lib/constants.ts +15 -0
  117. package/lib/decorators/index.ts +3 -0
  118. package/lib/decorators/inject-agent.decorator.ts +15 -0
  119. package/lib/decorators/tool.decorator.ts +86 -0
  120. package/lib/errors/agent-run.error.ts +29 -0
  121. package/lib/errors/index.ts +2 -0
  122. package/lib/errors/messages.ts +158 -0
  123. package/lib/errors/tool-configuration.error.ts +24 -0
  124. package/lib/index.ts +26 -0
  125. package/lib/interfaces/agent-observer.interface.ts +113 -0
  126. package/lib/interfaces/completed-run.interface.ts +14 -0
  127. package/lib/interfaces/langchain-module-options.interface.ts +91 -0
  128. package/lib/langchain.module-definition.ts +36 -0
  129. package/lib/langchain.module.ts +138 -0
  130. package/lib/logging/index.ts +2 -0
  131. package/lib/logging/observe-tool-call.util.ts +59 -0
  132. package/lib/logging/run-reporter.ts +56 -0
  133. package/lib/run/index.ts +2 -0
  134. package/lib/run/run-summary.util.ts +51 -0
  135. package/lib/tools/index.ts +2 -0
  136. package/lib/tools/tool-discovery.service.ts +191 -0
  137. package/lib/tools/tool-errors.middleware.ts +68 -0
  138. package/lib/tools/tool-method.util.ts +81 -0
  139. package/lib/tools/tool-modules.util.ts +104 -0
  140. package/lib/tools/tool-name.util.ts +57 -0
  141. package/lib/tools/tool-schema.factory.ts +142 -0
  142. package/lib/tools/tool.factory.ts +56 -0
  143. package/package.json +112 -91
  144. package/dist/factories/message.factory.d.ts +0 -7
  145. package/dist/factories/message.factory.js +0 -28
  146. package/dist/langchain.service.d.ts +0 -18
  147. package/dist/langchain.service.js +0 -69
  148. package/dist/tool-discovery.service.d.ts +0 -11
  149. package/dist/tool-discovery.service.js +0 -79
  150. package/dist/types/message.d.ts +0 -69
  151. package/dist/types/message.js +0 -104
@@ -0,0 +1,63 @@
1
+ import { OnModuleInit } from '@nestjs/common';
2
+ import { DiscoveryService } from '@nestjs/core';
3
+ import { LangChainModuleOptions } from './interfaces/langchain-module-options.interface.js';
4
+ import { CompletedRun } from './interfaces/completed-run.interface.js';
5
+ import { ToolDiscoveryService } from './tools/index.js';
6
+ /**
7
+ * One agent, registered through `LangChainModule`. Inject it directly for the
8
+ * unnamed agent, or with `@InjectAgent(name)` for a named one.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * constructor(private readonly agent: Agent) {}
13
+ *
14
+ * async ask(question: string): Promise<string> {
15
+ * const { output } = await this.agent.run(question);
16
+ * return output;
17
+ * }
18
+ * ```
19
+ */
20
+ export declare class Agent implements OnModuleInit {
21
+ #private;
22
+ private readonly options;
23
+ private readonly agentName;
24
+ private readonly toolDiscovery;
25
+ private readonly discovery;
26
+ private readonly logger;
27
+ private readonly prefix;
28
+ private readonly reporter;
29
+ private readonly modelErrors;
30
+ /** @internal Built by `LangChainModule`, never by hand. */
31
+ constructor(options: LangChainModuleOptions, agentName: string, toolDiscovery: ToolDiscoveryService, discovery: DiscoveryService);
32
+ /**
33
+ * Called by Nest: builds the agent, once the tool modules' providers exist.
34
+ *
35
+ * @internal
36
+ */
37
+ onModuleInit(): Promise<void>;
38
+ private assertUniqueName;
39
+ /**
40
+ * Build the model from its configuration, or hand back the instance the
41
+ * caller already built. Recognised by the members `createAgent` checks,
42
+ * never by `instanceof`, which two copies of `@langchain/core` break.
43
+ * See #52.
44
+ *
45
+ * @param option The `model` option.
46
+ * @returns The model `createAgent` receives.
47
+ */
48
+ private resolveModel;
49
+ /**
50
+ * Runs the agent to completion on one user message. Every failure is thrown
51
+ * as an `AgentRunError`, the original error in its `cause`.
52
+ *
53
+ * @param input The user's message.
54
+ * @returns The model's answer, the tools it called and what the run cost.
55
+ * @example
56
+ * ```ts
57
+ * const { output, tools } = await agent.run('What is 6 times 7?');
58
+ * ```
59
+ */
60
+ run(input: string): Promise<CompletedRun>;
61
+ private complete;
62
+ }
63
+ //# sourceMappingURL=agent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../lib/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAOhD,OAAO,EACL,sBAAsB,EAEvB,MAAM,oDAAoD,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,yCAAyC,CAAC;AACvE,OAAO,EACL,oBAAoB,EAGrB,MAAM,kBAAkB,CAAC;AAsB1B;;;;;;;;;;;;;GAaG;AACH,qBACa,KAAM,YAAW,YAAY;;IActC,OAAO,CAAC,QAAQ,CAAC,OAAO;IAExB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,aAAa;IAC9B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAd5B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;IACvC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAc;IAGvC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAsB;IAElD,2DAA2D;gBAGxC,OAAO,EAAE,sBAAsB,EAE/B,SAAS,EAAE,MAAM,EACjB,aAAa,EAAE,oBAAoB,EACnC,SAAS,EAAE,gBAAgB;IAmB9C;;;;OAIG;IACG,YAAY;IAwBlB,OAAO,CAAC,gBAAgB;IAiBxB;;;;;;;;OAQG;YACW,YAAY;IA8B1B;;;;;;;;;;OAUG;IACG,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;YAsCjC,QAAQ;CA+BvB"}
package/dist/agent.js ADDED
@@ -0,0 +1,189 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ 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;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
11
+ return function (target, key) { decorator(target, key, paramIndex); }
12
+ };
13
+ import { Inject, Injectable, Logger } from '@nestjs/common';
14
+ import { DiscoveryService } from '@nestjs/core';
15
+ import { createAgent, initChatModel } from 'langchain';
16
+ import { AGENT_NAME_TOKEN, MODULE_OPTIONS_TOKEN, UNNAMED_AGENT, } from './langchain.module-definition.js';
17
+ import { ToolDiscoveryService, toolErrors, unknownTools, } from './tools/index.js';
18
+ import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
19
+ import { LOG_CONTEXT, messageOf, RunReporter } from './logging/index.js';
20
+ import { summariseRun } from './run/index.js';
21
+ import { AgentRunError } from './errors/index.js';
22
+ import { agentNotBootstrapped, agentPrefix, agentRunFailed, duplicateAgentName, modelNeverReplied, modelNotBuilt, modelReplyEmpty, notAChatModel, } from './errors/messages.js';
23
+ /**
24
+ * One agent, registered through `LangChainModule`. Inject it directly for the
25
+ * unnamed agent, or with `@InjectAgent(name)` for a named one.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * constructor(private readonly agent: Agent) {}
30
+ *
31
+ * async ask(question: string): Promise<string> {
32
+ * const { output } = await this.agent.run(question);
33
+ * return output;
34
+ * }
35
+ * ```
36
+ */
37
+ let Agent = class Agent {
38
+ options;
39
+ agentName;
40
+ toolDiscovery;
41
+ discovery;
42
+ // Assembled in `onModuleInit`, once every tool module's providers exist.
43
+ #graph;
44
+ // Without a context, since Nest appends the instance's to each call's own.
45
+ logger = new Logger();
46
+ prefix;
47
+ reporter;
48
+ // Model failures, which only LangChain's callbacks see. Tool calls are
49
+ // reported by their wrapper and the tool middlewares.
50
+ modelErrors;
51
+ /** @internal Built by `LangChainModule`, never by hand. */
52
+ constructor(options, agentName, toolDiscovery, discovery) {
53
+ this.options = options;
54
+ this.agentName = agentName;
55
+ this.toolDiscovery = toolDiscovery;
56
+ this.discovery = discovery;
57
+ this.prefix = agentPrefix(agentName);
58
+ this.reporter = new RunReporter({
59
+ logger: this.logger,
60
+ agent: agentName,
61
+ prefix: this.prefix,
62
+ observer: options.observer,
63
+ });
64
+ this.modelErrors = BaseCallbackHandler.fromMethods({
65
+ handleLLMError: (error) => {
66
+ this.reporter.logError(`the model failed: ${messageOf(error)}`);
67
+ this.reporter.notify((observer) => observer.onModelError?.({ agent: agentName, error }));
68
+ },
69
+ });
70
+ }
71
+ /**
72
+ * Called by Nest: builds the agent, once the tool modules' providers exist.
73
+ *
74
+ * @internal
75
+ */
76
+ async onModuleInit() {
77
+ this.assertUniqueName();
78
+ const tools = await this.toolDiscovery.getToolsFromModules(this.options.tools ?? [], this.reporter);
79
+ this.#graph = createAgent({
80
+ model: await this.resolveModel(this.options.model),
81
+ tools,
82
+ systemPrompt: this.options.systemPrompt,
83
+ // The first wraps the next: `toolErrors` sits right around the tool.
84
+ middleware: [unknownTools(this.reporter), toolErrors(this.reporter)],
85
+ });
86
+ this.logger.log(`${this.prefix}ready with ${tools.length} tool${tools.length === 1 ? '' : 's'}`, LOG_CONTEXT);
87
+ }
88
+ // Every registration provides its own name, so a name found twice in the
89
+ // container is two agents behind one token.
90
+ assertUniqueName() {
91
+ const registered = this.discovery
92
+ .getProviders()
93
+ .filter(({ token, instance }) => token === AGENT_NAME_TOKEN && instance === this.agentName);
94
+ if (registered.length > 1) {
95
+ throw new Error(duplicateAgentName(this.agentName === UNNAMED_AGENT ? undefined : this.agentName));
96
+ }
97
+ }
98
+ /**
99
+ * Build the model from its configuration, or hand back the instance the
100
+ * caller already built. Recognised by the members `createAgent` checks,
101
+ * never by `instanceof`, which two copies of `@langchain/core` break.
102
+ * See #52.
103
+ *
104
+ * @param option The `model` option.
105
+ * @returns The model `createAgent` receives.
106
+ */
107
+ async resolveModel(option) {
108
+ // The types forbid a primitive, an untyped configuration does not. See #236.
109
+ const given = option;
110
+ if (typeof given !== 'object' || given === null) {
111
+ throw new Error(notAChatModel(this.prefix));
112
+ }
113
+ if (!('invoke' in option)) {
114
+ const { model, ...fields } = option;
115
+ try {
116
+ return await initChatModel(model, fields);
117
+ }
118
+ catch (error) {
119
+ throw new Error(modelNotBuilt(this.prefix, error), { cause: error });
120
+ }
121
+ }
122
+ if (typeof option.invoke !== 'function' ||
123
+ typeof option.bindTools !== 'function' ||
124
+ typeof option._streamResponseChunks !== 'function') {
125
+ throw new Error(notAChatModel(this.prefix));
126
+ }
127
+ // The same object, typed from the declarations `createAgent` reads.
128
+ return option;
129
+ }
130
+ /**
131
+ * Runs the agent to completion on one user message. Every failure is thrown
132
+ * as an `AgentRunError`, the original error in its `cause`.
133
+ *
134
+ * @param input The user's message.
135
+ * @returns The model's answer, the tools it called and what the run cost.
136
+ * @example
137
+ * ```ts
138
+ * const { output, tools } = await agent.run('What is 6 times 7?');
139
+ * ```
140
+ */
141
+ async run(input) {
142
+ const startedAt = Date.now();
143
+ const durationMs = () => Date.now() - startedAt;
144
+ try {
145
+ const { answer, messages } = await this.complete(input);
146
+ const finished = {
147
+ durationMs: durationMs(),
148
+ ...summariseRun(messages),
149
+ };
150
+ this.reporter.notify((observer) => observer.onRunFinish?.({ agent: this.agentName, ...finished }));
151
+ return { status: 'completed', output: answer, ...finished };
152
+ }
153
+ catch (thrown) {
154
+ const error = thrown instanceof AgentRunError
155
+ ? thrown
156
+ : new AgentRunError(agentRunFailed(this.prefix, thrown), this.agentName, { cause: thrown });
157
+ this.reporter.notify((observer) => observer.onRunError?.({
158
+ agent: this.agentName,
159
+ durationMs: durationMs(),
160
+ error,
161
+ }));
162
+ throw error;
163
+ }
164
+ }
165
+ async complete(input) {
166
+ if (!this.#graph) {
167
+ throw new AgentRunError(agentNotBootstrapped(this.prefix), this.agentName);
168
+ }
169
+ const { messages } = await this.#graph.invoke({ messages: [{ role: 'user', content: input }] }, { callbacks: [this.modelErrors, ...(this.options.callbacks ?? [])] });
170
+ const last = messages[messages.length - 1];
171
+ if (!last || last.type !== 'ai') {
172
+ throw new AgentRunError(modelNeverReplied(this.prefix, last?.type), this.agentName);
173
+ }
174
+ // `text`, not `content`: block answers are an array.
175
+ if (!last.text) {
176
+ throw new AgentRunError(modelReplyEmpty(this.prefix), this.agentName);
177
+ }
178
+ return { answer: last.text, messages };
179
+ }
180
+ };
181
+ Agent = __decorate([
182
+ Injectable(),
183
+ __param(0, Inject(MODULE_OPTIONS_TOKEN)),
184
+ __param(1, Inject(AGENT_NAME_TOKEN)),
185
+ __metadata("design:paramtypes", [Object, String, ToolDiscoveryService,
186
+ DiscoveryService])
187
+ ], Agent);
188
+ export { Agent };
189
+ //# sourceMappingURL=agent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent.js","sourceRoot":"","sources":["../lib/agent.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAgB,MAAM,gBAAgB,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AACvD,OAAO,EACL,gBAAgB,EAChB,oBAAoB,EACpB,aAAa,GACd,MAAM,kCAAkC,CAAC;AAM1C,OAAO,EACL,oBAAoB,EACpB,UAAU,EACV,YAAY,GACb,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CA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Ae,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CACvD,EAAE,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,EAAE,EAChD,EAAE,SAAS,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC,EAAE,CACrE,CAAC;QAEF,MAAM,IAAI,GAAG,QAAQ,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAE3C,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;YAChC,MAAM,IAAI,aAAa,CACrB,iBAAiB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,EAC1C,IAAI,CAAC,SAAS,CACf,CAAC;QACJ,CAAC;QAED,qDAAqD;QACrD,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;YACf,MAAM,IAAI,aAAa,CAAC,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;QACxE,CAAC;QAED,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,CAAC;IACzC,CAAC;CACF,CAAA;AA1MY,KAAK;IADjB,UAAU,EAAE;IAcR,WAAA,MAAM,CAAC,oBAAoB,CAAC,CAAA;IAE5B,WAAA,MAAM,CAAC,gBAAgB,CAAC,CAAA;qDAEO,oBAAoB;QACxB,gBAAgB;GAlBnC,KAAK,CA0MjB"}
@@ -0,0 +1,5 @@
1
+ export declare const TOOL_METADATA = "nestjs-langchain:tool";
2
+ export declare const TOOL_PARAMS_METADATA = "nestjs-langchain:tool-params";
3
+ export declare const PARAM_TYPES_METADATA = "design:paramtypes";
4
+ export declare const MODULE_METADATA_KEYS: string[];
5
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../lib/constants.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,aAAa,0BAA0B,CAAC;AACrD,eAAO,MAAM,oBAAoB,iCAAiC,CAAC;AAGnE,eAAO,MAAM,oBAAoB,sBAAsB,CAAC;AAIxD,eAAO,MAAM,oBAAoB,UAKhC,CAAC"}
@@ -0,0 +1,14 @@
1
+ // Namespaced like the other tokens, so another library's marks are not read.
2
+ export const TOOL_METADATA = 'nestjs-langchain:tool';
3
+ export const TOOL_PARAMS_METADATA = 'nestjs-langchain:tool-params';
4
+ // Emitted by `emitDecoratorMetadata` on every decorated method.
5
+ export const PARAM_TYPES_METADATA = 'design:paramtypes';
6
+ // The keys `@Module()` writes, restated: `@nestjs/common/constants` is
7
+ // internal. See #97.
8
+ export const MODULE_METADATA_KEYS = [
9
+ 'imports',
10
+ 'providers',
11
+ 'controllers',
12
+ 'exports',
13
+ ];
14
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../lib/constants.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,MAAM,CAAC,MAAM,aAAa,GAAG,uBAAuB,CAAC;AACrD,MAAM,CAAC,MAAM,oBAAoB,GAAG,8BAA8B,CAAC;AAEnE,gEAAgE;AAChE,MAAM,CAAC,MAAM,oBAAoB,GAAG,mBAAmB,CAAC;AAExD,uEAAuE;AACvE,qBAAqB;AACrB,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,SAAS;IACT,WAAW;IACX,aAAa;IACb,SAAS;CACV,CAAC"}
@@ -1,2 +1,4 @@
1
- export * from './inject-agent.decorator';
2
- export * from './tool.decorator';
1
+ export { InjectAgent } from './inject-agent.decorator.js';
2
+ export { Tool, ToolParam } from './tool.decorator.js';
3
+ export type { ToolOptions, ToolParamOptions } from './tool.decorator.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/decorators/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACtD,YAAY,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC"}
@@ -1,18 +1,3 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./inject-agent.decorator"), exports);
18
- __exportStar(require("./tool.decorator"), exports);
1
+ export { InjectAgent } from './inject-agent.decorator.js';
2
+ export { Tool, ToolParam } from './tool.decorator.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/decorators/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC"}
@@ -1 +1,13 @@
1
+ /**
2
+ * Injects the agent registered under `name`. The agent registered without a
3
+ * name is injected as `Agent`, with no decorator.
4
+ *
5
+ * @param name The `name` the agent was registered with.
6
+ * @returns The parameter decorator.
7
+ * @example
8
+ * ```ts
9
+ * constructor(@InjectAgent('MATH_AGENT') private readonly math: Agent) {}
10
+ * ```
11
+ */
1
12
  export declare const InjectAgent: (name: string) => PropertyDecorator & ParameterDecorator;
13
+ //# sourceMappingURL=inject-agent.decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inject-agent.decorator.d.ts","sourceRoot":"","sources":["../../lib/decorators/inject-agent.decorator.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,WAAW,GAAI,MAAM,MAAM,2CAAgC,CAAC"}
@@ -1,7 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InjectAgent = void 0;
4
- const common_1 = require("@nestjs/common");
5
- const langchain_module_definition_1 = require("../langchain.module-definition");
6
- const InjectAgent = (name) => (0, common_1.Inject)((0, langchain_module_definition_1.getAgentToken)(name));
7
- exports.InjectAgent = InjectAgent;
1
+ import { Inject } from '@nestjs/common';
2
+ import { getAgentToken } from '../langchain.module-definition.js';
3
+ /**
4
+ * Injects the agent registered under `name`. The agent registered without a
5
+ * name is injected as `Agent`, with no decorator.
6
+ *
7
+ * @param name The `name` the agent was registered with.
8
+ * @returns The parameter decorator.
9
+ * @example
10
+ * ```ts
11
+ * constructor(@InjectAgent('MATH_AGENT') private readonly math: Agent) {}
12
+ * ```
13
+ */
14
+ export const InjectAgent = (name) => Inject(getAgentToken(name));
15
+ //# sourceMappingURL=inject-agent.decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inject-agent.decorator.js","sourceRoot":"","sources":["../../lib/decorators/inject-agent.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,mCAAmC,CAAC;AAElE;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC"}
@@ -1,13 +1,61 @@
1
- export declare const TOOL_METADATA = "TOOL_METADATA";
2
- export declare const TOOL_PARAM_METADATA = "TOOL_PARAM_METADATA";
1
+ import type { ZodType } from 'zod';
2
+ /** The options of `@Tool()`. */
3
3
  export interface ToolOptions {
4
+ /**
5
+ * The name the model calls, the method name by default. Matches
6
+ * `/^[a-zA-Z0-9_-]{1,64}$/` and does not start with `extract-`, which
7
+ * LangChain reserves for structured output.
8
+ */
9
+ name?: string;
10
+ /** What the tool does, which the model reads to decide when to call it. */
4
11
  description: string;
5
12
  }
6
- export declare const Tool: (options: ToolOptions) => import("@nestjs/common").CustomDecorator<string>;
13
+ /**
14
+ * Exposes a provider method as a tool to the agents whose `tools` list its
15
+ * module. A method decorator only: discovery never reads one placed on a
16
+ * class.
17
+ *
18
+ * @param options The tool's description and, optionally, its name.
19
+ * @returns The method decorator.
20
+ * @example
21
+ * ```ts
22
+ * \@Tool({ description: 'Doubles a number.' })
23
+ * double(@ToolParam({ name: 'x' }) x: number): number {
24
+ * return x * 2;
25
+ * }
26
+ * ```
27
+ */
28
+ export declare const Tool: (options: ToolOptions) => MethodDecorator;
29
+ /** The options of `@ToolParam()`. */
7
30
  export interface ToolParamOptions {
31
+ /** The argument's name in the schema the model fills in. */
8
32
  name: string;
9
- type: 'string' | 'number' | 'boolean' | 'object';
10
- description: string;
33
+ /** What the argument means, read by the model. */
34
+ description?: string;
35
+ /** Required unless the signature resolves to string, number or boolean. */
36
+ schema?: ZodType;
37
+ /** Lets the model leave the argument out; declare the parameter `x?: T`. */
38
+ optional?: boolean;
39
+ }
40
+ /** What `@ToolParam()` records for one parameter. */
41
+ export interface ToolParamMetadata extends ToolParamOptions {
42
+ /** The parameter's position in the method's signature. */
43
+ index: number;
11
44
  }
12
- export declare const TOOL_PARAMS_METADATA = "LANGCHAIN_TOOL_PARAMS";
45
+ /**
46
+ * Declares a parameter of a `@Tool()` method as an argument of the tool.
47
+ *
48
+ * @param options The argument's name and, optionally, its description,
49
+ * schema and whether it is optional.
50
+ * @returns The parameter decorator.
51
+ * @example
52
+ * ```ts
53
+ * const Tags = z.array(z.string());
54
+ *
55
+ * count(@ToolParam({ name: 'tags', schema: Tags }) tags: string[]): number {
56
+ * return tags.length;
57
+ * }
58
+ * ```
59
+ */
13
60
  export declare function ToolParam(options: ToolParamOptions): ParameterDecorator;
61
+ //# sourceMappingURL=tool.decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool.decorator.d.ts","sourceRoot":"","sources":["../../lib/decorators/tool.decorator.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAInC,gCAAgC;AAChC,MAAM,WAAW,WAAW;IAC1B;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,2EAA2E;IAC3E,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,IAAI,GAAI,SAAS,WAAW,KAAG,eACP,CAAC;AAEtC,qCAAqC;AACrC,MAAM,WAAW,gBAAgB;IAC/B,4DAA4D;IAC5D,IAAI,EAAE,MAAM,CAAC;IACb,kDAAkD;IAClD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,2EAA2E;IAC3E,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,4EAA4E;IAC5E,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,qDAAqD;AACrD,MAAM,WAAW,iBAAkB,SAAQ,gBAAgB;IACzD,0DAA0D;IAC1D,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,gBAAgB,GAAG,kBAAkB,CAiBvE"}
@@ -1,21 +1,45 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TOOL_PARAMS_METADATA = exports.Tool = exports.TOOL_PARAM_METADATA = exports.TOOL_METADATA = void 0;
4
- exports.ToolParam = ToolParam;
5
- const common_1 = require("@nestjs/common");
6
- exports.TOOL_METADATA = 'TOOL_METADATA';
7
- exports.TOOL_PARAM_METADATA = 'TOOL_PARAM_METADATA';
8
- const Tool = (options) => (0, common_1.SetMetadata)(exports.TOOL_METADATA, options);
9
- exports.Tool = Tool;
10
- exports.TOOL_PARAMS_METADATA = 'LANGCHAIN_TOOL_PARAMS';
11
- function ToolParam(options) {
1
+ import { SetMetadata } from '@nestjs/common';
2
+ import { TOOL_METADATA, TOOL_PARAMS_METADATA } from '../constants.js';
3
+ import { toolParamOutsideMethod } from '../errors/messages.js';
4
+ /**
5
+ * Exposes a provider method as a tool to the agents whose `tools` list its
6
+ * module. A method decorator only: discovery never reads one placed on a
7
+ * class.
8
+ *
9
+ * @param options The tool's description and, optionally, its name.
10
+ * @returns The method decorator.
11
+ * @example
12
+ * ```ts
13
+ * \@Tool({ description: 'Doubles a number.' })
14
+ * double(@ToolParam({ name: 'x' }) x: number): number {
15
+ * return x * 2;
16
+ * }
17
+ * ```
18
+ */
19
+ export const Tool = (options) => SetMetadata(TOOL_METADATA, options);
20
+ /**
21
+ * Declares a parameter of a `@Tool()` method as an argument of the tool.
22
+ *
23
+ * @param options The argument's name and, optionally, its description,
24
+ * schema and whether it is optional.
25
+ * @returns The parameter decorator.
26
+ * @example
27
+ * ```ts
28
+ * const Tags = z.array(z.string());
29
+ *
30
+ * count(@ToolParam({ name: 'tags', schema: Tags }) tags: string[]): number {
31
+ * return tags.length;
32
+ * }
33
+ * ```
34
+ */
35
+ export function ToolParam(options) {
12
36
  return (target, propertyKey, parameterIndex) => {
13
37
  if (propertyKey === undefined) {
14
- throw new Error('ToolParam can only be used on method parameters');
38
+ throw new Error(toolParamOutsideMethod());
15
39
  }
16
- const existingParams = Reflect.getMetadata(exports.TOOL_PARAMS_METADATA, target, propertyKey) || [];
17
- // On enregistre l'index pour savoir quel argument remplir plus tard
18
- existingParams.push({ ...options, index: parameterIndex });
19
- Reflect.defineMetadata(exports.TOOL_PARAMS_METADATA, existingParams, target, propertyKey);
40
+ // Own metadata only: a subclass redeclaring a method starts afresh.
41
+ const existing = Reflect.getOwnMetadata(TOOL_PARAMS_METADATA, target, propertyKey) ?? [];
42
+ Reflect.defineMetadata(TOOL_PARAMS_METADATA, [...existing, { ...options, index: parameterIndex }], target, propertyKey);
20
43
  };
21
44
  }
45
+ //# sourceMappingURL=tool.decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool.decorator.js","sourceRoot":"","sources":["../../lib/decorators/tool.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AACtE,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAc/D;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,OAAoB,EAAmB,EAAE,CAC5D,WAAW,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;AAoBtC;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,SAAS,CAAC,OAAyB;IACjD,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,cAAc,EAAE,EAAE;QAC7C,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;YAC9B,MAAM,IAAI,KAAK,CAAC,sBAAsB,EAAE,CAAC,CAAC;QAC5C,CAAC;QAED,oEAAoE;QACpE,MAAM,QAAQ,GACZ,OAAO,CAAC,cAAc,CAAC,oBAAoB,EAAE,MAAM,EAAE,WAAW,CAAC,IAAI,EAAE,CAAC;QAE1E,OAAO,CAAC,cAAc,CACpB,oBAAoB,EACpB,CAAC,GAAG,QAAQ,EAAE,EAAE,GAAG,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,CAAC,EACpD,MAAM,EACN,WAAW,CACZ,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * A run of an agent failed. The base class of every run failure: everything
3
+ * `run()` throws is one, with the original error, if any, in `cause`.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * try {
8
+ * await agent.run(question);
9
+ * } catch (error) {
10
+ * if (error instanceof AgentRunError) {
11
+ * console.error(error.agent, error.cause);
12
+ * }
13
+ * }
14
+ * ```
15
+ */
16
+ export declare class AgentRunError extends Error {
17
+ /** `'default'` when the agent was registered without a name. */
18
+ readonly agent: string;
19
+ /** The error that made the run fail, when it came from elsewhere. */
20
+ readonly cause?: unknown;
21
+ constructor(message: string,
22
+ /** `'default'` when the agent was registered without a name. */
23
+ agent: string, options?: {
24
+ cause?: unknown;
25
+ });
26
+ }
27
+ //# sourceMappingURL=agent-run.error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-run.error.d.ts","sourceRoot":"","sources":["../../lib/errors/agent-run.error.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,qBAAa,aAAc,SAAQ,KAAK;IAMpC,gEAAgE;IAChE,QAAQ,CAAC,KAAK,EAAE,MAAM;IANxB,qEAAqE;IACrE,SAAiB,KAAK,CAAC,EAAE,OAAO,CAAC;gBAG/B,OAAO,EAAE,MAAM;IACf,gEAAgE;IACvD,KAAK,EAAE,MAAM,EACtB,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE;CAKhC"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * A run of an agent failed. The base class of every run failure: everything
3
+ * `run()` throws is one, with the original error, if any, in `cause`.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * try {
8
+ * await agent.run(question);
9
+ * } catch (error) {
10
+ * if (error instanceof AgentRunError) {
11
+ * console.error(error.agent, error.cause);
12
+ * }
13
+ * }
14
+ * ```
15
+ */
16
+ export class AgentRunError extends Error {
17
+ agent;
18
+ constructor(message,
19
+ /** `'default'` when the agent was registered without a name. */
20
+ agent, options) {
21
+ super(message, options);
22
+ this.agent = agent;
23
+ this.name = 'AgentRunError';
24
+ }
25
+ }
26
+ //# sourceMappingURL=agent-run.error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-run.error.js","sourceRoot":"","sources":["../../lib/errors/agent-run.error.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,aAAc,SAAQ,KAAK;IAO3B;IAHX,YACE,OAAe;IACf,gEAAgE;IACvD,KAAa,EACtB,OAA6B;QAE7B,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAHf,UAAK,GAAL,KAAK,CAAQ;QAItB,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF"}
@@ -0,0 +1,3 @@
1
+ export { AgentRunError } from './agent-run.error.js';
2
+ export { ToolConfigurationError } from './tool-configuration.error.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/errors/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { AgentRunError } from './agent-run.error.js';
2
+ export { ToolConfigurationError } from './tool-configuration.error.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/errors/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC"}
@@ -0,0 +1,26 @@
1
+ export declare const agentPrefix: (agent: string) => string;
2
+ export declare const toolProblems: (prefix: string, problems: string[]) => string;
3
+ export declare const notAToolModule: (entry: string) => string;
4
+ export declare const toolModuleNotImported: (entry: string) => string;
5
+ export declare const severalToolModuleInstances: (entry: string, count: number) => string;
6
+ export declare const toolModuleWithoutTools: (entry: string) => string;
7
+ export declare const toolParamWithoutTool: (where: string) => string;
8
+ export declare const toolParamOutsideMethod: () => string;
9
+ export declare const toolOnNonSingleton: (provider: string, scope: "request" | "transient" | "request-dependency", methods: string[]) => string;
10
+ export declare const duplicateToolName: (name: string, first: string, second: string) => string;
11
+ export declare const invalidToolName: (where: string, name: string, pattern: string, fromMethodName: boolean) => string;
12
+ export declare const reservedToolName: (where: string, name: string, prefix: string) => string;
13
+ export declare const cannotInferSchema: (where: string, param: string, declared: string) => string;
14
+ export declare const schemaContradictsSignature: (where: string, param: string, declared: string[], expected: string) => string;
15
+ export declare const noJsonSchemaForm: (where: string, param: string, reason: string) => string;
16
+ export declare const duplicateParamName: (where: string, name: string, first: number, second: number) => string;
17
+ export declare const notAChatModel: (prefix: string) => string;
18
+ export declare const modelNotBuilt: (prefix: string, cause: unknown) => string;
19
+ export declare const agentNotBootstrapped: (prefix: string) => string;
20
+ export declare const duplicateAgentName: (name: string | undefined) => string;
21
+ export declare const reservedAgentName: (name: string) => string;
22
+ export declare const agentRunFailed: (prefix: string, cause: unknown) => string;
23
+ export declare const modelNeverReplied: (prefix: string, last: string | undefined) => string;
24
+ export declare const modelReplyEmpty: (prefix: string) => string;
25
+ export declare const unknownTool: () => string;
26
+ //# sourceMappingURL=messages.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../lib/errors/messages.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,WAAW,GAAI,OAAO,MAAM,KAAG,MACA,CAAC;AAE7C,eAAO,MAAM,YAAY,GAAI,QAAQ,MAAM,EAAE,UAAU,MAAM,EAAE,KAAG,MAIV,CAAC;AAEzD,eAAO,MAAM,cAAc,GAAI,OAAO,MAAM,KAAG,MAE6B,CAAC;AAE7E,eAAO,MAAM,qBAAqB,GAAI,OAAO,MAAM,KAAG,MAE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