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