@dudousxd/nestjs-agent 1.7.0 → 1.9.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/README.md +52 -0
- package/dist/{agent-deps.factory-CiC5Va9J.d.cts → agent-deps.factory-DuTy1UyU.d.cts} +49 -2
- package/dist/{agent-deps.factory-CiC5Va9J.d.ts → agent-deps.factory-DuTy1UyU.d.ts} +49 -2
- package/dist/durable/index.cjs +244 -74
- package/dist/durable/index.cjs.map +1 -1
- package/dist/durable/index.d.cts +1 -1
- package/dist/durable/index.d.ts +1 -1
- package/dist/durable/index.js +192 -22
- package/dist/durable/index.js.map +1 -1
- package/dist/genui/index.cjs +186 -0
- package/dist/genui/index.cjs.map +1 -0
- package/dist/genui/index.d.cts +81 -0
- package/dist/genui/index.d.ts +81 -0
- package/dist/genui/index.js +155 -0
- package/dist/genui/index.js.map +1 -0
- package/dist/index.cjs +248 -73
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -4
- package/dist/index.d.ts +17 -4
- package/dist/index.js +194 -21
- package/dist/index.js.map +1 -1
- package/package.json +13 -3
|
@@ -0,0 +1,186 @@
|
|
|
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/genui/index.ts
|
|
23
|
+
var genui_exports = {};
|
|
24
|
+
__export(genui_exports, {
|
|
25
|
+
AgentGenuiModule: () => AgentGenuiModule,
|
|
26
|
+
GENUI_CATALOG: () => GENUI_CATALOG,
|
|
27
|
+
GENUI_OPTIONS: () => GENUI_OPTIONS,
|
|
28
|
+
GenuiCatalogResolver: () => GenuiCatalogResolver,
|
|
29
|
+
InjectGenuiCatalog: () => InjectGenuiCatalog
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(genui_exports);
|
|
32
|
+
|
|
33
|
+
// src/genui/agent-genui.module.ts
|
|
34
|
+
var import_genui = require("@dudousxd/nestjs-agent-core/genui");
|
|
35
|
+
var import_common = require("@nestjs/common");
|
|
36
|
+
|
|
37
|
+
// src/functional-tool.ts
|
|
38
|
+
var AGENT_TOOL_BRAND = /* @__PURE__ */ Symbol.for("@dudousxd/nestjs-agent:functional-tool");
|
|
39
|
+
function brand(tool) {
|
|
40
|
+
return {
|
|
41
|
+
[AGENT_TOOL_BRAND]: true,
|
|
42
|
+
spec: tool.spec,
|
|
43
|
+
handler: tool.handler
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
__name(brand, "brand");
|
|
47
|
+
function provideAgentTools(factory, inject) {
|
|
48
|
+
return {
|
|
49
|
+
provide: /* @__PURE__ */ Symbol("nestjs-agent:functional-tool-list"),
|
|
50
|
+
useFactory: /* @__PURE__ */ __name(async (...deps) => (await factory(...deps)).map(brand), "useFactory"),
|
|
51
|
+
inject: inject ?? []
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
__name(provideAgentTools, "provideAgentTools");
|
|
55
|
+
|
|
56
|
+
// src/genui/agent-genui.module.ts
|
|
57
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
58
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
59
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
|
|
60
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
61
|
+
} else {
|
|
62
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
63
|
+
if (d = decorators[i]) {
|
|
64
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
69
|
+
}
|
|
70
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
71
|
+
var GENUI_CATALOG = /* @__PURE__ */ Symbol.for("@dudousxd/nestjs-agent:genui-catalog");
|
|
72
|
+
var GENUI_OPTIONS = /* @__PURE__ */ Symbol.for("@dudousxd/nestjs-agent:genui-options");
|
|
73
|
+
var InjectGenuiCatalog = /* @__PURE__ */ __name(() => (0, import_common.Inject)(GENUI_CATALOG), "InjectGenuiCatalog");
|
|
74
|
+
var GenuiCatalogResolver = class {
|
|
75
|
+
static {
|
|
76
|
+
__name(this, "GenuiCatalogResolver");
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
function resolverProviders(resolver) {
|
|
80
|
+
if (resolver === void 0) return [];
|
|
81
|
+
return [
|
|
82
|
+
typeof resolver === "function" ? {
|
|
83
|
+
provide: GenuiCatalogResolver,
|
|
84
|
+
useClass: resolver
|
|
85
|
+
} : {
|
|
86
|
+
provide: GenuiCatalogResolver,
|
|
87
|
+
useValue: resolver
|
|
88
|
+
}
|
|
89
|
+
];
|
|
90
|
+
}
|
|
91
|
+
__name(resolverProviders, "resolverProviders");
|
|
92
|
+
function toolOptions(options, resolver) {
|
|
93
|
+
const { catalog: _catalog, ...rest } = options;
|
|
94
|
+
return {
|
|
95
|
+
...rest,
|
|
96
|
+
...resolver !== void 0 ? {
|
|
97
|
+
resolveCatalog: /* @__PURE__ */ __name((scope) => resolver.resolve(scope), "resolveCatalog")
|
|
98
|
+
} : {}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
__name(toolOptions, "toolOptions");
|
|
102
|
+
function coreProviders(resolver) {
|
|
103
|
+
return [
|
|
104
|
+
{
|
|
105
|
+
provide: GENUI_CATALOG,
|
|
106
|
+
useFactory: /* @__PURE__ */ __name((options) => options.catalog ?? (0, import_genui.defineCatalog)([]), "useFactory"),
|
|
107
|
+
inject: [
|
|
108
|
+
GENUI_OPTIONS
|
|
109
|
+
]
|
|
110
|
+
},
|
|
111
|
+
...resolverProviders(resolver),
|
|
112
|
+
// The generated tools, registered by the agent module's discovery like any `provideAgentTool`.
|
|
113
|
+
// Built from the injected catalog, so `overrideProvider(GENUI_CATALOG)` changes them too.
|
|
114
|
+
provideAgentTools((options, catalog, perRequest) => (0, import_genui.genuiTools)(catalog, toolOptions(options, perRequest)), [
|
|
115
|
+
GENUI_OPTIONS,
|
|
116
|
+
GENUI_CATALOG,
|
|
117
|
+
{
|
|
118
|
+
token: GenuiCatalogResolver,
|
|
119
|
+
optional: true
|
|
120
|
+
}
|
|
121
|
+
])
|
|
122
|
+
];
|
|
123
|
+
}
|
|
124
|
+
__name(coreProviders, "coreProviders");
|
|
125
|
+
var EXPORTS = [
|
|
126
|
+
GENUI_CATALOG,
|
|
127
|
+
GENUI_OPTIONS
|
|
128
|
+
];
|
|
129
|
+
var AgentGenuiModule = class _AgentGenuiModule {
|
|
130
|
+
static {
|
|
131
|
+
__name(this, "AgentGenuiModule");
|
|
132
|
+
}
|
|
133
|
+
static forRoot(options = {}) {
|
|
134
|
+
const { resolver, ...rest } = options;
|
|
135
|
+
return {
|
|
136
|
+
module: _AgentGenuiModule,
|
|
137
|
+
global: true,
|
|
138
|
+
providers: [
|
|
139
|
+
{
|
|
140
|
+
provide: GENUI_OPTIONS,
|
|
141
|
+
useValue: rest
|
|
142
|
+
},
|
|
143
|
+
...coreProviders(resolver)
|
|
144
|
+
],
|
|
145
|
+
exports: resolver === void 0 ? EXPORTS : [
|
|
146
|
+
...EXPORTS,
|
|
147
|
+
GenuiCatalogResolver
|
|
148
|
+
]
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
static forRootAsync(options) {
|
|
152
|
+
return {
|
|
153
|
+
module: _AgentGenuiModule,
|
|
154
|
+
global: true,
|
|
155
|
+
imports: options.imports ?? [],
|
|
156
|
+
providers: [
|
|
157
|
+
{
|
|
158
|
+
provide: GENUI_OPTIONS,
|
|
159
|
+
useFactory: options.useFactory,
|
|
160
|
+
inject: options.inject ?? []
|
|
161
|
+
},
|
|
162
|
+
...coreProviders(options.resolver)
|
|
163
|
+
],
|
|
164
|
+
exports: options.resolver === void 0 ? EXPORTS : [
|
|
165
|
+
...EXPORTS,
|
|
166
|
+
GenuiCatalogResolver
|
|
167
|
+
]
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
AgentGenuiModule = _ts_decorate([
|
|
172
|
+
(0, import_common.Module)({})
|
|
173
|
+
], AgentGenuiModule);
|
|
174
|
+
|
|
175
|
+
// src/genui/index.ts
|
|
176
|
+
__reExport(genui_exports, require("@dudousxd/nestjs-agent-core/genui"), module.exports);
|
|
177
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
178
|
+
0 && (module.exports = {
|
|
179
|
+
AgentGenuiModule,
|
|
180
|
+
GENUI_CATALOG,
|
|
181
|
+
GENUI_OPTIONS,
|
|
182
|
+
GenuiCatalogResolver,
|
|
183
|
+
InjectGenuiCatalog,
|
|
184
|
+
...require("@dudousxd/nestjs-agent-core/genui")
|
|
185
|
+
});
|
|
186
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/genui/index.ts","../../src/genui/agent-genui.module.ts","../../src/functional-tool.ts"],"sourcesContent":["/**\n * `@dudousxd/nestjs-agent/genui` — generative UI for NestJS: {@link AgentGenuiModule} registers the\n * tools that push catalog components, `GENUI_CATALOG` / `@InjectGenuiCatalog()` expose the catalog,\n * and {@link GenuiCatalogResolver} serves per-request (per-tenant) catalogs. The catalog itself —\n * `defineComponent`, `defineCatalog`, validation, text fallbacks — is re-exported from\n * `@dudousxd/nestjs-agent-core/genui`, the isomorphic entry a browser imports too.\n */\nexport {\n AgentGenuiModule,\n type AgentGenuiModuleAsyncOptions,\n type AgentGenuiModuleOptions,\n type AgentGenuiOptions,\n GENUI_CATALOG,\n GENUI_OPTIONS,\n GenuiCatalogResolver,\n InjectGenuiCatalog,\n} from './agent-genui.module.js';\nexport * from '@dudousxd/nestjs-agent-core/genui';\n","import type { ToolPresentation } from '@dudousxd/nestjs-agent-core';\nimport type {\n Catalog,\n GenuiCatalogScope,\n GenuiToolsOptions,\n TreeLimits,\n} from '@dudousxd/nestjs-agent-core/genui';\nimport { defineCatalog, genuiTools } from '@dudousxd/nestjs-agent-core/genui';\nimport {\n type DynamicModule,\n type FactoryProvider,\n Inject,\n Module,\n type ModuleMetadata,\n type Provider,\n type Type,\n} from '@nestjs/common';\nimport { provideAgentTools } from '../functional-tool.js';\n\n/** The app's genui catalog — `@InjectGenuiCatalog()` it anywhere; `overrideProvider` it in tests. */\nexport const GENUI_CATALOG = Symbol.for('@dudousxd/nestjs-agent:genui-catalog');\n\n/** The resolved {@link AgentGenuiOptions} (without the resolver). */\nexport const GENUI_OPTIONS = Symbol.for('@dudousxd/nestjs-agent:genui-options');\n\n/** Inject the app's genui catalog ({@link GENUI_CATALOG}). */\nexport const InjectGenuiCatalog = (): ParameterDecorator & PropertyDecorator =>\n Inject(GENUI_CATALOG);\n\n/**\n * Picks the catalog for ONE request — a tenant's own, versioned components. Consulted when a genui\n * call is validated and when the turn's tool list is described to the model, so components can\n * change without rebuilding tools at boot. Cache in here if resolving costs a round trip: it runs\n * once per turn per genui tool, and once per call.\n *\n * Provide it through `AgentGenuiModule.forRoot({ resolver })` (a class, resolved with DI, or an\n * instance); it is then injectable, and replaceable in a test with\n * `overrideProvider(GenuiCatalogResolver)`.\n */\nexport abstract class GenuiCatalogResolver {\n abstract resolve(scope: GenuiCatalogScope): Catalog | Promise<Catalog>;\n}\n\n/** What {@link AgentGenuiModule} builds tools from. */\nexport interface AgentGenuiOptions {\n /**\n * The catalog. With a {@link GenuiCatalogResolver} it is the boot-time one — it names the\n * per-component tools — and may be empty. Default: an empty catalog.\n */\n catalog?: Catalog;\n /** `per-component` (default): one `ui__show_<snake>` tool per component. `tree`: one tool taking a composed tree. */\n mode?: 'per-component' | 'tree';\n /** End the model's turn once a genui call succeeds (no narrating follow-up call). */\n terminal?: boolean;\n /** Tool name in `tree` mode. Default `ui__render`. */\n treeToolName?: string;\n /** Text prepended to the tree tool's description. */\n treeInstructions?: string;\n treeLimits?: TreeLimits;\n /** Tool-name prefix in `per-component` mode. Default `ui__show_`. */\n namePrefix?: string;\n /**\n * Also register ONE generic tool taking `{ component, props }`, validated against the request's\n * catalog — how a model reaches a tenant's own components, which no boot-time tool names. `true`\n * names it `ui__show`.\n */\n showTool?: boolean | string;\n /** Text prepended to the show tool's description. */\n showInstructions?: string;\n /** Roles allowed to call the tools. Undefined → the roles policy default. */\n roles?: string[];\n /** Override the generated presentation per component (or per tree / show tool name). */\n presentation?: (component: string) => ToolPresentation | undefined;\n}\n\ntype ResolverOption = Type<GenuiCatalogResolver> | GenuiCatalogResolver;\n\nexport interface AgentGenuiModuleOptions extends AgentGenuiOptions {\n /** Per-request catalogs. A class is instantiated with DI (its dependencies must be global or exported to the root). */\n resolver?: ResolverOption;\n}\n\nexport interface AgentGenuiModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {\n inject?: FactoryProvider['inject'];\n useFactory: (...deps: any[]) => AgentGenuiOptions | Promise<AgentGenuiOptions>;\n /** Per-request catalogs. A class resolves its dependencies from `imports`. */\n resolver?: ResolverOption;\n}\n\nfunction resolverProviders(resolver: ResolverOption | undefined): Provider[] {\n if (resolver === undefined) return [];\n return [\n typeof resolver === 'function'\n ? { provide: GenuiCatalogResolver, useClass: resolver }\n : { provide: GenuiCatalogResolver, useValue: resolver },\n ];\n}\n\nfunction toolOptions(\n options: AgentGenuiOptions,\n resolver: GenuiCatalogResolver | undefined,\n): GenuiToolsOptions {\n const { catalog: _catalog, ...rest } = options;\n return {\n ...rest,\n ...(resolver !== undefined\n ? { resolveCatalog: (scope: GenuiCatalogScope) => resolver.resolve(scope) }\n : {}),\n };\n}\n\nfunction coreProviders(resolver: ResolverOption | undefined): Provider[] {\n return [\n {\n provide: GENUI_CATALOG,\n useFactory: (options: AgentGenuiOptions) => options.catalog ?? defineCatalog([]),\n inject: [GENUI_OPTIONS],\n },\n ...resolverProviders(resolver),\n // The generated tools, registered by the agent module's discovery like any `provideAgentTool`.\n // Built from the injected catalog, so `overrideProvider(GENUI_CATALOG)` changes them too.\n provideAgentTools(\n (options: AgentGenuiOptions, catalog: Catalog, perRequest?: GenuiCatalogResolver) =>\n genuiTools(catalog, toolOptions(options, perRequest)),\n [GENUI_OPTIONS, GENUI_CATALOG, { token: GenuiCatalogResolver, optional: true }],\n ),\n ];\n}\n\nconst EXPORTS = [GENUI_CATALOG, GENUI_OPTIONS];\n\n/**\n * Generative UI for the agent: registers the tools that let the model push catalog components\n * (`ui__show_<component>`, or one tree tool, or the generic `ui__show`), and makes the catalog\n * injectable. Global, like `AgentModule`; import it once, next to it.\n *\n * ```ts\n * AgentGenuiModule.forRoot({ catalog, mode: 'tree', terminal: true })\n * ```\n */\n@Module({})\nexport class AgentGenuiModule {\n static forRoot(options: AgentGenuiModuleOptions = {}): DynamicModule {\n const { resolver, ...rest } = options;\n return {\n module: AgentGenuiModule,\n global: true,\n providers: [{ provide: GENUI_OPTIONS, useValue: rest }, ...coreProviders(resolver)],\n exports: resolver === undefined ? EXPORTS : [...EXPORTS, GenuiCatalogResolver],\n };\n }\n\n static forRootAsync(options: AgentGenuiModuleAsyncOptions): DynamicModule {\n return {\n module: AgentGenuiModule,\n global: true,\n imports: options.imports ?? [],\n providers: [\n { provide: GENUI_OPTIONS, useFactory: options.useFactory, inject: options.inject ?? [] },\n ...coreProviders(options.resolver),\n ],\n exports: options.resolver === undefined ? EXPORTS : [...EXPORTS, GenuiCatalogResolver],\n };\n }\n}\n","import type { ToolHandler, ToolSpec } from '@dudousxd/nestjs-agent-core';\nimport type { FactoryProvider, Provider } from '@nestjs/common';\n\n/**\n * Brand stamped onto a functional-tool provider instance so `AiToolDiscoveryService` picks it up\n * when it walks every provider at boot — the declarative alternative to a manual `registry.register`.\n */\nexport const AGENT_TOOL_BRAND = Symbol.for('@dudousxd/nestjs-agent:functional-tool');\n\n/** A tool expressed as data + handler (e.g. from `createExecuteSqlTool`), not an `@AiTool` class. */\nexport interface FunctionalTool {\n spec: ToolSpec;\n handler: ToolHandler;\n}\n\n/** A {@link FunctionalTool} carrying {@link AGENT_TOOL_BRAND} — what a provider resolves to. */\nexport interface BrandedFunctionalTool extends FunctionalTool {\n readonly [AGENT_TOOL_BRAND]: true;\n}\n\nfunction brand(tool: FunctionalTool): BrandedFunctionalTool {\n return { [AGENT_TOOL_BRAND]: true, spec: tool.spec, handler: tool.handler };\n}\n\n/** Narrows a provider instance to a branded functional tool for boot-time registration. */\nexport function isBrandedFunctionalTool(value: object): value is BrandedFunctionalTool {\n return AGENT_TOOL_BRAND in value && 'spec' in value && 'handler' in value;\n}\n\n/**\n * Registers a functional tool declaratively — drop the returned provider in a module's `providers`\n * and `AiToolDiscoveryService` auto-registers it at boot. Each call uses a fresh, unique provide\n * token so the same tool can be provided many times without collisions.\n *\n * Two forms: a static tool (`useValue`) or a factory with `inject` for DI-resolved dependencies\n * (`useFactory`). Either way the resolved instance is branded with {@link AGENT_TOOL_BRAND}.\n */\nexport function provideAgentTool(tool: FunctionalTool): Provider;\nexport function provideAgentTool(\n factory: (...deps: never[]) => FunctionalTool,\n inject?: FactoryProvider['inject'],\n): Provider;\nexport function provideAgentTool(\n factoryOrTool: FunctionalTool | ((...deps: never[]) => FunctionalTool),\n inject?: FactoryProvider['inject'],\n): Provider {\n const provide = Symbol('nestjs-agent:functional-tool-instance');\n if (typeof factoryOrTool === 'function') {\n return {\n provide,\n useFactory: (...deps: never[]) => brand(factoryOrTool(...deps)),\n inject: inject ?? [],\n };\n }\n return { provide, useValue: brand(factoryOrTool) };\n}\n\n/**\n * Registers a LIST of functional tools from one factory — for a producer whose tool count is only\n * known once its dependencies resolve (a catalog loaded from config, one tool per entry). The\n * factory's result is branded element by element; `AiToolDiscoveryService` registers each.\n */\nexport function provideAgentTools(\n factory: (...deps: never[]) => readonly FunctionalTool[] | Promise<readonly FunctionalTool[]>,\n inject?: FactoryProvider['inject'],\n): Provider {\n return {\n provide: Symbol('nestjs-agent:functional-tool-list'),\n useFactory: async (...deps: never[]) => (await factory(...deps)).map(brand),\n inject: inject ?? [],\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;ACOA,mBAA0C;AAC1C,oBAQO;;;ACTA,IAAMA,mBAAmBC,uBAAOC,IAAI,wCAAA;AAa3C,SAASC,MAAMC,MAAoB;AACjC,SAAO;IAAE,CAACJ,gBAAAA,GAAmB;IAAMK,MAAMD,KAAKC;IAAMC,SAASF,KAAKE;EAAQ;AAC5E;AAFSH;AA0CF,SAASI,kBACdC,SACAC,QAAkC;AAElC,SAAO;IACLC,SAASC,uBAAO,mCAAA;IAChBC,YAAY,iCAAUC,UAAmB,MAAML,QAAAA,GAAWK,IAAAA,GAAOC,IAAIC,KAAAA,GAAzD;IACZN,QAAQA,UAAU,CAAA;EACpB;AACF;AATgBF;;;;;;;;;;;;;;;;;AD1CT,IAAMS,gBAAgBC,uBAAOC,IAAI,sCAAA;AAGjC,IAAMC,gBAAgBF,uBAAOC,IAAI,sCAAA;AAGjC,IAAME,qBAAqB,iCAChCC,sBAAOL,aAAAA,GADyB;AAa3B,IAAeM,uBAAf,MAAeA;SAAAA;;;AAEtB;AAgDA,SAASC,kBAAkBC,UAAoC;AAC7D,MAAIA,aAAaC,OAAW,QAAO,CAAA;AACnC,SAAO;IACL,OAAOD,aAAa,aAChB;MAAEE,SAASJ;MAAsBK,UAAUH;IAAS,IACpD;MAAEE,SAASJ;MAAsBM,UAAUJ;IAAS;;AAE5D;AAPSD;AAST,SAASM,YACPC,SACAN,UAA0C;AAE1C,QAAM,EAAEO,SAASC,UAAU,GAAGC,KAAAA,IAASH;AACvC,SAAO;IACL,GAAGG;IACH,GAAIT,aAAaC,SACb;MAAES,gBAAgB,wBAACC,UAA6BX,SAASY,QAAQD,KAAAA,GAA/C;IAAsD,IACxE,CAAC;EACP;AACF;AAXSN;AAaT,SAASQ,cAAcb,UAAoC;AACzD,SAAO;IACL;MACEE,SAASV;MACTsB,YAAY,wBAACR,YAA+BA,QAAQC,eAAWQ,4BAAc,CAAA,CAAE,GAAnE;MACZC,QAAQ;QAACrB;;IACX;OACGI,kBAAkBC,QAAAA;;;IAGrBiB,kBACE,CAACX,SAA4BC,SAAkBW,mBAC7CC,yBAAWZ,SAASF,YAAYC,SAASY,UAAAA,CAAAA,GAC3C;MAACvB;MAAeH;MAAe;QAAE4B,OAAOtB;QAAsBuB,UAAU;MAAK;KAAE;;AAGrF;AAhBSR;AAkBT,IAAMS,UAAU;EAAC9B;EAAeG;;AAYzB,IAAM4B,mBAAN,MAAMA,kBAAAA;SAAAA;;;EACX,OAAOC,QAAQlB,UAAmC,CAAC,GAAkB;AACnE,UAAM,EAAEN,UAAU,GAAGS,KAAAA,IAASH;AAC9B,WAAO;MACLmB,QAAQF;MACRG,QAAQ;MACRC,WAAW;QAAC;UAAEzB,SAASP;UAAeS,UAAUK;QAAK;WAAMI,cAAcb,QAAAA;;MACzE4B,SAAS5B,aAAaC,SAAYqB,UAAU;WAAIA;QAASxB;;IAC3D;EACF;EAEA,OAAO+B,aAAavB,SAAsD;AACxE,WAAO;MACLmB,QAAQF;MACRG,QAAQ;MACRI,SAASxB,QAAQwB,WAAW,CAAA;MAC5BH,WAAW;QACT;UAAEzB,SAASP;UAAemB,YAAYR,QAAQQ;UAAYE,QAAQV,QAAQU,UAAU,CAAA;QAAG;WACpFH,cAAcP,QAAQN,QAAQ;;MAEnC4B,SAAStB,QAAQN,aAAaC,SAAYqB,UAAU;WAAIA;QAASxB;;IACnE;EACF;AACF;;;;;;ADnJA,0BAAc,8CAjBd;","names":["AGENT_TOOL_BRAND","Symbol","for","brand","tool","spec","handler","provideAgentTools","factory","inject","provide","Symbol","useFactory","deps","map","brand","GENUI_CATALOG","Symbol","for","GENUI_OPTIONS","InjectGenuiCatalog","Inject","GenuiCatalogResolver","resolverProviders","resolver","undefined","provide","useClass","useValue","toolOptions","options","catalog","_catalog","rest","resolveCatalog","scope","resolve","coreProviders","useFactory","defineCatalog","inject","provideAgentTools","perRequest","genuiTools","token","optional","EXPORTS","AgentGenuiModule","forRoot","module","global","providers","exports","forRootAsync","imports"]}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { ToolPresentation } from '@dudousxd/nestjs-agent-core';
|
|
2
|
+
import { Catalog, TreeLimits, GenuiCatalogScope } from '@dudousxd/nestjs-agent-core/genui';
|
|
3
|
+
export * from '@dudousxd/nestjs-agent-core/genui';
|
|
4
|
+
import { Type, DynamicModule, ModuleMetadata, FactoryProvider } from '@nestjs/common';
|
|
5
|
+
|
|
6
|
+
/** The app's genui catalog — `@InjectGenuiCatalog()` it anywhere; `overrideProvider` it in tests. */
|
|
7
|
+
declare const GENUI_CATALOG: unique symbol;
|
|
8
|
+
/** The resolved {@link AgentGenuiOptions} (without the resolver). */
|
|
9
|
+
declare const GENUI_OPTIONS: unique symbol;
|
|
10
|
+
/** Inject the app's genui catalog ({@link GENUI_CATALOG}). */
|
|
11
|
+
declare const InjectGenuiCatalog: () => ParameterDecorator & PropertyDecorator;
|
|
12
|
+
/**
|
|
13
|
+
* Picks the catalog for ONE request — a tenant's own, versioned components. Consulted when a genui
|
|
14
|
+
* call is validated and when the turn's tool list is described to the model, so components can
|
|
15
|
+
* change without rebuilding tools at boot. Cache in here if resolving costs a round trip: it runs
|
|
16
|
+
* once per turn per genui tool, and once per call.
|
|
17
|
+
*
|
|
18
|
+
* Provide it through `AgentGenuiModule.forRoot({ resolver })` (a class, resolved with DI, or an
|
|
19
|
+
* instance); it is then injectable, and replaceable in a test with
|
|
20
|
+
* `overrideProvider(GenuiCatalogResolver)`.
|
|
21
|
+
*/
|
|
22
|
+
declare abstract class GenuiCatalogResolver {
|
|
23
|
+
abstract resolve(scope: GenuiCatalogScope): Catalog | Promise<Catalog>;
|
|
24
|
+
}
|
|
25
|
+
/** What {@link AgentGenuiModule} builds tools from. */
|
|
26
|
+
interface AgentGenuiOptions {
|
|
27
|
+
/**
|
|
28
|
+
* The catalog. With a {@link GenuiCatalogResolver} it is the boot-time one — it names the
|
|
29
|
+
* per-component tools — and may be empty. Default: an empty catalog.
|
|
30
|
+
*/
|
|
31
|
+
catalog?: Catalog;
|
|
32
|
+
/** `per-component` (default): one `ui__show_<snake>` tool per component. `tree`: one tool taking a composed tree. */
|
|
33
|
+
mode?: 'per-component' | 'tree';
|
|
34
|
+
/** End the model's turn once a genui call succeeds (no narrating follow-up call). */
|
|
35
|
+
terminal?: boolean;
|
|
36
|
+
/** Tool name in `tree` mode. Default `ui__render`. */
|
|
37
|
+
treeToolName?: string;
|
|
38
|
+
/** Text prepended to the tree tool's description. */
|
|
39
|
+
treeInstructions?: string;
|
|
40
|
+
treeLimits?: TreeLimits;
|
|
41
|
+
/** Tool-name prefix in `per-component` mode. Default `ui__show_`. */
|
|
42
|
+
namePrefix?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Also register ONE generic tool taking `{ component, props }`, validated against the request's
|
|
45
|
+
* catalog — how a model reaches a tenant's own components, which no boot-time tool names. `true`
|
|
46
|
+
* names it `ui__show`.
|
|
47
|
+
*/
|
|
48
|
+
showTool?: boolean | string;
|
|
49
|
+
/** Text prepended to the show tool's description. */
|
|
50
|
+
showInstructions?: string;
|
|
51
|
+
/** Roles allowed to call the tools. Undefined → the roles policy default. */
|
|
52
|
+
roles?: string[];
|
|
53
|
+
/** Override the generated presentation per component (or per tree / show tool name). */
|
|
54
|
+
presentation?: (component: string) => ToolPresentation | undefined;
|
|
55
|
+
}
|
|
56
|
+
type ResolverOption = Type<GenuiCatalogResolver> | GenuiCatalogResolver;
|
|
57
|
+
interface AgentGenuiModuleOptions extends AgentGenuiOptions {
|
|
58
|
+
/** Per-request catalogs. A class is instantiated with DI (its dependencies must be global or exported to the root). */
|
|
59
|
+
resolver?: ResolverOption;
|
|
60
|
+
}
|
|
61
|
+
interface AgentGenuiModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
62
|
+
inject?: FactoryProvider['inject'];
|
|
63
|
+
useFactory: (...deps: any[]) => AgentGenuiOptions | Promise<AgentGenuiOptions>;
|
|
64
|
+
/** Per-request catalogs. A class resolves its dependencies from `imports`. */
|
|
65
|
+
resolver?: ResolverOption;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Generative UI for the agent: registers the tools that let the model push catalog components
|
|
69
|
+
* (`ui__show_<component>`, or one tree tool, or the generic `ui__show`), and makes the catalog
|
|
70
|
+
* injectable. Global, like `AgentModule`; import it once, next to it.
|
|
71
|
+
*
|
|
72
|
+
* ```ts
|
|
73
|
+
* AgentGenuiModule.forRoot({ catalog, mode: 'tree', terminal: true })
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
declare class AgentGenuiModule {
|
|
77
|
+
static forRoot(options?: AgentGenuiModuleOptions): DynamicModule;
|
|
78
|
+
static forRootAsync(options: AgentGenuiModuleAsyncOptions): DynamicModule;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export { AgentGenuiModule, type AgentGenuiModuleAsyncOptions, type AgentGenuiModuleOptions, type AgentGenuiOptions, GENUI_CATALOG, GENUI_OPTIONS, GenuiCatalogResolver, InjectGenuiCatalog };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { ToolPresentation } from '@dudousxd/nestjs-agent-core';
|
|
2
|
+
import { Catalog, TreeLimits, GenuiCatalogScope } from '@dudousxd/nestjs-agent-core/genui';
|
|
3
|
+
export * from '@dudousxd/nestjs-agent-core/genui';
|
|
4
|
+
import { Type, DynamicModule, ModuleMetadata, FactoryProvider } from '@nestjs/common';
|
|
5
|
+
|
|
6
|
+
/** The app's genui catalog — `@InjectGenuiCatalog()` it anywhere; `overrideProvider` it in tests. */
|
|
7
|
+
declare const GENUI_CATALOG: unique symbol;
|
|
8
|
+
/** The resolved {@link AgentGenuiOptions} (without the resolver). */
|
|
9
|
+
declare const GENUI_OPTIONS: unique symbol;
|
|
10
|
+
/** Inject the app's genui catalog ({@link GENUI_CATALOG}). */
|
|
11
|
+
declare const InjectGenuiCatalog: () => ParameterDecorator & PropertyDecorator;
|
|
12
|
+
/**
|
|
13
|
+
* Picks the catalog for ONE request — a tenant's own, versioned components. Consulted when a genui
|
|
14
|
+
* call is validated and when the turn's tool list is described to the model, so components can
|
|
15
|
+
* change without rebuilding tools at boot. Cache in here if resolving costs a round trip: it runs
|
|
16
|
+
* once per turn per genui tool, and once per call.
|
|
17
|
+
*
|
|
18
|
+
* Provide it through `AgentGenuiModule.forRoot({ resolver })` (a class, resolved with DI, or an
|
|
19
|
+
* instance); it is then injectable, and replaceable in a test with
|
|
20
|
+
* `overrideProvider(GenuiCatalogResolver)`.
|
|
21
|
+
*/
|
|
22
|
+
declare abstract class GenuiCatalogResolver {
|
|
23
|
+
abstract resolve(scope: GenuiCatalogScope): Catalog | Promise<Catalog>;
|
|
24
|
+
}
|
|
25
|
+
/** What {@link AgentGenuiModule} builds tools from. */
|
|
26
|
+
interface AgentGenuiOptions {
|
|
27
|
+
/**
|
|
28
|
+
* The catalog. With a {@link GenuiCatalogResolver} it is the boot-time one — it names the
|
|
29
|
+
* per-component tools — and may be empty. Default: an empty catalog.
|
|
30
|
+
*/
|
|
31
|
+
catalog?: Catalog;
|
|
32
|
+
/** `per-component` (default): one `ui__show_<snake>` tool per component. `tree`: one tool taking a composed tree. */
|
|
33
|
+
mode?: 'per-component' | 'tree';
|
|
34
|
+
/** End the model's turn once a genui call succeeds (no narrating follow-up call). */
|
|
35
|
+
terminal?: boolean;
|
|
36
|
+
/** Tool name in `tree` mode. Default `ui__render`. */
|
|
37
|
+
treeToolName?: string;
|
|
38
|
+
/** Text prepended to the tree tool's description. */
|
|
39
|
+
treeInstructions?: string;
|
|
40
|
+
treeLimits?: TreeLimits;
|
|
41
|
+
/** Tool-name prefix in `per-component` mode. Default `ui__show_`. */
|
|
42
|
+
namePrefix?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Also register ONE generic tool taking `{ component, props }`, validated against the request's
|
|
45
|
+
* catalog — how a model reaches a tenant's own components, which no boot-time tool names. `true`
|
|
46
|
+
* names it `ui__show`.
|
|
47
|
+
*/
|
|
48
|
+
showTool?: boolean | string;
|
|
49
|
+
/** Text prepended to the show tool's description. */
|
|
50
|
+
showInstructions?: string;
|
|
51
|
+
/** Roles allowed to call the tools. Undefined → the roles policy default. */
|
|
52
|
+
roles?: string[];
|
|
53
|
+
/** Override the generated presentation per component (or per tree / show tool name). */
|
|
54
|
+
presentation?: (component: string) => ToolPresentation | undefined;
|
|
55
|
+
}
|
|
56
|
+
type ResolverOption = Type<GenuiCatalogResolver> | GenuiCatalogResolver;
|
|
57
|
+
interface AgentGenuiModuleOptions extends AgentGenuiOptions {
|
|
58
|
+
/** Per-request catalogs. A class is instantiated with DI (its dependencies must be global or exported to the root). */
|
|
59
|
+
resolver?: ResolverOption;
|
|
60
|
+
}
|
|
61
|
+
interface AgentGenuiModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
62
|
+
inject?: FactoryProvider['inject'];
|
|
63
|
+
useFactory: (...deps: any[]) => AgentGenuiOptions | Promise<AgentGenuiOptions>;
|
|
64
|
+
/** Per-request catalogs. A class resolves its dependencies from `imports`. */
|
|
65
|
+
resolver?: ResolverOption;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Generative UI for the agent: registers the tools that let the model push catalog components
|
|
69
|
+
* (`ui__show_<component>`, or one tree tool, or the generic `ui__show`), and makes the catalog
|
|
70
|
+
* injectable. Global, like `AgentModule`; import it once, next to it.
|
|
71
|
+
*
|
|
72
|
+
* ```ts
|
|
73
|
+
* AgentGenuiModule.forRoot({ catalog, mode: 'tree', terminal: true })
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
declare class AgentGenuiModule {
|
|
77
|
+
static forRoot(options?: AgentGenuiModuleOptions): DynamicModule;
|
|
78
|
+
static forRootAsync(options: AgentGenuiModuleAsyncOptions): DynamicModule;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export { AgentGenuiModule, type AgentGenuiModuleAsyncOptions, type AgentGenuiModuleOptions, type AgentGenuiOptions, GENUI_CATALOG, GENUI_OPTIONS, GenuiCatalogResolver, InjectGenuiCatalog };
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/genui/agent-genui.module.ts
|
|
5
|
+
import { defineCatalog, genuiTools } from "@dudousxd/nestjs-agent-core/genui";
|
|
6
|
+
import { Inject, Module } from "@nestjs/common";
|
|
7
|
+
|
|
8
|
+
// src/functional-tool.ts
|
|
9
|
+
var AGENT_TOOL_BRAND = /* @__PURE__ */ Symbol.for("@dudousxd/nestjs-agent:functional-tool");
|
|
10
|
+
function brand(tool) {
|
|
11
|
+
return {
|
|
12
|
+
[AGENT_TOOL_BRAND]: true,
|
|
13
|
+
spec: tool.spec,
|
|
14
|
+
handler: tool.handler
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
__name(brand, "brand");
|
|
18
|
+
function provideAgentTools(factory, inject) {
|
|
19
|
+
return {
|
|
20
|
+
provide: /* @__PURE__ */ Symbol("nestjs-agent:functional-tool-list"),
|
|
21
|
+
useFactory: /* @__PURE__ */ __name(async (...deps) => (await factory(...deps)).map(brand), "useFactory"),
|
|
22
|
+
inject: inject ?? []
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
__name(provideAgentTools, "provideAgentTools");
|
|
26
|
+
|
|
27
|
+
// src/genui/agent-genui.module.ts
|
|
28
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
29
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
30
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
|
|
31
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
32
|
+
} else {
|
|
33
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
34
|
+
if (d = decorators[i]) {
|
|
35
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
40
|
+
}
|
|
41
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
42
|
+
var GENUI_CATALOG = /* @__PURE__ */ Symbol.for("@dudousxd/nestjs-agent:genui-catalog");
|
|
43
|
+
var GENUI_OPTIONS = /* @__PURE__ */ Symbol.for("@dudousxd/nestjs-agent:genui-options");
|
|
44
|
+
var InjectGenuiCatalog = /* @__PURE__ */ __name(() => Inject(GENUI_CATALOG), "InjectGenuiCatalog");
|
|
45
|
+
var GenuiCatalogResolver = class {
|
|
46
|
+
static {
|
|
47
|
+
__name(this, "GenuiCatalogResolver");
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
function resolverProviders(resolver) {
|
|
51
|
+
if (resolver === void 0) return [];
|
|
52
|
+
return [
|
|
53
|
+
typeof resolver === "function" ? {
|
|
54
|
+
provide: GenuiCatalogResolver,
|
|
55
|
+
useClass: resolver
|
|
56
|
+
} : {
|
|
57
|
+
provide: GenuiCatalogResolver,
|
|
58
|
+
useValue: resolver
|
|
59
|
+
}
|
|
60
|
+
];
|
|
61
|
+
}
|
|
62
|
+
__name(resolverProviders, "resolverProviders");
|
|
63
|
+
function toolOptions(options, resolver) {
|
|
64
|
+
const { catalog: _catalog, ...rest } = options;
|
|
65
|
+
return {
|
|
66
|
+
...rest,
|
|
67
|
+
...resolver !== void 0 ? {
|
|
68
|
+
resolveCatalog: /* @__PURE__ */ __name((scope) => resolver.resolve(scope), "resolveCatalog")
|
|
69
|
+
} : {}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
__name(toolOptions, "toolOptions");
|
|
73
|
+
function coreProviders(resolver) {
|
|
74
|
+
return [
|
|
75
|
+
{
|
|
76
|
+
provide: GENUI_CATALOG,
|
|
77
|
+
useFactory: /* @__PURE__ */ __name((options) => options.catalog ?? defineCatalog([]), "useFactory"),
|
|
78
|
+
inject: [
|
|
79
|
+
GENUI_OPTIONS
|
|
80
|
+
]
|
|
81
|
+
},
|
|
82
|
+
...resolverProviders(resolver),
|
|
83
|
+
// The generated tools, registered by the agent module's discovery like any `provideAgentTool`.
|
|
84
|
+
// Built from the injected catalog, so `overrideProvider(GENUI_CATALOG)` changes them too.
|
|
85
|
+
provideAgentTools((options, catalog, perRequest) => genuiTools(catalog, toolOptions(options, perRequest)), [
|
|
86
|
+
GENUI_OPTIONS,
|
|
87
|
+
GENUI_CATALOG,
|
|
88
|
+
{
|
|
89
|
+
token: GenuiCatalogResolver,
|
|
90
|
+
optional: true
|
|
91
|
+
}
|
|
92
|
+
])
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
__name(coreProviders, "coreProviders");
|
|
96
|
+
var EXPORTS = [
|
|
97
|
+
GENUI_CATALOG,
|
|
98
|
+
GENUI_OPTIONS
|
|
99
|
+
];
|
|
100
|
+
var AgentGenuiModule = class _AgentGenuiModule {
|
|
101
|
+
static {
|
|
102
|
+
__name(this, "AgentGenuiModule");
|
|
103
|
+
}
|
|
104
|
+
static forRoot(options = {}) {
|
|
105
|
+
const { resolver, ...rest } = options;
|
|
106
|
+
return {
|
|
107
|
+
module: _AgentGenuiModule,
|
|
108
|
+
global: true,
|
|
109
|
+
providers: [
|
|
110
|
+
{
|
|
111
|
+
provide: GENUI_OPTIONS,
|
|
112
|
+
useValue: rest
|
|
113
|
+
},
|
|
114
|
+
...coreProviders(resolver)
|
|
115
|
+
],
|
|
116
|
+
exports: resolver === void 0 ? EXPORTS : [
|
|
117
|
+
...EXPORTS,
|
|
118
|
+
GenuiCatalogResolver
|
|
119
|
+
]
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
static forRootAsync(options) {
|
|
123
|
+
return {
|
|
124
|
+
module: _AgentGenuiModule,
|
|
125
|
+
global: true,
|
|
126
|
+
imports: options.imports ?? [],
|
|
127
|
+
providers: [
|
|
128
|
+
{
|
|
129
|
+
provide: GENUI_OPTIONS,
|
|
130
|
+
useFactory: options.useFactory,
|
|
131
|
+
inject: options.inject ?? []
|
|
132
|
+
},
|
|
133
|
+
...coreProviders(options.resolver)
|
|
134
|
+
],
|
|
135
|
+
exports: options.resolver === void 0 ? EXPORTS : [
|
|
136
|
+
...EXPORTS,
|
|
137
|
+
GenuiCatalogResolver
|
|
138
|
+
]
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
AgentGenuiModule = _ts_decorate([
|
|
143
|
+
Module({})
|
|
144
|
+
], AgentGenuiModule);
|
|
145
|
+
|
|
146
|
+
// src/genui/index.ts
|
|
147
|
+
export * from "@dudousxd/nestjs-agent-core/genui";
|
|
148
|
+
export {
|
|
149
|
+
AgentGenuiModule,
|
|
150
|
+
GENUI_CATALOG,
|
|
151
|
+
GENUI_OPTIONS,
|
|
152
|
+
GenuiCatalogResolver,
|
|
153
|
+
InjectGenuiCatalog
|
|
154
|
+
};
|
|
155
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/genui/agent-genui.module.ts","../../src/functional-tool.ts","../../src/genui/index.ts"],"sourcesContent":["import type { ToolPresentation } from '@dudousxd/nestjs-agent-core';\nimport type {\n Catalog,\n GenuiCatalogScope,\n GenuiToolsOptions,\n TreeLimits,\n} from '@dudousxd/nestjs-agent-core/genui';\nimport { defineCatalog, genuiTools } from '@dudousxd/nestjs-agent-core/genui';\nimport {\n type DynamicModule,\n type FactoryProvider,\n Inject,\n Module,\n type ModuleMetadata,\n type Provider,\n type Type,\n} from '@nestjs/common';\nimport { provideAgentTools } from '../functional-tool.js';\n\n/** The app's genui catalog — `@InjectGenuiCatalog()` it anywhere; `overrideProvider` it in tests. */\nexport const GENUI_CATALOG = Symbol.for('@dudousxd/nestjs-agent:genui-catalog');\n\n/** The resolved {@link AgentGenuiOptions} (without the resolver). */\nexport const GENUI_OPTIONS = Symbol.for('@dudousxd/nestjs-agent:genui-options');\n\n/** Inject the app's genui catalog ({@link GENUI_CATALOG}). */\nexport const InjectGenuiCatalog = (): ParameterDecorator & PropertyDecorator =>\n Inject(GENUI_CATALOG);\n\n/**\n * Picks the catalog for ONE request — a tenant's own, versioned components. Consulted when a genui\n * call is validated and when the turn's tool list is described to the model, so components can\n * change without rebuilding tools at boot. Cache in here if resolving costs a round trip: it runs\n * once per turn per genui tool, and once per call.\n *\n * Provide it through `AgentGenuiModule.forRoot({ resolver })` (a class, resolved with DI, or an\n * instance); it is then injectable, and replaceable in a test with\n * `overrideProvider(GenuiCatalogResolver)`.\n */\nexport abstract class GenuiCatalogResolver {\n abstract resolve(scope: GenuiCatalogScope): Catalog | Promise<Catalog>;\n}\n\n/** What {@link AgentGenuiModule} builds tools from. */\nexport interface AgentGenuiOptions {\n /**\n * The catalog. With a {@link GenuiCatalogResolver} it is the boot-time one — it names the\n * per-component tools — and may be empty. Default: an empty catalog.\n */\n catalog?: Catalog;\n /** `per-component` (default): one `ui__show_<snake>` tool per component. `tree`: one tool taking a composed tree. */\n mode?: 'per-component' | 'tree';\n /** End the model's turn once a genui call succeeds (no narrating follow-up call). */\n terminal?: boolean;\n /** Tool name in `tree` mode. Default `ui__render`. */\n treeToolName?: string;\n /** Text prepended to the tree tool's description. */\n treeInstructions?: string;\n treeLimits?: TreeLimits;\n /** Tool-name prefix in `per-component` mode. Default `ui__show_`. */\n namePrefix?: string;\n /**\n * Also register ONE generic tool taking `{ component, props }`, validated against the request's\n * catalog — how a model reaches a tenant's own components, which no boot-time tool names. `true`\n * names it `ui__show`.\n */\n showTool?: boolean | string;\n /** Text prepended to the show tool's description. */\n showInstructions?: string;\n /** Roles allowed to call the tools. Undefined → the roles policy default. */\n roles?: string[];\n /** Override the generated presentation per component (or per tree / show tool name). */\n presentation?: (component: string) => ToolPresentation | undefined;\n}\n\ntype ResolverOption = Type<GenuiCatalogResolver> | GenuiCatalogResolver;\n\nexport interface AgentGenuiModuleOptions extends AgentGenuiOptions {\n /** Per-request catalogs. A class is instantiated with DI (its dependencies must be global or exported to the root). */\n resolver?: ResolverOption;\n}\n\nexport interface AgentGenuiModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {\n inject?: FactoryProvider['inject'];\n useFactory: (...deps: any[]) => AgentGenuiOptions | Promise<AgentGenuiOptions>;\n /** Per-request catalogs. A class resolves its dependencies from `imports`. */\n resolver?: ResolverOption;\n}\n\nfunction resolverProviders(resolver: ResolverOption | undefined): Provider[] {\n if (resolver === undefined) return [];\n return [\n typeof resolver === 'function'\n ? { provide: GenuiCatalogResolver, useClass: resolver }\n : { provide: GenuiCatalogResolver, useValue: resolver },\n ];\n}\n\nfunction toolOptions(\n options: AgentGenuiOptions,\n resolver: GenuiCatalogResolver | undefined,\n): GenuiToolsOptions {\n const { catalog: _catalog, ...rest } = options;\n return {\n ...rest,\n ...(resolver !== undefined\n ? { resolveCatalog: (scope: GenuiCatalogScope) => resolver.resolve(scope) }\n : {}),\n };\n}\n\nfunction coreProviders(resolver: ResolverOption | undefined): Provider[] {\n return [\n {\n provide: GENUI_CATALOG,\n useFactory: (options: AgentGenuiOptions) => options.catalog ?? defineCatalog([]),\n inject: [GENUI_OPTIONS],\n },\n ...resolverProviders(resolver),\n // The generated tools, registered by the agent module's discovery like any `provideAgentTool`.\n // Built from the injected catalog, so `overrideProvider(GENUI_CATALOG)` changes them too.\n provideAgentTools(\n (options: AgentGenuiOptions, catalog: Catalog, perRequest?: GenuiCatalogResolver) =>\n genuiTools(catalog, toolOptions(options, perRequest)),\n [GENUI_OPTIONS, GENUI_CATALOG, { token: GenuiCatalogResolver, optional: true }],\n ),\n ];\n}\n\nconst EXPORTS = [GENUI_CATALOG, GENUI_OPTIONS];\n\n/**\n * Generative UI for the agent: registers the tools that let the model push catalog components\n * (`ui__show_<component>`, or one tree tool, or the generic `ui__show`), and makes the catalog\n * injectable. Global, like `AgentModule`; import it once, next to it.\n *\n * ```ts\n * AgentGenuiModule.forRoot({ catalog, mode: 'tree', terminal: true })\n * ```\n */\n@Module({})\nexport class AgentGenuiModule {\n static forRoot(options: AgentGenuiModuleOptions = {}): DynamicModule {\n const { resolver, ...rest } = options;\n return {\n module: AgentGenuiModule,\n global: true,\n providers: [{ provide: GENUI_OPTIONS, useValue: rest }, ...coreProviders(resolver)],\n exports: resolver === undefined ? EXPORTS : [...EXPORTS, GenuiCatalogResolver],\n };\n }\n\n static forRootAsync(options: AgentGenuiModuleAsyncOptions): DynamicModule {\n return {\n module: AgentGenuiModule,\n global: true,\n imports: options.imports ?? [],\n providers: [\n { provide: GENUI_OPTIONS, useFactory: options.useFactory, inject: options.inject ?? [] },\n ...coreProviders(options.resolver),\n ],\n exports: options.resolver === undefined ? EXPORTS : [...EXPORTS, GenuiCatalogResolver],\n };\n }\n}\n","import type { ToolHandler, ToolSpec } from '@dudousxd/nestjs-agent-core';\nimport type { FactoryProvider, Provider } from '@nestjs/common';\n\n/**\n * Brand stamped onto a functional-tool provider instance so `AiToolDiscoveryService` picks it up\n * when it walks every provider at boot — the declarative alternative to a manual `registry.register`.\n */\nexport const AGENT_TOOL_BRAND = Symbol.for('@dudousxd/nestjs-agent:functional-tool');\n\n/** A tool expressed as data + handler (e.g. from `createExecuteSqlTool`), not an `@AiTool` class. */\nexport interface FunctionalTool {\n spec: ToolSpec;\n handler: ToolHandler;\n}\n\n/** A {@link FunctionalTool} carrying {@link AGENT_TOOL_BRAND} — what a provider resolves to. */\nexport interface BrandedFunctionalTool extends FunctionalTool {\n readonly [AGENT_TOOL_BRAND]: true;\n}\n\nfunction brand(tool: FunctionalTool): BrandedFunctionalTool {\n return { [AGENT_TOOL_BRAND]: true, spec: tool.spec, handler: tool.handler };\n}\n\n/** Narrows a provider instance to a branded functional tool for boot-time registration. */\nexport function isBrandedFunctionalTool(value: object): value is BrandedFunctionalTool {\n return AGENT_TOOL_BRAND in value && 'spec' in value && 'handler' in value;\n}\n\n/**\n * Registers a functional tool declaratively — drop the returned provider in a module's `providers`\n * and `AiToolDiscoveryService` auto-registers it at boot. Each call uses a fresh, unique provide\n * token so the same tool can be provided many times without collisions.\n *\n * Two forms: a static tool (`useValue`) or a factory with `inject` for DI-resolved dependencies\n * (`useFactory`). Either way the resolved instance is branded with {@link AGENT_TOOL_BRAND}.\n */\nexport function provideAgentTool(tool: FunctionalTool): Provider;\nexport function provideAgentTool(\n factory: (...deps: never[]) => FunctionalTool,\n inject?: FactoryProvider['inject'],\n): Provider;\nexport function provideAgentTool(\n factoryOrTool: FunctionalTool | ((...deps: never[]) => FunctionalTool),\n inject?: FactoryProvider['inject'],\n): Provider {\n const provide = Symbol('nestjs-agent:functional-tool-instance');\n if (typeof factoryOrTool === 'function') {\n return {\n provide,\n useFactory: (...deps: never[]) => brand(factoryOrTool(...deps)),\n inject: inject ?? [],\n };\n }\n return { provide, useValue: brand(factoryOrTool) };\n}\n\n/**\n * Registers a LIST of functional tools from one factory — for a producer whose tool count is only\n * known once its dependencies resolve (a catalog loaded from config, one tool per entry). The\n * factory's result is branded element by element; `AiToolDiscoveryService` registers each.\n */\nexport function provideAgentTools(\n factory: (...deps: never[]) => readonly FunctionalTool[] | Promise<readonly FunctionalTool[]>,\n inject?: FactoryProvider['inject'],\n): Provider {\n return {\n provide: Symbol('nestjs-agent:functional-tool-list'),\n useFactory: async (...deps: never[]) => (await factory(...deps)).map(brand),\n inject: inject ?? [],\n };\n}\n","/**\n * `@dudousxd/nestjs-agent/genui` — generative UI for NestJS: {@link AgentGenuiModule} registers the\n * tools that push catalog components, `GENUI_CATALOG` / `@InjectGenuiCatalog()` expose the catalog,\n * and {@link GenuiCatalogResolver} serves per-request (per-tenant) catalogs. The catalog itself —\n * `defineComponent`, `defineCatalog`, validation, text fallbacks — is re-exported from\n * `@dudousxd/nestjs-agent-core/genui`, the isomorphic entry a browser imports too.\n */\nexport {\n AgentGenuiModule,\n type AgentGenuiModuleAsyncOptions,\n type AgentGenuiModuleOptions,\n type AgentGenuiOptions,\n GENUI_CATALOG,\n GENUI_OPTIONS,\n GenuiCatalogResolver,\n InjectGenuiCatalog,\n} from './agent-genui.module.js';\nexport * from '@dudousxd/nestjs-agent-core/genui';\n"],"mappings":";;;;AAOA,SAASA,eAAeC,kBAAkB;AAC1C,SAGEC,QACAC,cAIK;;;ACTA,IAAMC,mBAAmBC,uBAAOC,IAAI,wCAAA;AAa3C,SAASC,MAAMC,MAAoB;AACjC,SAAO;IAAE,CAACJ,gBAAAA,GAAmB;IAAMK,MAAMD,KAAKC;IAAMC,SAASF,KAAKE;EAAQ;AAC5E;AAFSH;AA0CF,SAASI,kBACdC,SACAC,QAAkC;AAElC,SAAO;IACLC,SAASC,uBAAO,mCAAA;IAChBC,YAAY,iCAAUC,UAAmB,MAAML,QAAAA,GAAWK,IAAAA,GAAOC,IAAIC,KAAAA,GAAzD;IACZN,QAAQA,UAAU,CAAA;EACpB;AACF;AATgBF;;;;;;;;;;;;;;;;;AD1CT,IAAMS,gBAAgBC,uBAAOC,IAAI,sCAAA;AAGjC,IAAMC,gBAAgBF,uBAAOC,IAAI,sCAAA;AAGjC,IAAME,qBAAqB,6BAChCC,OAAOL,aAAAA,GADyB;AAa3B,IAAeM,uBAAf,MAAeA;SAAAA;;;AAEtB;AAgDA,SAASC,kBAAkBC,UAAoC;AAC7D,MAAIA,aAAaC,OAAW,QAAO,CAAA;AACnC,SAAO;IACL,OAAOD,aAAa,aAChB;MAAEE,SAASJ;MAAsBK,UAAUH;IAAS,IACpD;MAAEE,SAASJ;MAAsBM,UAAUJ;IAAS;;AAE5D;AAPSD;AAST,SAASM,YACPC,SACAN,UAA0C;AAE1C,QAAM,EAAEO,SAASC,UAAU,GAAGC,KAAAA,IAASH;AACvC,SAAO;IACL,GAAGG;IACH,GAAIT,aAAaC,SACb;MAAES,gBAAgB,wBAACC,UAA6BX,SAASY,QAAQD,KAAAA,GAA/C;IAAsD,IACxE,CAAC;EACP;AACF;AAXSN;AAaT,SAASQ,cAAcb,UAAoC;AACzD,SAAO;IACL;MACEE,SAASV;MACTsB,YAAY,wBAACR,YAA+BA,QAAQC,WAAWQ,cAAc,CAAA,CAAE,GAAnE;MACZC,QAAQ;QAACrB;;IACX;OACGI,kBAAkBC,QAAAA;;;IAGrBiB,kBACE,CAACX,SAA4BC,SAAkBW,eAC7CC,WAAWZ,SAASF,YAAYC,SAASY,UAAAA,CAAAA,GAC3C;MAACvB;MAAeH;MAAe;QAAE4B,OAAOtB;QAAsBuB,UAAU;MAAK;KAAE;;AAGrF;AAhBSR;AAkBT,IAAMS,UAAU;EAAC9B;EAAeG;;AAYzB,IAAM4B,mBAAN,MAAMA,kBAAAA;SAAAA;;;EACX,OAAOC,QAAQlB,UAAmC,CAAC,GAAkB;AACnE,UAAM,EAAEN,UAAU,GAAGS,KAAAA,IAASH;AAC9B,WAAO;MACLmB,QAAQF;MACRG,QAAQ;MACRC,WAAW;QAAC;UAAEzB,SAASP;UAAeS,UAAUK;QAAK;WAAMI,cAAcb,QAAAA;;MACzE4B,SAAS5B,aAAaC,SAAYqB,UAAU;WAAIA;QAASxB;;IAC3D;EACF;EAEA,OAAO+B,aAAavB,SAAsD;AACxE,WAAO;MACLmB,QAAQF;MACRG,QAAQ;MACRI,SAASxB,QAAQwB,WAAW,CAAA;MAC5BH,WAAW;QACT;UAAEzB,SAASP;UAAemB,YAAYR,QAAQQ;UAAYE,QAAQV,QAAQU,UAAU,CAAA;QAAG;WACpFH,cAAcP,QAAQN,QAAQ;;MAEnC4B,SAAStB,QAAQN,aAAaC,SAAYqB,UAAU;WAAIA;QAASxB;;IACnE;EACF;AACF;;;;;;AEnJA,cAAc;","names":["defineCatalog","genuiTools","Inject","Module","AGENT_TOOL_BRAND","Symbol","for","brand","tool","spec","handler","provideAgentTools","factory","inject","provide","Symbol","useFactory","deps","map","brand","GENUI_CATALOG","Symbol","for","GENUI_OPTIONS","InjectGenuiCatalog","Inject","GenuiCatalogResolver","resolverProviders","resolver","undefined","provide","useClass","useValue","toolOptions","options","catalog","_catalog","rest","resolveCatalog","scope","resolve","coreProviders","useFactory","defineCatalog","inject","provideAgentTools","perRequest","genuiTools","token","optional","EXPORTS","AgentGenuiModule","forRoot","module","global","providers","exports","forRootAsync","imports"]}
|