@dudousxd/nestjs-agent 0.1.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 -0
- package/README.md +49 -0
- package/dist/agent-deps.factory-D9BHJJqm.d.cts +173 -0
- package/dist/agent-deps.factory-D9BHJJqm.d.ts +173 -0
- package/dist/durable/index.cjs +1590 -0
- package/dist/durable/index.cjs.map +1 -0
- package/dist/durable/index.d.cts +66 -0
- package/dist/durable/index.d.ts +66 -0
- package/dist/durable/index.js +1562 -0
- package/dist/durable/index.js.map +1 -0
- package/dist/index.cjs +1468 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +195 -0
- package/dist/index.d.ts +195 -0
- package/dist/index.js +1427 -0
- package/dist/index.js.map +1 -0
- package/package.json +80 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1468 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
|
|
22
|
+
// src/index.ts
|
|
23
|
+
var index_exports = {};
|
|
24
|
+
__export(index_exports, {
|
|
25
|
+
AGENT_TOOL_BRAND: () => AGENT_TOOL_BRAND,
|
|
26
|
+
AI_TOOL_METADATA: () => AI_TOOL_METADATA,
|
|
27
|
+
AgentDepsFactory: () => AgentDepsFactory2,
|
|
28
|
+
AgentModule: () => AgentModule,
|
|
29
|
+
AgentService: () => AgentService,
|
|
30
|
+
AiTool: () => AiTool,
|
|
31
|
+
AiToolDiscoveryService: () => AiToolDiscoveryService,
|
|
32
|
+
HeaderActorResolver: () => HeaderActorResolver,
|
|
33
|
+
InProcessTokenStreamSink: () => InProcessTokenStreamSink,
|
|
34
|
+
InlineAgentRunner: () => InlineAgentRunner,
|
|
35
|
+
LedgerQuotaStore: () => LedgerQuotaStore,
|
|
36
|
+
delegateToolName: () => delegateToolName,
|
|
37
|
+
provideAgentTool: () => provideAgentTool,
|
|
38
|
+
readAiToolMetadata: () => readAiToolMetadata,
|
|
39
|
+
utcDay: () => utcDay
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(index_exports);
|
|
42
|
+
|
|
43
|
+
// src/agent.module.ts
|
|
44
|
+
var import_nestjs_agent_core11 = require("@dudousxd/nestjs-agent-core");
|
|
45
|
+
var import_common9 = require("@nestjs/common");
|
|
46
|
+
var import_core2 = require("@nestjs/core");
|
|
47
|
+
|
|
48
|
+
// src/agent-deps.factory.ts
|
|
49
|
+
var import_nestjs_agent_core = require("@dudousxd/nestjs-agent-core");
|
|
50
|
+
var import_nestjs_agent_core2 = require("@dudousxd/nestjs-agent-core");
|
|
51
|
+
var import_common = require("@nestjs/common");
|
|
52
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
53
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
54
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
55
|
+
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;
|
|
56
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
57
|
+
}
|
|
58
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
59
|
+
function _ts_metadata(k, v) {
|
|
60
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
61
|
+
}
|
|
62
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
63
|
+
function _ts_param(paramIndex, decorator) {
|
|
64
|
+
return function(target, key) {
|
|
65
|
+
decorator(target, key, paramIndex);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
__name(_ts_param, "_ts_param");
|
|
69
|
+
function delegateToolName(target) {
|
|
70
|
+
return `ask_${target.replace(/[^a-zA-Z0-9]+/g, "_")}`;
|
|
71
|
+
}
|
|
72
|
+
__name(delegateToolName, "delegateToolName");
|
|
73
|
+
var AgentDepsFactory2 = class {
|
|
74
|
+
static {
|
|
75
|
+
__name(this, "AgentDepsFactory");
|
|
76
|
+
}
|
|
77
|
+
options;
|
|
78
|
+
model;
|
|
79
|
+
store;
|
|
80
|
+
sink;
|
|
81
|
+
rolesPolicy;
|
|
82
|
+
registry;
|
|
83
|
+
agents;
|
|
84
|
+
quota;
|
|
85
|
+
constructor(options, model, store, sink, rolesPolicy, registry, agents, quota) {
|
|
86
|
+
this.options = options;
|
|
87
|
+
this.model = model;
|
|
88
|
+
this.store = store;
|
|
89
|
+
this.sink = sink;
|
|
90
|
+
this.rolesPolicy = rolesPolicy;
|
|
91
|
+
this.registry = registry;
|
|
92
|
+
this.agents = agents;
|
|
93
|
+
this.quota = quota;
|
|
94
|
+
}
|
|
95
|
+
defaultAgentName() {
|
|
96
|
+
return this.options.defaultAgent?.name ?? "default";
|
|
97
|
+
}
|
|
98
|
+
effectiveTools(definition) {
|
|
99
|
+
if (definition === void 0) {
|
|
100
|
+
return void 0;
|
|
101
|
+
}
|
|
102
|
+
const delegated = (definition.delegatesTo ?? []).map(delegateToolName);
|
|
103
|
+
if (definition.tools === void 0 && delegated.length === 0) {
|
|
104
|
+
return void 0;
|
|
105
|
+
}
|
|
106
|
+
return [
|
|
107
|
+
...definition.tools ?? [],
|
|
108
|
+
...delegated
|
|
109
|
+
];
|
|
110
|
+
}
|
|
111
|
+
forAgent(agentName) {
|
|
112
|
+
const name = agentName ?? this.defaultAgentName();
|
|
113
|
+
const definition = this.agents.get(name);
|
|
114
|
+
const personas = /* @__PURE__ */ new Map();
|
|
115
|
+
for (const persona of definition?.personas ?? []) {
|
|
116
|
+
personas.set(persona.id, persona);
|
|
117
|
+
}
|
|
118
|
+
const toolAllowList = this.effectiveTools(definition);
|
|
119
|
+
const followUpsCount = this.followUpsCount();
|
|
120
|
+
const retrieval = this.options.retrieval;
|
|
121
|
+
return {
|
|
122
|
+
model: this.model,
|
|
123
|
+
store: this.store,
|
|
124
|
+
sink: this.sink,
|
|
125
|
+
rolesPolicy: this.rolesPolicy,
|
|
126
|
+
registry: this.registry,
|
|
127
|
+
systemPrompt: definition?.systemPrompt ?? "You are a helpful assistant.",
|
|
128
|
+
maxSteps: definition?.maxSteps ?? 8,
|
|
129
|
+
personas,
|
|
130
|
+
defaultPersona: definition?.defaultPersona ?? "default",
|
|
131
|
+
...definition?.modelId !== void 0 ? {
|
|
132
|
+
modelId: definition.modelId
|
|
133
|
+
} : {},
|
|
134
|
+
...this.quota !== void 0 ? {
|
|
135
|
+
quota: this.quota
|
|
136
|
+
} : {},
|
|
137
|
+
...toolAllowList !== void 0 ? {
|
|
138
|
+
toolAllowList
|
|
139
|
+
} : {},
|
|
140
|
+
...this.options.toolTimeoutMs !== void 0 ? {
|
|
141
|
+
toolTimeoutMs: this.options.toolTimeoutMs
|
|
142
|
+
} : {},
|
|
143
|
+
...followUpsCount !== void 0 ? {
|
|
144
|
+
followUpsCount
|
|
145
|
+
} : {},
|
|
146
|
+
...retrieval?.mode === "inject" ? {
|
|
147
|
+
retriever: retrieval.retriever,
|
|
148
|
+
...retrieval.topK !== void 0 ? {
|
|
149
|
+
retrievalTopK: retrieval.topK
|
|
150
|
+
} : {}
|
|
151
|
+
} : {}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/** Normalize the `followUps` option (`true` → 3, `{ count }` → count) to a number, or undefined. */
|
|
155
|
+
followUpsCount() {
|
|
156
|
+
const followUps = this.options.followUps;
|
|
157
|
+
if (followUps === true) {
|
|
158
|
+
return 3;
|
|
159
|
+
}
|
|
160
|
+
if (typeof followUps === "object") {
|
|
161
|
+
return followUps.count;
|
|
162
|
+
}
|
|
163
|
+
return void 0;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
AgentDepsFactory2 = _ts_decorate([
|
|
167
|
+
(0, import_common.Injectable)(),
|
|
168
|
+
_ts_param(0, (0, import_common.Inject)(import_nestjs_agent_core2.AGENT_OPTIONS)),
|
|
169
|
+
_ts_param(1, (0, import_common.Inject)(import_nestjs_agent_core.AGENT_MODEL)),
|
|
170
|
+
_ts_param(2, (0, import_common.Inject)(import_nestjs_agent_core.AGENT_STORE)),
|
|
171
|
+
_ts_param(3, (0, import_common.Inject)(import_nestjs_agent_core.AGENT_SINK)),
|
|
172
|
+
_ts_param(4, (0, import_common.Inject)(import_nestjs_agent_core.AGENT_ROLES_POLICY)),
|
|
173
|
+
_ts_param(5, (0, import_common.Inject)(import_nestjs_agent_core.AGENT_TOOL_REGISTRY)),
|
|
174
|
+
_ts_param(6, (0, import_common.Inject)(import_nestjs_agent_core.AGENT_REGISTRY)),
|
|
175
|
+
_ts_param(7, (0, import_common.Inject)(import_nestjs_agent_core.AGENT_QUOTA_STORE)),
|
|
176
|
+
_ts_metadata("design:type", Function),
|
|
177
|
+
_ts_metadata("design:paramtypes", [
|
|
178
|
+
typeof AgentModuleOptions === "undefined" ? Object : AgentModuleOptions,
|
|
179
|
+
typeof ModelProvider === "undefined" ? Object : ModelProvider,
|
|
180
|
+
typeof AgentStore === "undefined" ? Object : AgentStore,
|
|
181
|
+
typeof TokenStreamSink === "undefined" ? Object : TokenStreamSink,
|
|
182
|
+
typeof RolesPolicy === "undefined" ? Object : RolesPolicy,
|
|
183
|
+
typeof import_nestjs_agent_core.ToolRegistry === "undefined" ? Object : import_nestjs_agent_core.ToolRegistry,
|
|
184
|
+
typeof import_nestjs_agent_core.AgentRegistry === "undefined" ? Object : import_nestjs_agent_core.AgentRegistry,
|
|
185
|
+
Object
|
|
186
|
+
])
|
|
187
|
+
], AgentDepsFactory2);
|
|
188
|
+
|
|
189
|
+
// src/agent.service.ts
|
|
190
|
+
var import_nestjs_agent_core3 = require("@dudousxd/nestjs-agent-core");
|
|
191
|
+
var import_common2 = require("@nestjs/common");
|
|
192
|
+
|
|
193
|
+
// src/agent-deps.ts
|
|
194
|
+
function utcDay(date = /* @__PURE__ */ new Date()) {
|
|
195
|
+
return date.toISOString().slice(0, 10);
|
|
196
|
+
}
|
|
197
|
+
__name(utcDay, "utcDay");
|
|
198
|
+
function childSinkWriter(writer) {
|
|
199
|
+
return {
|
|
200
|
+
write: /* @__PURE__ */ __name((chunk) => writer.write(chunk), "write"),
|
|
201
|
+
end: /* @__PURE__ */ __name(async () => {
|
|
202
|
+
}, "end"),
|
|
203
|
+
fail: /* @__PURE__ */ __name(async () => {
|
|
204
|
+
}, "fail")
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
__name(childSinkWriter, "childSinkWriter");
|
|
208
|
+
|
|
209
|
+
// src/agent.service.ts
|
|
210
|
+
function _ts_decorate2(decorators, target, key, desc) {
|
|
211
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
212
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
213
|
+
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;
|
|
214
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
215
|
+
}
|
|
216
|
+
__name(_ts_decorate2, "_ts_decorate");
|
|
217
|
+
function _ts_metadata2(k, v) {
|
|
218
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
219
|
+
}
|
|
220
|
+
__name(_ts_metadata2, "_ts_metadata");
|
|
221
|
+
function _ts_param2(paramIndex, decorator) {
|
|
222
|
+
return function(target, key) {
|
|
223
|
+
decorator(target, key, paramIndex);
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
__name(_ts_param2, "_ts_param");
|
|
227
|
+
var AgentService = class {
|
|
228
|
+
static {
|
|
229
|
+
__name(this, "AgentService");
|
|
230
|
+
}
|
|
231
|
+
runner;
|
|
232
|
+
store;
|
|
233
|
+
deps;
|
|
234
|
+
constructor(runner, store, deps) {
|
|
235
|
+
this.runner = runner;
|
|
236
|
+
this.store = store;
|
|
237
|
+
this.deps = deps;
|
|
238
|
+
}
|
|
239
|
+
async chat(params) {
|
|
240
|
+
const agentName = params.agentName ?? this.deps.defaultAgentName();
|
|
241
|
+
let threadId = params.threadId;
|
|
242
|
+
if (threadId === void 0) {
|
|
243
|
+
if (params.regenerate === true) {
|
|
244
|
+
throw new import_common2.BadRequestException("regenerate requires an existing threadId");
|
|
245
|
+
}
|
|
246
|
+
const created = await this.store.createThread({
|
|
247
|
+
actor: params.actor,
|
|
248
|
+
persona: params.personaId ?? this.deps.forAgent(agentName).defaultPersona
|
|
249
|
+
});
|
|
250
|
+
threadId = created.id;
|
|
251
|
+
} else if (params.regenerate === true) {
|
|
252
|
+
await this.assertOwnsThread(params.actor, threadId);
|
|
253
|
+
}
|
|
254
|
+
const persona = this.resolvePersona(agentName, params.personaId);
|
|
255
|
+
const input = {
|
|
256
|
+
threadId,
|
|
257
|
+
actor: params.actor,
|
|
258
|
+
userText: params.message,
|
|
259
|
+
day: utcDay(),
|
|
260
|
+
agentName,
|
|
261
|
+
...params.regenerate === true ? {
|
|
262
|
+
regenerate: true
|
|
263
|
+
} : {},
|
|
264
|
+
...persona !== void 0 ? {
|
|
265
|
+
persona
|
|
266
|
+
} : {},
|
|
267
|
+
...params.pageContext !== void 0 ? {
|
|
268
|
+
pageContext: params.pageContext
|
|
269
|
+
} : {}
|
|
270
|
+
};
|
|
271
|
+
const { runId } = await this.runner.start(input);
|
|
272
|
+
await this.store.setActiveStream(threadId, runId);
|
|
273
|
+
return {
|
|
274
|
+
runId,
|
|
275
|
+
threadId
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
subscribe(runId) {
|
|
279
|
+
return this.deps.forAgent().sink.subscribe(runId);
|
|
280
|
+
}
|
|
281
|
+
async approve(actor, toolCallId) {
|
|
282
|
+
const runId = await this.runForOwnedToolCall(actor, toolCallId);
|
|
283
|
+
return this.runner.signal(runId, toolCallId, {
|
|
284
|
+
approved: true
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
async reject(actor, toolCallId, reason) {
|
|
288
|
+
const runId = await this.runForOwnedToolCall(actor, toolCallId);
|
|
289
|
+
return this.runner.signal(runId, toolCallId, {
|
|
290
|
+
approved: false,
|
|
291
|
+
...reason !== void 0 ? {
|
|
292
|
+
reason
|
|
293
|
+
} : {}
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
async cancel(actor, runId) {
|
|
297
|
+
await this.assertOwnsActiveStream(actor, runId);
|
|
298
|
+
return this.runner.cancel(runId);
|
|
299
|
+
}
|
|
300
|
+
resolvePersona(agentName, id) {
|
|
301
|
+
const deps = this.deps.forAgent(agentName);
|
|
302
|
+
return deps.personas.get(id ?? deps.defaultPersona);
|
|
303
|
+
}
|
|
304
|
+
personaCatalog(agentName) {
|
|
305
|
+
return [
|
|
306
|
+
...this.deps.forAgent(agentName).personas.values()
|
|
307
|
+
].map((persona) => ({
|
|
308
|
+
id: persona.id,
|
|
309
|
+
label: persona.label
|
|
310
|
+
}));
|
|
311
|
+
}
|
|
312
|
+
listThreads(actorRef) {
|
|
313
|
+
return this.store.listThreads(actorRef);
|
|
314
|
+
}
|
|
315
|
+
async getThread(actor, threadId) {
|
|
316
|
+
await this.assertOwnsThread(actor, threadId);
|
|
317
|
+
return this.store.getThread(threadId);
|
|
318
|
+
}
|
|
319
|
+
async deleteThread(actor, threadId) {
|
|
320
|
+
await this.assertOwnsThread(actor, threadId);
|
|
321
|
+
return this.store.softDeleteThread(threadId);
|
|
322
|
+
}
|
|
323
|
+
async forkThread(actor, threadId, fromMessageId) {
|
|
324
|
+
await this.assertOwnsThread(actor, threadId);
|
|
325
|
+
return this.store.forkThread(threadId, fromMessageId);
|
|
326
|
+
}
|
|
327
|
+
async quotaToday(actorRef) {
|
|
328
|
+
return this.store.quotaToday(actorRef, utcDay());
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Authorization seam for thread-scoped endpoints: the caller must own the thread. A missing
|
|
332
|
+
* thread is a 404; someone else's thread is a 403 — one actor never touches another's thread.
|
|
333
|
+
*/
|
|
334
|
+
async assertOwnsThread(actor, threadId) {
|
|
335
|
+
const owner = await this.store.ownerOfThread(threadId);
|
|
336
|
+
if (owner === null) {
|
|
337
|
+
throw new import_common2.NotFoundException(`thread ${threadId} not found`);
|
|
338
|
+
}
|
|
339
|
+
if (owner !== actor.id) {
|
|
340
|
+
throw new import_common2.ForbiddenException("thread belongs to another actor");
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Authorization + routing seam for HITL approve/reject: assert the caller owns the tool call's
|
|
345
|
+
* thread, then resolve the run currently awaiting that call (its thread's active stream). That run
|
|
346
|
+
* is the sub-agent's own child run when the pending call belongs to a delegated agent — the client
|
|
347
|
+
* never knows or supplies a runId; it is derived here from the tool call alone.
|
|
348
|
+
*/
|
|
349
|
+
async runForOwnedToolCall(actor, toolCallId) {
|
|
350
|
+
const owner = await this.store.ownerOfToolCall(toolCallId);
|
|
351
|
+
if (owner === null) {
|
|
352
|
+
throw new import_common2.NotFoundException(`tool call ${toolCallId} not found`);
|
|
353
|
+
}
|
|
354
|
+
if (owner !== actor.id) {
|
|
355
|
+
throw new import_common2.ForbiddenException("tool call belongs to another actor");
|
|
356
|
+
}
|
|
357
|
+
const runId = await this.store.runForToolCall(toolCallId);
|
|
358
|
+
if (runId === null) {
|
|
359
|
+
throw new import_common2.NotFoundException(`tool call ${toolCallId} has no active run to signal`);
|
|
360
|
+
}
|
|
361
|
+
return runId;
|
|
362
|
+
}
|
|
363
|
+
/** Authorization seam for `cancel`: the caller must own the thread currently streaming this run. */
|
|
364
|
+
async assertOwnsActiveStream(actor, runId) {
|
|
365
|
+
const owner = await this.store.ownerOfActiveStream(runId);
|
|
366
|
+
if (owner === null) {
|
|
367
|
+
throw new import_common2.NotFoundException(`no active run ${runId}`);
|
|
368
|
+
}
|
|
369
|
+
if (owner !== actor.id) {
|
|
370
|
+
throw new import_common2.ForbiddenException("run belongs to another actor");
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
AgentService = _ts_decorate2([
|
|
375
|
+
(0, import_common2.Injectable)(),
|
|
376
|
+
_ts_param2(0, (0, import_common2.Inject)(import_nestjs_agent_core3.AGENT_RUNNER)),
|
|
377
|
+
_ts_param2(1, (0, import_common2.Inject)(import_nestjs_agent_core3.AGENT_STORE)),
|
|
378
|
+
_ts_param2(2, (0, import_common2.Inject)(import_nestjs_agent_core3.AGENT_DEPS_FACTORY)),
|
|
379
|
+
_ts_metadata2("design:type", Function),
|
|
380
|
+
_ts_metadata2("design:paramtypes", [
|
|
381
|
+
typeof AgentRunner === "undefined" ? Object : AgentRunner,
|
|
382
|
+
typeof AgentStore === "undefined" ? Object : AgentStore,
|
|
383
|
+
typeof AgentDepsFactory === "undefined" ? Object : AgentDepsFactory
|
|
384
|
+
])
|
|
385
|
+
], AgentService);
|
|
386
|
+
|
|
387
|
+
// src/controller/chat.controller.ts
|
|
388
|
+
var import_nestjs_agent_core4 = require("@dudousxd/nestjs-agent-core");
|
|
389
|
+
var import_common3 = require("@nestjs/common");
|
|
390
|
+
function _ts_decorate3(decorators, target, key, desc) {
|
|
391
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
392
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
393
|
+
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;
|
|
394
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
395
|
+
}
|
|
396
|
+
__name(_ts_decorate3, "_ts_decorate");
|
|
397
|
+
function _ts_metadata3(k, v) {
|
|
398
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
399
|
+
}
|
|
400
|
+
__name(_ts_metadata3, "_ts_metadata");
|
|
401
|
+
function _ts_param3(paramIndex, decorator) {
|
|
402
|
+
return function(target, key) {
|
|
403
|
+
decorator(target, key, paramIndex);
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
__name(_ts_param3, "_ts_param");
|
|
407
|
+
var ChatController = class {
|
|
408
|
+
static {
|
|
409
|
+
__name(this, "ChatController");
|
|
410
|
+
}
|
|
411
|
+
agent;
|
|
412
|
+
actorResolver;
|
|
413
|
+
constructor(agent, actorResolver) {
|
|
414
|
+
this.agent = agent;
|
|
415
|
+
this.actorResolver = actorResolver;
|
|
416
|
+
}
|
|
417
|
+
async chat(req, res, body) {
|
|
418
|
+
const actor = await this.actorResolver.resolve(req);
|
|
419
|
+
const { runId, threadId } = await this.agent.chat({
|
|
420
|
+
actor,
|
|
421
|
+
message: body.message,
|
|
422
|
+
...body.threadId !== void 0 ? {
|
|
423
|
+
threadId: body.threadId
|
|
424
|
+
} : {},
|
|
425
|
+
...body.agent !== void 0 ? {
|
|
426
|
+
agentName: body.agent
|
|
427
|
+
} : {},
|
|
428
|
+
...body.persona !== void 0 ? {
|
|
429
|
+
personaId: body.persona
|
|
430
|
+
} : {},
|
|
431
|
+
...body.pageContext !== void 0 ? {
|
|
432
|
+
pageContext: body.pageContext
|
|
433
|
+
} : {},
|
|
434
|
+
...body.regenerate === true ? {
|
|
435
|
+
regenerate: true
|
|
436
|
+
} : {}
|
|
437
|
+
});
|
|
438
|
+
await this.pipe(res, runId, threadId);
|
|
439
|
+
}
|
|
440
|
+
async stream(runId, res) {
|
|
441
|
+
await this.pipe(res, runId);
|
|
442
|
+
}
|
|
443
|
+
async cancel(req, runId) {
|
|
444
|
+
const actor = await this.actorResolver.resolve(req);
|
|
445
|
+
await this.agent.cancel(actor, runId);
|
|
446
|
+
return {
|
|
447
|
+
aborted: true
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
async pipe(res, runId, threadId) {
|
|
451
|
+
res.setHeader("Content-Type", "text/event-stream");
|
|
452
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
453
|
+
res.setHeader("Connection", "keep-alive");
|
|
454
|
+
res.setHeader("X-Agent-Run-Id", runId);
|
|
455
|
+
if (threadId !== void 0) {
|
|
456
|
+
res.setHeader("X-Agent-Thread-Id", threadId);
|
|
457
|
+
}
|
|
458
|
+
res.write(`event: meta
|
|
459
|
+
data: ${JSON.stringify({
|
|
460
|
+
runId,
|
|
461
|
+
threadId
|
|
462
|
+
})}
|
|
463
|
+
|
|
464
|
+
`);
|
|
465
|
+
const decoder = new TextDecoder();
|
|
466
|
+
try {
|
|
467
|
+
for await (const chunk of this.agent.subscribe(runId)) {
|
|
468
|
+
res.write(`data: ${JSON.stringify({
|
|
469
|
+
delta: decoder.decode(chunk)
|
|
470
|
+
})}
|
|
471
|
+
|
|
472
|
+
`);
|
|
473
|
+
}
|
|
474
|
+
res.write("event: done\ndata: {}\n\n");
|
|
475
|
+
} catch (error) {
|
|
476
|
+
const payload = error instanceof import_nestjs_agent_core4.AgentStreamError ? {
|
|
477
|
+
code: error.code,
|
|
478
|
+
message: error.message
|
|
479
|
+
} : {
|
|
480
|
+
code: "run_failed",
|
|
481
|
+
message: error instanceof Error ? error.message : "stream error"
|
|
482
|
+
};
|
|
483
|
+
res.write(`event: error
|
|
484
|
+
data: ${JSON.stringify(payload)}
|
|
485
|
+
|
|
486
|
+
`);
|
|
487
|
+
}
|
|
488
|
+
res.end();
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
_ts_decorate3([
|
|
492
|
+
(0, import_common3.Post)("chat"),
|
|
493
|
+
_ts_param3(0, (0, import_common3.Req)()),
|
|
494
|
+
_ts_param3(1, (0, import_common3.Res)()),
|
|
495
|
+
_ts_param3(2, (0, import_common3.Body)()),
|
|
496
|
+
_ts_metadata3("design:type", Function),
|
|
497
|
+
_ts_metadata3("design:paramtypes", [
|
|
498
|
+
typeof Request === "undefined" ? Object : Request,
|
|
499
|
+
typeof Response === "undefined" ? Object : Response,
|
|
500
|
+
typeof ChatBody === "undefined" ? Object : ChatBody
|
|
501
|
+
]),
|
|
502
|
+
_ts_metadata3("design:returntype", Promise)
|
|
503
|
+
], ChatController.prototype, "chat", null);
|
|
504
|
+
_ts_decorate3([
|
|
505
|
+
(0, import_common3.Get)("chat/:runId/stream"),
|
|
506
|
+
_ts_param3(0, (0, import_common3.Param)("runId")),
|
|
507
|
+
_ts_param3(1, (0, import_common3.Res)()),
|
|
508
|
+
_ts_metadata3("design:type", Function),
|
|
509
|
+
_ts_metadata3("design:paramtypes", [
|
|
510
|
+
String,
|
|
511
|
+
typeof Response === "undefined" ? Object : Response
|
|
512
|
+
]),
|
|
513
|
+
_ts_metadata3("design:returntype", Promise)
|
|
514
|
+
], ChatController.prototype, "stream", null);
|
|
515
|
+
_ts_decorate3([
|
|
516
|
+
(0, import_common3.Post)("chat/:runId/cancel"),
|
|
517
|
+
_ts_param3(0, (0, import_common3.Req)()),
|
|
518
|
+
_ts_param3(1, (0, import_common3.Param)("runId")),
|
|
519
|
+
_ts_metadata3("design:type", Function),
|
|
520
|
+
_ts_metadata3("design:paramtypes", [
|
|
521
|
+
typeof Request === "undefined" ? Object : Request,
|
|
522
|
+
String
|
|
523
|
+
]),
|
|
524
|
+
_ts_metadata3("design:returntype", Promise)
|
|
525
|
+
], ChatController.prototype, "cancel", null);
|
|
526
|
+
ChatController = _ts_decorate3([
|
|
527
|
+
(0, import_common3.Controller)(),
|
|
528
|
+
_ts_param3(1, (0, import_common3.Inject)(import_nestjs_agent_core4.AGENT_ACTOR_RESOLVER)),
|
|
529
|
+
_ts_metadata3("design:type", Function),
|
|
530
|
+
_ts_metadata3("design:paramtypes", [
|
|
531
|
+
typeof AgentService === "undefined" ? Object : AgentService,
|
|
532
|
+
typeof ActorResolver === "undefined" ? Object : ActorResolver
|
|
533
|
+
])
|
|
534
|
+
], ChatController);
|
|
535
|
+
|
|
536
|
+
// src/controller/quota.controller.ts
|
|
537
|
+
var import_nestjs_agent_core5 = require("@dudousxd/nestjs-agent-core");
|
|
538
|
+
var import_common4 = require("@nestjs/common");
|
|
539
|
+
function _ts_decorate4(decorators, target, key, desc) {
|
|
540
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
541
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
542
|
+
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;
|
|
543
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
544
|
+
}
|
|
545
|
+
__name(_ts_decorate4, "_ts_decorate");
|
|
546
|
+
function _ts_metadata4(k, v) {
|
|
547
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
548
|
+
}
|
|
549
|
+
__name(_ts_metadata4, "_ts_metadata");
|
|
550
|
+
function _ts_param4(paramIndex, decorator) {
|
|
551
|
+
return function(target, key) {
|
|
552
|
+
decorator(target, key, paramIndex);
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
__name(_ts_param4, "_ts_param");
|
|
556
|
+
var QuotaController = class {
|
|
557
|
+
static {
|
|
558
|
+
__name(this, "QuotaController");
|
|
559
|
+
}
|
|
560
|
+
agent;
|
|
561
|
+
actorResolver;
|
|
562
|
+
constructor(agent, actorResolver) {
|
|
563
|
+
this.agent = agent;
|
|
564
|
+
this.actorResolver = actorResolver;
|
|
565
|
+
}
|
|
566
|
+
async today(req) {
|
|
567
|
+
const actor = await this.actorResolver.resolve(req);
|
|
568
|
+
return this.agent.quotaToday(actor.id);
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
_ts_decorate4([
|
|
572
|
+
(0, import_common4.Get)("today"),
|
|
573
|
+
_ts_param4(0, (0, import_common4.Req)()),
|
|
574
|
+
_ts_metadata4("design:type", Function),
|
|
575
|
+
_ts_metadata4("design:paramtypes", [
|
|
576
|
+
typeof Request === "undefined" ? Object : Request
|
|
577
|
+
]),
|
|
578
|
+
_ts_metadata4("design:returntype", Promise)
|
|
579
|
+
], QuotaController.prototype, "today", null);
|
|
580
|
+
QuotaController = _ts_decorate4([
|
|
581
|
+
(0, import_common4.Controller)("quota"),
|
|
582
|
+
_ts_param4(1, (0, import_common4.Inject)(import_nestjs_agent_core5.AGENT_ACTOR_RESOLVER)),
|
|
583
|
+
_ts_metadata4("design:type", Function),
|
|
584
|
+
_ts_metadata4("design:paramtypes", [
|
|
585
|
+
typeof AgentService === "undefined" ? Object : AgentService,
|
|
586
|
+
typeof ActorResolver === "undefined" ? Object : ActorResolver
|
|
587
|
+
])
|
|
588
|
+
], QuotaController);
|
|
589
|
+
|
|
590
|
+
// src/controller/threads.controller.ts
|
|
591
|
+
var import_nestjs_agent_core6 = require("@dudousxd/nestjs-agent-core");
|
|
592
|
+
var import_common5 = require("@nestjs/common");
|
|
593
|
+
function _ts_decorate5(decorators, target, key, desc) {
|
|
594
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
595
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
596
|
+
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;
|
|
597
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
598
|
+
}
|
|
599
|
+
__name(_ts_decorate5, "_ts_decorate");
|
|
600
|
+
function _ts_metadata5(k, v) {
|
|
601
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
602
|
+
}
|
|
603
|
+
__name(_ts_metadata5, "_ts_metadata");
|
|
604
|
+
function _ts_param5(paramIndex, decorator) {
|
|
605
|
+
return function(target, key) {
|
|
606
|
+
decorator(target, key, paramIndex);
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
__name(_ts_param5, "_ts_param");
|
|
610
|
+
var ThreadsController = class {
|
|
611
|
+
static {
|
|
612
|
+
__name(this, "ThreadsController");
|
|
613
|
+
}
|
|
614
|
+
agent;
|
|
615
|
+
actorResolver;
|
|
616
|
+
constructor(agent, actorResolver) {
|
|
617
|
+
this.agent = agent;
|
|
618
|
+
this.actorResolver = actorResolver;
|
|
619
|
+
}
|
|
620
|
+
async list(req) {
|
|
621
|
+
const actor = await this.actorResolver.resolve(req);
|
|
622
|
+
return this.agent.listThreads(actor.id);
|
|
623
|
+
}
|
|
624
|
+
personas() {
|
|
625
|
+
return this.agent.personaCatalog();
|
|
626
|
+
}
|
|
627
|
+
async detail(req, id) {
|
|
628
|
+
const actor = await this.actorResolver.resolve(req);
|
|
629
|
+
return this.agent.getThread(actor, id);
|
|
630
|
+
}
|
|
631
|
+
async remove(req, id) {
|
|
632
|
+
const actor = await this.actorResolver.resolve(req);
|
|
633
|
+
await this.agent.deleteThread(actor, id);
|
|
634
|
+
return {
|
|
635
|
+
ok: true
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
async fork(req, id, messageId) {
|
|
639
|
+
const actor = await this.actorResolver.resolve(req);
|
|
640
|
+
return this.agent.forkThread(actor, id, messageId);
|
|
641
|
+
}
|
|
642
|
+
};
|
|
643
|
+
_ts_decorate5([
|
|
644
|
+
(0, import_common5.Get)(),
|
|
645
|
+
_ts_param5(0, (0, import_common5.Req)()),
|
|
646
|
+
_ts_metadata5("design:type", Function),
|
|
647
|
+
_ts_metadata5("design:paramtypes", [
|
|
648
|
+
typeof Request === "undefined" ? Object : Request
|
|
649
|
+
]),
|
|
650
|
+
_ts_metadata5("design:returntype", Promise)
|
|
651
|
+
], ThreadsController.prototype, "list", null);
|
|
652
|
+
_ts_decorate5([
|
|
653
|
+
(0, import_common5.Get)("personas/catalog"),
|
|
654
|
+
_ts_metadata5("design:type", Function),
|
|
655
|
+
_ts_metadata5("design:paramtypes", []),
|
|
656
|
+
_ts_metadata5("design:returntype", void 0)
|
|
657
|
+
], ThreadsController.prototype, "personas", null);
|
|
658
|
+
_ts_decorate5([
|
|
659
|
+
(0, import_common5.Get)(":id"),
|
|
660
|
+
_ts_param5(0, (0, import_common5.Req)()),
|
|
661
|
+
_ts_param5(1, (0, import_common5.Param)("id")),
|
|
662
|
+
_ts_metadata5("design:type", Function),
|
|
663
|
+
_ts_metadata5("design:paramtypes", [
|
|
664
|
+
typeof Request === "undefined" ? Object : Request,
|
|
665
|
+
String
|
|
666
|
+
]),
|
|
667
|
+
_ts_metadata5("design:returntype", Promise)
|
|
668
|
+
], ThreadsController.prototype, "detail", null);
|
|
669
|
+
_ts_decorate5([
|
|
670
|
+
(0, import_common5.Delete)(":id"),
|
|
671
|
+
_ts_param5(0, (0, import_common5.Req)()),
|
|
672
|
+
_ts_param5(1, (0, import_common5.Param)("id")),
|
|
673
|
+
_ts_metadata5("design:type", Function),
|
|
674
|
+
_ts_metadata5("design:paramtypes", [
|
|
675
|
+
typeof Request === "undefined" ? Object : Request,
|
|
676
|
+
String
|
|
677
|
+
]),
|
|
678
|
+
_ts_metadata5("design:returntype", Promise)
|
|
679
|
+
], ThreadsController.prototype, "remove", null);
|
|
680
|
+
_ts_decorate5([
|
|
681
|
+
(0, import_common5.Post)(":id/fork-from/:messageId"),
|
|
682
|
+
_ts_param5(0, (0, import_common5.Req)()),
|
|
683
|
+
_ts_param5(1, (0, import_common5.Param)("id")),
|
|
684
|
+
_ts_param5(2, (0, import_common5.Param)("messageId")),
|
|
685
|
+
_ts_metadata5("design:type", Function),
|
|
686
|
+
_ts_metadata5("design:paramtypes", [
|
|
687
|
+
typeof Request === "undefined" ? Object : Request,
|
|
688
|
+
String,
|
|
689
|
+
String
|
|
690
|
+
]),
|
|
691
|
+
_ts_metadata5("design:returntype", Promise)
|
|
692
|
+
], ThreadsController.prototype, "fork", null);
|
|
693
|
+
ThreadsController = _ts_decorate5([
|
|
694
|
+
(0, import_common5.Controller)("threads"),
|
|
695
|
+
_ts_param5(1, (0, import_common5.Inject)(import_nestjs_agent_core6.AGENT_ACTOR_RESOLVER)),
|
|
696
|
+
_ts_metadata5("design:type", Function),
|
|
697
|
+
_ts_metadata5("design:paramtypes", [
|
|
698
|
+
typeof AgentService === "undefined" ? Object : AgentService,
|
|
699
|
+
typeof ActorResolver === "undefined" ? Object : ActorResolver
|
|
700
|
+
])
|
|
701
|
+
], ThreadsController);
|
|
702
|
+
|
|
703
|
+
// src/controller/tool-call.controller.ts
|
|
704
|
+
var import_nestjs_agent_core7 = require("@dudousxd/nestjs-agent-core");
|
|
705
|
+
var import_common6 = require("@nestjs/common");
|
|
706
|
+
function _ts_decorate6(decorators, target, key, desc) {
|
|
707
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
708
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
709
|
+
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;
|
|
710
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
711
|
+
}
|
|
712
|
+
__name(_ts_decorate6, "_ts_decorate");
|
|
713
|
+
function _ts_metadata6(k, v) {
|
|
714
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
715
|
+
}
|
|
716
|
+
__name(_ts_metadata6, "_ts_metadata");
|
|
717
|
+
function _ts_param6(paramIndex, decorator) {
|
|
718
|
+
return function(target, key) {
|
|
719
|
+
decorator(target, key, paramIndex);
|
|
720
|
+
};
|
|
721
|
+
}
|
|
722
|
+
__name(_ts_param6, "_ts_param");
|
|
723
|
+
var ToolCallController = class {
|
|
724
|
+
static {
|
|
725
|
+
__name(this, "ToolCallController");
|
|
726
|
+
}
|
|
727
|
+
agent;
|
|
728
|
+
actorResolver;
|
|
729
|
+
constructor(agent, actorResolver) {
|
|
730
|
+
this.agent = agent;
|
|
731
|
+
this.actorResolver = actorResolver;
|
|
732
|
+
}
|
|
733
|
+
async approve(req, body) {
|
|
734
|
+
const actor = await this.actorResolver.resolve(req);
|
|
735
|
+
await this.agent.approve(actor, body.toolCallId);
|
|
736
|
+
return {
|
|
737
|
+
ok: true
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
async reject(req, body) {
|
|
741
|
+
const actor = await this.actorResolver.resolve(req);
|
|
742
|
+
await this.agent.reject(actor, body.toolCallId, body.reason);
|
|
743
|
+
return {
|
|
744
|
+
ok: true
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
};
|
|
748
|
+
_ts_decorate6([
|
|
749
|
+
(0, import_common6.Post)("approve"),
|
|
750
|
+
_ts_param6(0, (0, import_common6.Req)()),
|
|
751
|
+
_ts_param6(1, (0, import_common6.Body)()),
|
|
752
|
+
_ts_metadata6("design:type", Function),
|
|
753
|
+
_ts_metadata6("design:paramtypes", [
|
|
754
|
+
typeof Request === "undefined" ? Object : Request,
|
|
755
|
+
typeof ApproveBody === "undefined" ? Object : ApproveBody
|
|
756
|
+
]),
|
|
757
|
+
_ts_metadata6("design:returntype", Promise)
|
|
758
|
+
], ToolCallController.prototype, "approve", null);
|
|
759
|
+
_ts_decorate6([
|
|
760
|
+
(0, import_common6.Post)("reject"),
|
|
761
|
+
_ts_param6(0, (0, import_common6.Req)()),
|
|
762
|
+
_ts_param6(1, (0, import_common6.Body)()),
|
|
763
|
+
_ts_metadata6("design:type", Function),
|
|
764
|
+
_ts_metadata6("design:paramtypes", [
|
|
765
|
+
typeof Request === "undefined" ? Object : Request,
|
|
766
|
+
typeof RejectBody === "undefined" ? Object : RejectBody
|
|
767
|
+
]),
|
|
768
|
+
_ts_metadata6("design:returntype", Promise)
|
|
769
|
+
], ToolCallController.prototype, "reject", null);
|
|
770
|
+
ToolCallController = _ts_decorate6([
|
|
771
|
+
(0, import_common6.Controller)("tool-call"),
|
|
772
|
+
_ts_param6(1, (0, import_common6.Inject)(import_nestjs_agent_core7.AGENT_ACTOR_RESOLVER)),
|
|
773
|
+
_ts_metadata6("design:type", Function),
|
|
774
|
+
_ts_metadata6("design:paramtypes", [
|
|
775
|
+
typeof AgentService === "undefined" ? Object : AgentService,
|
|
776
|
+
typeof ActorResolver === "undefined" ? Object : ActorResolver
|
|
777
|
+
])
|
|
778
|
+
], ToolCallController);
|
|
779
|
+
|
|
780
|
+
// src/discovery/ai-tool-discovery.service.ts
|
|
781
|
+
var import_nestjs_agent_core8 = require("@dudousxd/nestjs-agent-core");
|
|
782
|
+
var import_common7 = require("@nestjs/common");
|
|
783
|
+
var import_core = require("@nestjs/core");
|
|
784
|
+
var import_zod = require("zod");
|
|
785
|
+
|
|
786
|
+
// src/decorator/ai-tool.decorator.ts
|
|
787
|
+
var import_reflect_metadata = require("reflect-metadata");
|
|
788
|
+
var AI_TOOL_METADATA = Symbol("nestjs-agent:ai-tool");
|
|
789
|
+
function AiTool(options) {
|
|
790
|
+
return (target) => {
|
|
791
|
+
Reflect.defineMetadata(AI_TOOL_METADATA, options, target);
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
__name(AiTool, "AiTool");
|
|
795
|
+
function readAiToolMetadata(target) {
|
|
796
|
+
return Reflect.getMetadata(AI_TOOL_METADATA, target.constructor);
|
|
797
|
+
}
|
|
798
|
+
__name(readAiToolMetadata, "readAiToolMetadata");
|
|
799
|
+
|
|
800
|
+
// src/functional-tool.ts
|
|
801
|
+
var AGENT_TOOL_BRAND = Symbol.for("@dudousxd/nestjs-agent:functional-tool");
|
|
802
|
+
function brand(tool) {
|
|
803
|
+
return {
|
|
804
|
+
[AGENT_TOOL_BRAND]: true,
|
|
805
|
+
spec: tool.spec,
|
|
806
|
+
handler: tool.handler
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
__name(brand, "brand");
|
|
810
|
+
function isBrandedFunctionalTool(value) {
|
|
811
|
+
return AGENT_TOOL_BRAND in value && "spec" in value && "handler" in value;
|
|
812
|
+
}
|
|
813
|
+
__name(isBrandedFunctionalTool, "isBrandedFunctionalTool");
|
|
814
|
+
function provideAgentTool(factoryOrTool, inject) {
|
|
815
|
+
const provide = Symbol("nestjs-agent:functional-tool-instance");
|
|
816
|
+
if (typeof factoryOrTool === "function") {
|
|
817
|
+
return {
|
|
818
|
+
provide,
|
|
819
|
+
useFactory: /* @__PURE__ */ __name((...deps) => brand(factoryOrTool(...deps)), "useFactory"),
|
|
820
|
+
inject: inject ?? []
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
return {
|
|
824
|
+
provide,
|
|
825
|
+
useValue: brand(factoryOrTool)
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
__name(provideAgentTool, "provideAgentTool");
|
|
829
|
+
|
|
830
|
+
// src/discovery/ai-tool-discovery.service.ts
|
|
831
|
+
function _ts_decorate7(decorators, target, key, desc) {
|
|
832
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
833
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
834
|
+
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;
|
|
835
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
836
|
+
}
|
|
837
|
+
__name(_ts_decorate7, "_ts_decorate");
|
|
838
|
+
function _ts_metadata7(k, v) {
|
|
839
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
840
|
+
}
|
|
841
|
+
__name(_ts_metadata7, "_ts_metadata");
|
|
842
|
+
function _ts_param7(paramIndex, decorator) {
|
|
843
|
+
return function(target, key) {
|
|
844
|
+
decorator(target, key, paramIndex);
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
__name(_ts_param7, "_ts_param");
|
|
848
|
+
var AiToolDiscoveryService = class _AiToolDiscoveryService {
|
|
849
|
+
static {
|
|
850
|
+
__name(this, "AiToolDiscoveryService");
|
|
851
|
+
}
|
|
852
|
+
discovery;
|
|
853
|
+
registry;
|
|
854
|
+
agents;
|
|
855
|
+
options;
|
|
856
|
+
logger = new import_common7.Logger(_AiToolDiscoveryService.name);
|
|
857
|
+
constructor(discovery, registry, agents, options) {
|
|
858
|
+
this.discovery = discovery;
|
|
859
|
+
this.registry = registry;
|
|
860
|
+
this.agents = agents;
|
|
861
|
+
this.options = options;
|
|
862
|
+
}
|
|
863
|
+
onApplicationBootstrap() {
|
|
864
|
+
let count = 0;
|
|
865
|
+
let functional = 0;
|
|
866
|
+
for (const wrapper of this.discovery.getProviders()) {
|
|
867
|
+
const instance = wrapper.instance;
|
|
868
|
+
if (instance === null || instance === void 0 || typeof instance !== "object") {
|
|
869
|
+
continue;
|
|
870
|
+
}
|
|
871
|
+
if (isBrandedFunctionalTool(instance)) {
|
|
872
|
+
if (this.registerFunctionalTool(instance)) {
|
|
873
|
+
functional += 1;
|
|
874
|
+
}
|
|
875
|
+
continue;
|
|
876
|
+
}
|
|
877
|
+
const meta = readAiToolMetadata(instance);
|
|
878
|
+
if (meta === void 0) {
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
const handler = instance;
|
|
882
|
+
if (typeof handler.execute !== "function") {
|
|
883
|
+
this.logger.warn(`@AiTool "${meta.name}" has no execute() method \u2014 skipped`);
|
|
884
|
+
continue;
|
|
885
|
+
}
|
|
886
|
+
this.registry.register({
|
|
887
|
+
name: meta.name,
|
|
888
|
+
kind: meta.kind,
|
|
889
|
+
description: meta.description,
|
|
890
|
+
inputSchema: meta.input,
|
|
891
|
+
// Leave roles unset when the tool didn't declare any — the RolesPolicy owns the default
|
|
892
|
+
// (its `defaultRoles`). Baking it in here too would leak it into custom policies.
|
|
893
|
+
...meta.roles !== void 0 ? {
|
|
894
|
+
roles: meta.roles
|
|
895
|
+
} : {},
|
|
896
|
+
...meta.ability !== void 0 ? {
|
|
897
|
+
ability: meta.ability
|
|
898
|
+
} : {}
|
|
899
|
+
}, {
|
|
900
|
+
execute: /* @__PURE__ */ __name((input, ctx) => instance.execute(input, ctx), "execute")
|
|
901
|
+
});
|
|
902
|
+
count += 1;
|
|
903
|
+
}
|
|
904
|
+
for (const tool of this.options.tools ?? []) {
|
|
905
|
+
if (this.registerFunctionalTool(tool)) {
|
|
906
|
+
functional += 1;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
let delegates = 0;
|
|
910
|
+
for (const definition of this.agents.list()) {
|
|
911
|
+
for (const target of definition.delegatesTo ?? []) {
|
|
912
|
+
const targetDefinition = this.agents.get(target);
|
|
913
|
+
if (targetDefinition === void 0) {
|
|
914
|
+
throw new Error(`Agent "${definition.name}" delegatesTo "${target}", which is not a registered agent. Register it via AgentModule.forFeature([...]) or fix the name.`);
|
|
915
|
+
}
|
|
916
|
+
const name = delegateToolName(target);
|
|
917
|
+
if (this.registry.has(name)) {
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
const targetBlurb = typeof targetDefinition.systemPrompt === "string" ? ` It is: ${targetDefinition.systemPrompt}` : "";
|
|
921
|
+
this.registry.register(
|
|
922
|
+
{
|
|
923
|
+
name,
|
|
924
|
+
kind: "agent",
|
|
925
|
+
targetAgent: target,
|
|
926
|
+
description: `Delegate a task to the "${target}" agent and get its answer.${targetBlurb}`,
|
|
927
|
+
inputSchema: import_zod.z.object({
|
|
928
|
+
task: import_zod.z.string()
|
|
929
|
+
})
|
|
930
|
+
},
|
|
931
|
+
// Loop-handled (kind 'agent'); the handler is never called.
|
|
932
|
+
{
|
|
933
|
+
execute: /* @__PURE__ */ __name(async () => ({}), "execute")
|
|
934
|
+
}
|
|
935
|
+
);
|
|
936
|
+
delegates += 1;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
this.logger.log(`registered ${count} AI tool(s), ${functional} functional tool(s) and ${delegates} delegate tool(s)`);
|
|
940
|
+
}
|
|
941
|
+
/**
|
|
942
|
+
* Registers a functional tool's `{ spec, handler }`, skipping it if a tool of the same name is
|
|
943
|
+
* already registered. Returns whether it was newly registered.
|
|
944
|
+
*/
|
|
945
|
+
registerFunctionalTool(tool) {
|
|
946
|
+
if (this.registry.has(tool.spec.name)) {
|
|
947
|
+
this.logger.warn(`functional tool "${tool.spec.name}" duplicates a registered name \u2014 skipped`);
|
|
948
|
+
return false;
|
|
949
|
+
}
|
|
950
|
+
this.registry.register(tool.spec, tool.handler);
|
|
951
|
+
return true;
|
|
952
|
+
}
|
|
953
|
+
};
|
|
954
|
+
AiToolDiscoveryService = _ts_decorate7([
|
|
955
|
+
(0, import_common7.Injectable)(),
|
|
956
|
+
_ts_param7(1, (0, import_common7.Inject)(import_nestjs_agent_core8.AGENT_TOOL_REGISTRY)),
|
|
957
|
+
_ts_param7(2, (0, import_common7.Inject)(import_nestjs_agent_core8.AGENT_REGISTRY)),
|
|
958
|
+
_ts_param7(3, (0, import_common7.Inject)(import_nestjs_agent_core8.AGENT_OPTIONS)),
|
|
959
|
+
_ts_metadata7("design:type", Function),
|
|
960
|
+
_ts_metadata7("design:paramtypes", [
|
|
961
|
+
typeof import_core.DiscoveryService === "undefined" ? Object : import_core.DiscoveryService,
|
|
962
|
+
typeof ToolRegistry === "undefined" ? Object : ToolRegistry,
|
|
963
|
+
typeof AgentRegistry === "undefined" ? Object : AgentRegistry,
|
|
964
|
+
typeof AgentModuleOptions === "undefined" ? Object : AgentModuleOptions
|
|
965
|
+
])
|
|
966
|
+
], AiToolDiscoveryService);
|
|
967
|
+
|
|
968
|
+
// src/in-process-sink.ts
|
|
969
|
+
var import_nestjs_agent_core9 = require("@dudousxd/nestjs-agent-core");
|
|
970
|
+
var InProcessTokenStreamSink = class {
|
|
971
|
+
static {
|
|
972
|
+
__name(this, "InProcessTokenStreamSink");
|
|
973
|
+
}
|
|
974
|
+
runs = /* @__PURE__ */ new Map();
|
|
975
|
+
buffer(runId) {
|
|
976
|
+
let buf = this.runs.get(runId);
|
|
977
|
+
if (buf === void 0) {
|
|
978
|
+
buf = {
|
|
979
|
+
chunks: [],
|
|
980
|
+
ended: false,
|
|
981
|
+
notify: /* @__PURE__ */ new Set()
|
|
982
|
+
};
|
|
983
|
+
this.runs.set(runId, buf);
|
|
984
|
+
}
|
|
985
|
+
return buf;
|
|
986
|
+
}
|
|
987
|
+
wake(buf) {
|
|
988
|
+
for (const resolve of buf.notify) {
|
|
989
|
+
resolve();
|
|
990
|
+
}
|
|
991
|
+
buf.notify.clear();
|
|
992
|
+
}
|
|
993
|
+
open(runId) {
|
|
994
|
+
const buf = this.buffer(runId);
|
|
995
|
+
return {
|
|
996
|
+
write: /* @__PURE__ */ __name((chunk) => {
|
|
997
|
+
buf.chunks.push(chunk);
|
|
998
|
+
this.wake(buf);
|
|
999
|
+
}, "write"),
|
|
1000
|
+
end: /* @__PURE__ */ __name(() => {
|
|
1001
|
+
buf.ended = true;
|
|
1002
|
+
this.wake(buf);
|
|
1003
|
+
}, "end"),
|
|
1004
|
+
fail: /* @__PURE__ */ __name((error) => {
|
|
1005
|
+
buf.failure = error;
|
|
1006
|
+
buf.ended = true;
|
|
1007
|
+
this.wake(buf);
|
|
1008
|
+
}, "fail")
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
async *subscribe(runId) {
|
|
1012
|
+
const buf = this.buffer(runId);
|
|
1013
|
+
let index = 0;
|
|
1014
|
+
while (true) {
|
|
1015
|
+
while (index < buf.chunks.length) {
|
|
1016
|
+
const chunk = buf.chunks[index];
|
|
1017
|
+
index += 1;
|
|
1018
|
+
if (chunk !== void 0) {
|
|
1019
|
+
yield chunk;
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
if (buf.ended) {
|
|
1023
|
+
if (buf.failure !== void 0) {
|
|
1024
|
+
throw new import_nestjs_agent_core9.AgentStreamError(buf.failure);
|
|
1025
|
+
}
|
|
1026
|
+
return;
|
|
1027
|
+
}
|
|
1028
|
+
await new Promise((resolve) => buf.notify.add(resolve));
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
close(runId) {
|
|
1032
|
+
this.runs.delete(runId);
|
|
1033
|
+
}
|
|
1034
|
+
};
|
|
1035
|
+
|
|
1036
|
+
// src/ledger-quota-store.ts
|
|
1037
|
+
var LedgerQuotaStore = class {
|
|
1038
|
+
static {
|
|
1039
|
+
__name(this, "LedgerQuotaStore");
|
|
1040
|
+
}
|
|
1041
|
+
store;
|
|
1042
|
+
limitTokens;
|
|
1043
|
+
constructor(store, limitTokens) {
|
|
1044
|
+
this.store = store;
|
|
1045
|
+
this.limitTokens = limitTokens;
|
|
1046
|
+
}
|
|
1047
|
+
async check(actorRef, day) {
|
|
1048
|
+
const { usedTokens } = await this.store.quotaToday(actorRef, day);
|
|
1049
|
+
return {
|
|
1050
|
+
usedTokens,
|
|
1051
|
+
limitTokens: this.limitTokens,
|
|
1052
|
+
withinLimit: usedTokens < this.limitTokens
|
|
1053
|
+
};
|
|
1054
|
+
}
|
|
1055
|
+
async bump() {
|
|
1056
|
+
}
|
|
1057
|
+
};
|
|
1058
|
+
|
|
1059
|
+
// src/runner/inline-agent-runner.ts
|
|
1060
|
+
var import_nestjs_agent_core10 = require("@dudousxd/nestjs-agent-core");
|
|
1061
|
+
var import_common8 = require("@nestjs/common");
|
|
1062
|
+
function _ts_decorate8(decorators, target, key, desc) {
|
|
1063
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1064
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
1065
|
+
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;
|
|
1066
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
1067
|
+
}
|
|
1068
|
+
__name(_ts_decorate8, "_ts_decorate");
|
|
1069
|
+
function _ts_metadata8(k, v) {
|
|
1070
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
1071
|
+
}
|
|
1072
|
+
__name(_ts_metadata8, "_ts_metadata");
|
|
1073
|
+
function _ts_param8(paramIndex, decorator) {
|
|
1074
|
+
return function(target, key) {
|
|
1075
|
+
decorator(target, key, paramIndex);
|
|
1076
|
+
};
|
|
1077
|
+
}
|
|
1078
|
+
__name(_ts_param8, "_ts_param");
|
|
1079
|
+
var InlineAgentRunner = class _InlineAgentRunner {
|
|
1080
|
+
static {
|
|
1081
|
+
__name(this, "InlineAgentRunner");
|
|
1082
|
+
}
|
|
1083
|
+
factory;
|
|
1084
|
+
store;
|
|
1085
|
+
logger = new import_common8.Logger(_InlineAgentRunner.name);
|
|
1086
|
+
pending = /* @__PURE__ */ new Map();
|
|
1087
|
+
constructor(factory, store) {
|
|
1088
|
+
this.factory = factory;
|
|
1089
|
+
this.store = store;
|
|
1090
|
+
}
|
|
1091
|
+
async start(input) {
|
|
1092
|
+
const runId = crypto.randomUUID();
|
|
1093
|
+
const day = input.day ?? utcDay();
|
|
1094
|
+
const deps = this.factory.forAgent(input.agentName);
|
|
1095
|
+
const hooks = this.topLevelHooks(runId, deps, input.actor, day);
|
|
1096
|
+
void (0, import_nestjs_agent_core10.runAgentLoop)({
|
|
1097
|
+
...deps,
|
|
1098
|
+
day
|
|
1099
|
+
}, input, hooks).catch(async (error) => {
|
|
1100
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1101
|
+
const code = error instanceof import_nestjs_agent_core10.QuotaExceededError ? "quota_exceeded" : "run_failed";
|
|
1102
|
+
this.logger.error(`agent run ${runId} failed (${code}): ${message}`);
|
|
1103
|
+
(0, import_nestjs_agent_core10.publishAgentRunFailed)({
|
|
1104
|
+
runId,
|
|
1105
|
+
code,
|
|
1106
|
+
message
|
|
1107
|
+
});
|
|
1108
|
+
const writer = await deps.sink.open(runId);
|
|
1109
|
+
await writer.fail({
|
|
1110
|
+
code,
|
|
1111
|
+
message
|
|
1112
|
+
});
|
|
1113
|
+
});
|
|
1114
|
+
return {
|
|
1115
|
+
runId
|
|
1116
|
+
};
|
|
1117
|
+
}
|
|
1118
|
+
async signal(runId, toolCallId, decision) {
|
|
1119
|
+
const key = `${runId}:${toolCallId}`;
|
|
1120
|
+
const resolve = this.pending.get(key);
|
|
1121
|
+
if (resolve !== void 0) {
|
|
1122
|
+
this.pending.delete(key);
|
|
1123
|
+
resolve(decision);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
async cancel(runId) {
|
|
1127
|
+
const deps = this.factory.forAgent();
|
|
1128
|
+
const writer = await deps.sink.open(runId);
|
|
1129
|
+
await writer.end();
|
|
1130
|
+
}
|
|
1131
|
+
topLevelHooks(runId, deps, actor, day) {
|
|
1132
|
+
return {
|
|
1133
|
+
runId,
|
|
1134
|
+
openSink: /* @__PURE__ */ __name(() => deps.sink.open(runId), "openSink"),
|
|
1135
|
+
awaitApproval: /* @__PURE__ */ __name((call) => new Promise((resolve) => {
|
|
1136
|
+
this.pending.set(`${runId}:${call.id}`, resolve);
|
|
1137
|
+
}), "awaitApproval"),
|
|
1138
|
+
step: /* @__PURE__ */ __name((_name, fn) => fn(), "step"),
|
|
1139
|
+
runAgent: /* @__PURE__ */ __name((agentName, task) => this.runNested(agentName, task, actor, day, 1, runId), "runAgent")
|
|
1140
|
+
};
|
|
1141
|
+
}
|
|
1142
|
+
/**
|
|
1143
|
+
* Delegate to another agent as a nested in-process run. The sub-agent streams into `sinkRunId`
|
|
1144
|
+
* (the top-level run the human is watching) so its output and any pending action tool are visible,
|
|
1145
|
+
* and its own approvals resolve through the shared pending map keyed by its own runId.
|
|
1146
|
+
*/
|
|
1147
|
+
async runNested(agentName, task, actor, day, depth, sinkRunId) {
|
|
1148
|
+
const subThread = await this.store.createThread({
|
|
1149
|
+
actor,
|
|
1150
|
+
persona: "default",
|
|
1151
|
+
transient: true
|
|
1152
|
+
});
|
|
1153
|
+
const runId = crypto.randomUUID();
|
|
1154
|
+
await this.store.setActiveStream(subThread.id, runId);
|
|
1155
|
+
const deps = this.factory.forAgent(agentName);
|
|
1156
|
+
const hooks = {
|
|
1157
|
+
runId,
|
|
1158
|
+
// Forward into the top-level stream; the top-level run owns end/fail on that shared sink.
|
|
1159
|
+
openSink: /* @__PURE__ */ __name(async () => childSinkWriter(await deps.sink.open(sinkRunId)), "openSink"),
|
|
1160
|
+
awaitApproval: /* @__PURE__ */ __name((call) => new Promise((resolve) => {
|
|
1161
|
+
this.pending.set(`${runId}:${call.id}`, resolve);
|
|
1162
|
+
}), "awaitApproval"),
|
|
1163
|
+
step: /* @__PURE__ */ __name((_name, fn) => fn(), "step"),
|
|
1164
|
+
runAgent: /* @__PURE__ */ __name((childName, childTask) => this.runNested(childName, childTask, actor, day, depth + 1, sinkRunId), "runAgent")
|
|
1165
|
+
};
|
|
1166
|
+
return (0, import_nestjs_agent_core10.runAgentLoop)({
|
|
1167
|
+
...deps,
|
|
1168
|
+
day
|
|
1169
|
+
}, {
|
|
1170
|
+
threadId: subThread.id,
|
|
1171
|
+
actor,
|
|
1172
|
+
userText: task,
|
|
1173
|
+
agentName,
|
|
1174
|
+
day,
|
|
1175
|
+
delegationDepth: depth,
|
|
1176
|
+
sinkRunId
|
|
1177
|
+
}, hooks);
|
|
1178
|
+
}
|
|
1179
|
+
};
|
|
1180
|
+
InlineAgentRunner = _ts_decorate8([
|
|
1181
|
+
(0, import_common8.Injectable)(),
|
|
1182
|
+
_ts_param8(0, (0, import_common8.Inject)(import_nestjs_agent_core10.AGENT_DEPS_FACTORY)),
|
|
1183
|
+
_ts_param8(1, (0, import_common8.Inject)(import_nestjs_agent_core10.AGENT_STORE)),
|
|
1184
|
+
_ts_metadata8("design:type", Function),
|
|
1185
|
+
_ts_metadata8("design:paramtypes", [
|
|
1186
|
+
typeof AgentDepsFactory === "undefined" ? Object : AgentDepsFactory,
|
|
1187
|
+
typeof AgentStore === "undefined" ? Object : AgentStore
|
|
1188
|
+
])
|
|
1189
|
+
], InlineAgentRunner);
|
|
1190
|
+
|
|
1191
|
+
// src/agent.module.ts
|
|
1192
|
+
function _ts_decorate9(decorators, target, key, desc) {
|
|
1193
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1194
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
1195
|
+
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;
|
|
1196
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
1197
|
+
}
|
|
1198
|
+
__name(_ts_decorate9, "_ts_decorate");
|
|
1199
|
+
var FEATURE_INIT = Symbol("nestjs-agent:feature-init");
|
|
1200
|
+
var DEFAULT_PATH = "agent";
|
|
1201
|
+
function defaultDefinition(options) {
|
|
1202
|
+
return {
|
|
1203
|
+
name: "default",
|
|
1204
|
+
...options.defaultAgent
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
__name(defaultDefinition, "defaultDefinition");
|
|
1208
|
+
function sharedProviders(durable, includeStore) {
|
|
1209
|
+
const providers = [
|
|
1210
|
+
{
|
|
1211
|
+
provide: import_nestjs_agent_core11.AGENT_TOOL_REGISTRY,
|
|
1212
|
+
useFactory: /* @__PURE__ */ __name(() => new import_nestjs_agent_core11.ToolRegistry(), "useFactory")
|
|
1213
|
+
},
|
|
1214
|
+
{
|
|
1215
|
+
provide: import_nestjs_agent_core11.AGENT_REGISTRY,
|
|
1216
|
+
useFactory: /* @__PURE__ */ __name((options) => {
|
|
1217
|
+
const registry = new import_nestjs_agent_core11.AgentRegistry();
|
|
1218
|
+
registry.register(defaultDefinition(options));
|
|
1219
|
+
return registry;
|
|
1220
|
+
}, "useFactory"),
|
|
1221
|
+
inject: [
|
|
1222
|
+
import_nestjs_agent_core11.AGENT_OPTIONS
|
|
1223
|
+
]
|
|
1224
|
+
},
|
|
1225
|
+
{
|
|
1226
|
+
provide: import_nestjs_agent_core11.AGENT_SINK,
|
|
1227
|
+
useFactory: /* @__PURE__ */ __name((options) => options.sink ?? new InProcessTokenStreamSink(), "useFactory"),
|
|
1228
|
+
inject: [
|
|
1229
|
+
import_nestjs_agent_core11.AGENT_OPTIONS
|
|
1230
|
+
]
|
|
1231
|
+
},
|
|
1232
|
+
{
|
|
1233
|
+
provide: import_nestjs_agent_core11.AGENT_ROLES_POLICY,
|
|
1234
|
+
useFactory: /* @__PURE__ */ __name((options) => options.rolesPolicy ?? new import_nestjs_agent_core11.DefaultRolesPolicy(options.defaultRoles), "useFactory"),
|
|
1235
|
+
inject: [
|
|
1236
|
+
import_nestjs_agent_core11.AGENT_OPTIONS
|
|
1237
|
+
]
|
|
1238
|
+
},
|
|
1239
|
+
{
|
|
1240
|
+
provide: import_nestjs_agent_core11.AGENT_ACTOR_RESOLVER,
|
|
1241
|
+
useFactory: /* @__PURE__ */ __name((options) => options.actorResolver, "useFactory"),
|
|
1242
|
+
inject: [
|
|
1243
|
+
import_nestjs_agent_core11.AGENT_OPTIONS
|
|
1244
|
+
]
|
|
1245
|
+
},
|
|
1246
|
+
{
|
|
1247
|
+
provide: import_nestjs_agent_core11.AGENT_MODEL,
|
|
1248
|
+
useFactory: /* @__PURE__ */ __name((o) => o.model, "useFactory"),
|
|
1249
|
+
inject: [
|
|
1250
|
+
import_nestjs_agent_core11.AGENT_OPTIONS
|
|
1251
|
+
]
|
|
1252
|
+
},
|
|
1253
|
+
{
|
|
1254
|
+
// An explicit `quota` wins; otherwise `quotaLimitTokens` binds the built-in ledger-backed
|
|
1255
|
+
// store; otherwise quotas are off (undefined). The ledger store reads the same usage rows
|
|
1256
|
+
// the loop already writes, so enforcement needs no extra shared state across replicas.
|
|
1257
|
+
provide: import_nestjs_agent_core11.AGENT_QUOTA_STORE,
|
|
1258
|
+
useFactory: /* @__PURE__ */ __name((o, store) => o.quota ?? (o.quotaLimitTokens !== void 0 ? new LedgerQuotaStore(store, o.quotaLimitTokens) : void 0), "useFactory"),
|
|
1259
|
+
inject: [
|
|
1260
|
+
import_nestjs_agent_core11.AGENT_OPTIONS,
|
|
1261
|
+
import_nestjs_agent_core11.AGENT_STORE
|
|
1262
|
+
]
|
|
1263
|
+
},
|
|
1264
|
+
AgentDepsFactory2,
|
|
1265
|
+
{
|
|
1266
|
+
provide: import_nestjs_agent_core11.AGENT_DEPS_FACTORY,
|
|
1267
|
+
useExisting: AgentDepsFactory2
|
|
1268
|
+
},
|
|
1269
|
+
AiToolDiscoveryService,
|
|
1270
|
+
InlineAgentRunner,
|
|
1271
|
+
AgentService
|
|
1272
|
+
];
|
|
1273
|
+
if (includeStore) {
|
|
1274
|
+
providers.push({
|
|
1275
|
+
provide: import_nestjs_agent_core11.AGENT_STORE,
|
|
1276
|
+
useFactory: /* @__PURE__ */ __name((o) => o.store, "useFactory"),
|
|
1277
|
+
inject: [
|
|
1278
|
+
import_nestjs_agent_core11.AGENT_OPTIONS
|
|
1279
|
+
]
|
|
1280
|
+
});
|
|
1281
|
+
}
|
|
1282
|
+
if (durable) {
|
|
1283
|
+
providers.push({
|
|
1284
|
+
provide: import_nestjs_agent_core11.AGENT_RUNNER,
|
|
1285
|
+
useFactory: /* @__PURE__ */ __name((durableRunner) => {
|
|
1286
|
+
if (durableRunner === void 0) {
|
|
1287
|
+
throw new Error("AgentModule.forRoot({ durable: true }) requires importing AgentDurableModule from '@dudousxd/nestjs-agent/durable' (alongside a configured DurableModule). It was not found.");
|
|
1288
|
+
}
|
|
1289
|
+
return durableRunner;
|
|
1290
|
+
}, "useFactory"),
|
|
1291
|
+
inject: [
|
|
1292
|
+
{
|
|
1293
|
+
token: import_nestjs_agent_core11.AGENT_DURABLE_RUNNER,
|
|
1294
|
+
optional: true
|
|
1295
|
+
}
|
|
1296
|
+
]
|
|
1297
|
+
});
|
|
1298
|
+
} else {
|
|
1299
|
+
providers.push({
|
|
1300
|
+
provide: import_nestjs_agent_core11.AGENT_RUNNER,
|
|
1301
|
+
useExisting: InlineAgentRunner
|
|
1302
|
+
});
|
|
1303
|
+
}
|
|
1304
|
+
return providers;
|
|
1305
|
+
}
|
|
1306
|
+
__name(sharedProviders, "sharedProviders");
|
|
1307
|
+
function exportsFor(includeStore) {
|
|
1308
|
+
return [
|
|
1309
|
+
import_nestjs_agent_core11.AGENT_OPTIONS,
|
|
1310
|
+
import_nestjs_agent_core11.AGENT_TOOL_REGISTRY,
|
|
1311
|
+
import_nestjs_agent_core11.AGENT_REGISTRY,
|
|
1312
|
+
import_nestjs_agent_core11.AGENT_SINK,
|
|
1313
|
+
import_nestjs_agent_core11.AGENT_ROLES_POLICY,
|
|
1314
|
+
import_nestjs_agent_core11.AGENT_ACTOR_RESOLVER,
|
|
1315
|
+
import_nestjs_agent_core11.AGENT_MODEL,
|
|
1316
|
+
...includeStore ? [
|
|
1317
|
+
import_nestjs_agent_core11.AGENT_STORE
|
|
1318
|
+
] : [],
|
|
1319
|
+
import_nestjs_agent_core11.AGENT_QUOTA_STORE,
|
|
1320
|
+
import_nestjs_agent_core11.AGENT_DEPS_FACTORY,
|
|
1321
|
+
AgentDepsFactory2,
|
|
1322
|
+
AgentService,
|
|
1323
|
+
InlineAgentRunner
|
|
1324
|
+
];
|
|
1325
|
+
}
|
|
1326
|
+
__name(exportsFor, "exportsFor");
|
|
1327
|
+
var CONTROLLERS = [
|
|
1328
|
+
ChatController,
|
|
1329
|
+
ThreadsController,
|
|
1330
|
+
ToolCallController,
|
|
1331
|
+
QuotaController
|
|
1332
|
+
];
|
|
1333
|
+
function routerFor(path) {
|
|
1334
|
+
return import_core2.RouterModule.register([
|
|
1335
|
+
{
|
|
1336
|
+
path,
|
|
1337
|
+
module: AgentModule
|
|
1338
|
+
}
|
|
1339
|
+
]);
|
|
1340
|
+
}
|
|
1341
|
+
__name(routerFor, "routerFor");
|
|
1342
|
+
var AgentModule = class _AgentModule {
|
|
1343
|
+
static {
|
|
1344
|
+
__name(this, "AgentModule");
|
|
1345
|
+
}
|
|
1346
|
+
static forRoot(options) {
|
|
1347
|
+
const path = options.path ?? DEFAULT_PATH;
|
|
1348
|
+
const includeStore = options.store !== void 0;
|
|
1349
|
+
return {
|
|
1350
|
+
module: _AgentModule,
|
|
1351
|
+
global: true,
|
|
1352
|
+
imports: [
|
|
1353
|
+
import_core2.DiscoveryModule,
|
|
1354
|
+
routerFor(path)
|
|
1355
|
+
],
|
|
1356
|
+
controllers: CONTROLLERS,
|
|
1357
|
+
providers: [
|
|
1358
|
+
{
|
|
1359
|
+
provide: import_nestjs_agent_core11.AGENT_OPTIONS,
|
|
1360
|
+
useValue: options
|
|
1361
|
+
},
|
|
1362
|
+
...sharedProviders(options.durable ?? false, includeStore)
|
|
1363
|
+
],
|
|
1364
|
+
exports: exportsFor(includeStore)
|
|
1365
|
+
};
|
|
1366
|
+
}
|
|
1367
|
+
static forRootAsync(options) {
|
|
1368
|
+
const path = options.path ?? DEFAULT_PATH;
|
|
1369
|
+
return {
|
|
1370
|
+
module: _AgentModule,
|
|
1371
|
+
global: true,
|
|
1372
|
+
imports: [
|
|
1373
|
+
import_core2.DiscoveryModule,
|
|
1374
|
+
routerFor(path),
|
|
1375
|
+
...options.imports ?? []
|
|
1376
|
+
],
|
|
1377
|
+
controllers: CONTROLLERS,
|
|
1378
|
+
providers: [
|
|
1379
|
+
{
|
|
1380
|
+
provide: import_nestjs_agent_core11.AGENT_OPTIONS,
|
|
1381
|
+
useFactory: options.useFactory,
|
|
1382
|
+
inject: options.inject ?? []
|
|
1383
|
+
},
|
|
1384
|
+
// The async factory resolves too late to inspect `store`, so we always bind AGENT_STORE here
|
|
1385
|
+
// (its factory reads the resolved options). Omit-and-import-a-store-module is a `forRoot` path.
|
|
1386
|
+
...sharedProviders(options.durable ?? false, true)
|
|
1387
|
+
],
|
|
1388
|
+
exports: exportsFor(true)
|
|
1389
|
+
};
|
|
1390
|
+
}
|
|
1391
|
+
/** Register additional named agents (an orchestrator + its sub-agents). */
|
|
1392
|
+
static forFeature(definitions) {
|
|
1393
|
+
return {
|
|
1394
|
+
module: _AgentModule,
|
|
1395
|
+
providers: [
|
|
1396
|
+
{
|
|
1397
|
+
provide: FEATURE_INIT,
|
|
1398
|
+
useFactory: /* @__PURE__ */ __name((registry) => {
|
|
1399
|
+
for (const definition of definitions) {
|
|
1400
|
+
registry.register(definition);
|
|
1401
|
+
}
|
|
1402
|
+
return true;
|
|
1403
|
+
}, "useFactory"),
|
|
1404
|
+
inject: [
|
|
1405
|
+
import_nestjs_agent_core11.AGENT_REGISTRY
|
|
1406
|
+
]
|
|
1407
|
+
}
|
|
1408
|
+
]
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
};
|
|
1412
|
+
AgentModule = _ts_decorate9([
|
|
1413
|
+
(0, import_common9.Global)(),
|
|
1414
|
+
(0, import_common9.Module)({})
|
|
1415
|
+
], AgentModule);
|
|
1416
|
+
|
|
1417
|
+
// src/resolver/header-actor-resolver.ts
|
|
1418
|
+
function readHeader(req, name) {
|
|
1419
|
+
if (typeof req !== "object" || req === null || !("headers" in req)) return void 0;
|
|
1420
|
+
const { headers } = req;
|
|
1421
|
+
if (typeof headers !== "object" || headers === null || !(name in headers)) return void 0;
|
|
1422
|
+
const value = Reflect.get(headers, name);
|
|
1423
|
+
return typeof value === "string" ? value : void 0;
|
|
1424
|
+
}
|
|
1425
|
+
__name(readHeader, "readHeader");
|
|
1426
|
+
var HeaderActorResolver = class {
|
|
1427
|
+
static {
|
|
1428
|
+
__name(this, "HeaderActorResolver");
|
|
1429
|
+
}
|
|
1430
|
+
resolve(req) {
|
|
1431
|
+
const id = readHeader(req, "x-actor-id");
|
|
1432
|
+
if (id === void 0 || id.length === 0) {
|
|
1433
|
+
throw new Error("HeaderActorResolver: missing x-actor-id header. No default actor is fabricated. Send x-actor-id from a trusted gateway, or configure AgentModule.forRoot({ actorResolver }) with a resolver that reads your authenticated principal.");
|
|
1434
|
+
}
|
|
1435
|
+
const roles = (readHeader(req, "x-actor-role") ?? "").split(",").map((role) => role.trim()).filter((role) => role.length > 0);
|
|
1436
|
+
const tenantRef = readHeader(req, "x-tenant-ref");
|
|
1437
|
+
return {
|
|
1438
|
+
id,
|
|
1439
|
+
roles,
|
|
1440
|
+
...tenantRef !== void 0 ? {
|
|
1441
|
+
tenantRef
|
|
1442
|
+
} : {}
|
|
1443
|
+
};
|
|
1444
|
+
}
|
|
1445
|
+
};
|
|
1446
|
+
|
|
1447
|
+
// src/index.ts
|
|
1448
|
+
__reExport(index_exports, require("@dudousxd/nestjs-agent-core"), module.exports);
|
|
1449
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1450
|
+
0 && (module.exports = {
|
|
1451
|
+
AGENT_TOOL_BRAND,
|
|
1452
|
+
AI_TOOL_METADATA,
|
|
1453
|
+
AgentDepsFactory,
|
|
1454
|
+
AgentModule,
|
|
1455
|
+
AgentService,
|
|
1456
|
+
AiTool,
|
|
1457
|
+
AiToolDiscoveryService,
|
|
1458
|
+
HeaderActorResolver,
|
|
1459
|
+
InProcessTokenStreamSink,
|
|
1460
|
+
InlineAgentRunner,
|
|
1461
|
+
LedgerQuotaStore,
|
|
1462
|
+
delegateToolName,
|
|
1463
|
+
provideAgentTool,
|
|
1464
|
+
readAiToolMetadata,
|
|
1465
|
+
utcDay,
|
|
1466
|
+
...require("@dudousxd/nestjs-agent-core")
|
|
1467
|
+
});
|
|
1468
|
+
//# sourceMappingURL=index.cjs.map
|