bazis 0.137.0 → 0.139.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 +19 -8
- package/cli/generateProject.ts +11 -18
- package/cli/main.ts +1 -1
- package/cli/templates/passport.ts +1 -1
- package/core/access/catalog/moduleFields.ts +25 -2
- package/core/access/testing/index.ts +26 -4
- package/core/agent/AgentContextBuilder.ts +119 -16
- package/core/agent/AgentRuntime.ts +8 -0
- package/core/agent/AgentToolExecutor.ts +7 -0
- package/core/agent/ModelProvider.ts +6 -0
- package/core/agent/README.md +35 -3
- package/core/agent/ToolRunner.ts +2 -1
- package/core/agent/defineTool.ts +109 -0
- package/core/agent/errors.ts +32 -0
- package/core/agent/index.ts +15 -3
- package/core/agent/internal/AgentExecutionDriver.ts +598 -24
- package/core/agent/internal/AgentExecutionState.ts +26 -3
- package/core/agent/moduleContributions-v1.ts +3 -10
- package/core/agent/semantic.ts +60 -1
- package/core/app/index.ts +1 -1
- package/core/app/messages.ts +3 -3
- package/core/app/runApp.ts +90 -26
- package/core/cache/ICache.ts +1 -1
- package/core/cache/README.md +4 -2
- package/core/cache/SPEC.md +19 -12
- package/core/cache/http/missingCacheModuleCheck.ts +5 -4
- package/core/cache/internal/buildCacheProviders.ts +4 -5
- package/core/cache/providers/cachedProviders.ts +1 -1
- package/core/di/README.md +10 -3
- package/core/di/index.ts +0 -4
- package/core/di/lowLevel.ts +12 -0
- package/core/di/module/index.ts +0 -3
- package/core/di/module/shortcuts.ts +31 -86
- package/core/generatedRuntime.ts +11 -0
- package/core/grpc/Grpc.module.ts +2 -1
- package/core/grpc/grpcClients.ts +2 -1
- package/core/http/Authorization/metadata.ts +1 -1
- package/core/http/Errors/HttpError.ts +1 -1
- package/core/http/Http.config.ts +22 -0
- package/core/http/HttpContext/HttpContext.ts +1 -1
- package/core/http/HttpServer.ts +10 -7
- package/core/http/OpenApi/openApiDocument.ts +1 -1
- package/core/http/httpModule.ts +10 -8
- package/core/http/index.ts +1 -0
- package/core/http/options.ts +5 -1
- package/core/http-client/httpClients.ts +2 -1
- package/core/infra/Infra.ts +3 -3
- package/core/infra/anthropic.ts +11 -0
- package/core/infra/connectors/anthropic.ts +440 -0
- package/core/infra/connectors/codex/CodexAppServerClient.ts +1 -1
- package/core/infra/connectors/codex/contracts.ts +3 -3
- package/core/infra/connectors/llm.ts +14 -1
- package/core/infra/connectors/llmRouter.ts +12 -0
- package/core/infra/connectors/openaiCompatible.ts +128 -63
- package/core/infra/connectors/openaiTextStream.ts +5 -2
- package/core/infra/connectors/redis.ts +2 -1
- package/core/infra/connectors/toolNames.ts +49 -0
- package/core/kernel/KernelBuilder.ts +2 -1
- package/core/kernel/config/ConfigRegistry.ts +2 -1
- package/core/kernel/config/addConfigOptions.ts +2 -1
- package/core/kernel/config/defineConfig.ts +1 -1
- package/core/kernel/events/EventBus.ts +1 -0
- package/core/kernel/events/eventSubscribers.ts +1 -1
- package/core/kernel/events/onEvent.ts +2 -1
- package/core/orm/buildOrmProviders.ts +2 -1
- package/core/orm/encryptedWith.ts +1 -1
- package/core/orm/ormModule.ts +2 -1
- package/core/orm/registerRepositories.ts +2 -1
- package/core/scripts/di-generate.ts +52 -8
- package/core/testing/index.ts +51 -11
- package/core/tools/index.ts +1 -1
- package/core/tools/orm/ormEntityTools.ts +54 -28
- package/core/websocket/DESIGN.md +7 -5
- package/core/websocket/middleware.ts +1 -1
- package/core/websocket/websocketModule.ts +3 -3
- package/core/workflow/definitions_module/WorkflowDefinitions.module.ts +1 -2
- package/core/workflow/definitions_module/code/defineProcess.ts +213 -11
- package/core/workflow/definitions_module/code/inspectProcesses.ts +12 -2
- package/core/workflow/index.ts +14 -1
- package/core/workflow/runtime_module/services/IWorkflowRuntime.service.ts +10 -6
- package/core/workflow/runtime_module/services/WorkflowRuntime.service.ts +5 -2
- package/core/workflow/testing/index.ts +46 -4
- package/index.ts +9 -3
- package/library/orm/Repository/README.md +14 -11
- package/library/terminal/messages.ts +6 -6
- package/library/validation/README.md +1 -1
- package/library/validation/SPEC.md +1 -1
- package/library/workflow/index.ts +1 -0
- package/library/workflow/nodeConfigs.ts +130 -0
- package/package.json +8 -2
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { ServiceProvider, type Class } from "../di";
|
|
2
|
+
import { registerFrameworkClassDeps } from "../di/module/autoDeps";
|
|
3
|
+
import { Tool } from "./decorators/Tool";
|
|
4
|
+
import { AgentSetupError } from "./errors";
|
|
5
|
+
import { toolMetadataOf, type ToolOptions } from "./metadata";
|
|
6
|
+
import type { AgentToolExecutionContext } from "./AgentToolExecutor";
|
|
7
|
+
|
|
8
|
+
declare const TOOL_TYPES: unique symbol;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A Tool made by {@link defineTool}. It stands in `@Module({ tools })` next to
|
|
12
|
+
* the classes with `@Tool`, and a process names it by reference.
|
|
13
|
+
*/
|
|
14
|
+
export type DefinedTool<I = unknown, O = unknown> = Class<object> & {
|
|
15
|
+
/** The name it is called by: `orm.order.find`. */
|
|
16
|
+
readonly toolName: string;
|
|
17
|
+
/** Only for the types: what `execute` takes and gives back. */
|
|
18
|
+
readonly [TOOL_TYPES]?: { readonly input: I; readonly output: O };
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** Several made Tools under the names their factory gave them: `orderTools.find`. */
|
|
22
|
+
export type ToolSet<K extends string = string> = { readonly [P in K]: DefinedTool };
|
|
23
|
+
|
|
24
|
+
/** What `defineTool` takes: what `@Tool` takes, without the classes of input and output, and the work itself. */
|
|
25
|
+
export interface DefinedToolOptions<I = unknown, O = unknown> extends Omit<ToolOptions, "input" | "output"> {
|
|
26
|
+
/**
|
|
27
|
+
* Does the work. `services` is the scope of the call: `services.resolve(OrdersDbContext)`.
|
|
28
|
+
* The input comes as the caller sent it: check it here.
|
|
29
|
+
*/
|
|
30
|
+
execute(input: I, context: AgentToolExecutionContext, services: ServiceProvider): O | Promise<O>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const DEFINED = new WeakSet<object>();
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A Tool without a class: for Tools made by a function, when their number and
|
|
37
|
+
* names are known only from its arguments. A Tool written by hand is a class
|
|
38
|
+
* with `@Tool`.
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* export function exportTools(formats: readonly string[]): ToolSet {
|
|
42
|
+
* return Object.fromEntries(formats.map((format) => [format, defineTool({
|
|
43
|
+
* name: `reports.export.${format}`,
|
|
44
|
+
* description: `Exports a report as ${format}.`,
|
|
45
|
+
* sideEffect: "read",
|
|
46
|
+
* callers: ["workflow"],
|
|
47
|
+
* inputSchema: { type: "object", properties: { reportId: { type: "string" } }, required: ["reportId"] },
|
|
48
|
+
* execute: (input: { reportId: string }, _context, services) => services.resolve(ReportService).export(input.reportId, format),
|
|
49
|
+
* })]));
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* @Module({ tools: [SendReportTool, exportTools(["pdf", "csv"])] })
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export function defineTool<I = unknown, O = unknown>(definition: DefinedToolOptions<I, O>): DefinedTool<I, O> {
|
|
56
|
+
if (definition === null || typeof definition !== "object" || typeof definition.execute !== "function") {
|
|
57
|
+
throw new AgentSetupError("defineTool takes the description of a Tool with its execute(input, context, services).");
|
|
58
|
+
}
|
|
59
|
+
const { execute, ...options } = definition;
|
|
60
|
+
|
|
61
|
+
@Tool(options)
|
|
62
|
+
class MadeTool {
|
|
63
|
+
static readonly toolName = String(options.name).trim();
|
|
64
|
+
|
|
65
|
+
constructor(private readonly services: ServiceProvider) {}
|
|
66
|
+
|
|
67
|
+
execute(input: unknown, context: AgentToolExecutionContext): unknown {
|
|
68
|
+
return execute(input as I, context, this.services);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The class is made here, so codegen never sees its constructor.
|
|
73
|
+
registerFrameworkClassDeps([[MadeTool, [ServiceProvider]]]);
|
|
74
|
+
DEFINED.add(MadeTool);
|
|
75
|
+
Object.defineProperty(MadeTool, "name", { value: `Tool(${MadeTool.toolName})` });
|
|
76
|
+
return MadeTool as unknown as DefinedTool<I, O>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Whether a value came from {@link defineTool}. */
|
|
80
|
+
export function isDefinedTool(value: unknown): value is DefinedTool {
|
|
81
|
+
return typeof value === "function" && DEFINED.has(value);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @internal The Tools a module lists in `tools`: classes with `@Tool`, made Tools
|
|
86
|
+
* and sets of them, as one list of classes.
|
|
87
|
+
*/
|
|
88
|
+
export function moduleToolsOf(value: unknown): readonly Class<object>[] {
|
|
89
|
+
if (value === undefined) return [];
|
|
90
|
+
if (!Array.isArray(value)) throw new AgentSetupError("tools must be an array of classes with @Tool, Tools made by defineTool and sets of them.");
|
|
91
|
+
const out: Class<object>[] = [];
|
|
92
|
+
for (const entry of value) {
|
|
93
|
+
if (typeof entry === "function") {
|
|
94
|
+
out.push(entry as Class<object>);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const members = typeof entry === "object" && entry !== null && !Array.isArray(entry) ? Object.entries(entry) : undefined;
|
|
98
|
+
if (members === undefined || members.length === 0 || members.some(([, member]) => !isDefinedTool(member))) {
|
|
99
|
+
throw new AgentSetupError("tools must list classes with @Tool, Tools made by defineTool and sets of them: an object in the list must hold only Tools made by defineTool.");
|
|
100
|
+
}
|
|
101
|
+
for (const [, member] of members) out.push(member as Class<object>);
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The name a class with `@Tool` or a made Tool is called by; `undefined` for anything else. */
|
|
107
|
+
export function toolNameOf(tool: unknown): string | undefined {
|
|
108
|
+
return typeof tool === "function" ? toolMetadataOf(tool as Class<object>)?.name : undefined;
|
|
109
|
+
}
|
package/core/agent/errors.ts
CHANGED
|
@@ -69,6 +69,38 @@ export class AgentModelTimeoutError extends Error {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/**
|
|
73
|
+
* A model provider could not serve the request right now: it is overloaded,
|
|
74
|
+
* limits the rate (HTTP 429), answered 5xx or the connection broke. The runtime
|
|
75
|
+
* repeats such a call (`AgentRuntimeOptions.modelRetry`); any other provider
|
|
76
|
+
* error fails the run at once.
|
|
77
|
+
*/
|
|
78
|
+
export class AgentModelUnavailableError extends Error {
|
|
79
|
+
/** How long the provider asked to wait (`Retry-After`), when it said so. */
|
|
80
|
+
readonly retryAfterMs?: number;
|
|
81
|
+
|
|
82
|
+
constructor(message: string, options: { readonly retryAfterMs?: number } = {}) {
|
|
83
|
+
super(message);
|
|
84
|
+
this.name = "AgentModelUnavailableError";
|
|
85
|
+
if (options.retryAfterMs !== undefined && Number.isFinite(options.retryAfterMs) && options.retryAfterMs >= 0) {
|
|
86
|
+
this.retryAfterMs = Math.floor(options.retryAfterMs);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The request does not fit the model's context window. The runtime replaces
|
|
93
|
+
* the older part of the dialog with a summary and sends the request again
|
|
94
|
+
* (`AgentRuntimeOptions.compaction`); a run that still does not fit fails
|
|
95
|
+
* with AGENT_CONTEXT_OVERFLOW.
|
|
96
|
+
*/
|
|
97
|
+
export class AgentModelContextOverflowError extends Error {
|
|
98
|
+
constructor(message: string) {
|
|
99
|
+
super(message);
|
|
100
|
+
this.name = "AgentModelContextOverflowError";
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
72
104
|
/** Error thrown when the agent runtime is configured incorrectly. */
|
|
73
105
|
export class AgentRuntimeError extends Error {
|
|
74
106
|
constructor(message: string) {
|
package/core/agent/index.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import type { Class } from "../di";
|
|
2
2
|
import { registerModuleDescriber } from "../kernel/report/moduleTree";
|
|
3
|
+
import { moduleToolsOf, type DefinedTool, type ToolSet } from "./defineTool";
|
|
3
4
|
import { toolMetadataOf } from "./metadata";
|
|
4
5
|
|
|
5
6
|
// The module tree of the startup screen: `orders.summary · read`.
|
|
6
|
-
registerModuleDescriber((metadata) => (metadata.tools
|
|
7
|
+
registerModuleDescriber((metadata) => moduleToolsOf(metadata.tools).map((tool) => {
|
|
7
8
|
const meta = toolMetadataOf(tool);
|
|
8
9
|
return { kind: "tool" as const, text: meta === undefined ? tool.name : `${meta.name} · ${meta.sideEffect}` };
|
|
9
10
|
}));
|
|
10
11
|
|
|
11
12
|
export { Tool } from "./decorators/Tool";
|
|
13
|
+
export { defineTool, isDefinedTool, toolNameOf, type DefinedTool, type DefinedToolOptions, type ToolSet } from "./defineTool";
|
|
12
14
|
export { describeTool } from "./Tool.contract";
|
|
13
15
|
export type {
|
|
14
16
|
AgentToolHookKindV1, AgentToolHookRefV1, AgentToolRequiredHookRefV1, AgentToolHookRegistrationBaseV1,
|
|
@@ -23,6 +25,8 @@ export {
|
|
|
23
25
|
AgentSetupError,
|
|
24
26
|
AgentToolExecutionError,
|
|
25
27
|
AgentModelTimeoutError,
|
|
28
|
+
AgentModelUnavailableError,
|
|
29
|
+
AgentModelContextOverflowError,
|
|
26
30
|
AgentToolPreCommitError,
|
|
27
31
|
ToolError,
|
|
28
32
|
} from "./errors";
|
|
@@ -31,6 +35,8 @@ export {
|
|
|
31
35
|
AgentRuntime,
|
|
32
36
|
type AgentRuntimeInvokeOptions,
|
|
33
37
|
type AgentRuntimeOptions,
|
|
38
|
+
type AgentModelRetryPolicy,
|
|
39
|
+
type AgentCompactionOptions,
|
|
34
40
|
type AgentRuntimeResult,
|
|
35
41
|
type AgentRuntimeStatus,
|
|
36
42
|
type AgentRuntimeToolCallContext,
|
|
@@ -100,6 +106,7 @@ export {
|
|
|
100
106
|
agentInvocation,
|
|
101
107
|
agentJsonSchema,
|
|
102
108
|
agentMessage,
|
|
109
|
+
agentMessageText,
|
|
103
110
|
agentModelCapabilities,
|
|
104
111
|
agentModelRequest,
|
|
105
112
|
agentModelResponse,
|
|
@@ -110,12 +117,14 @@ export {
|
|
|
110
117
|
agentToolContract,
|
|
111
118
|
agentToolResult,
|
|
112
119
|
agentToolResultPart,
|
|
120
|
+
agentReasoning,
|
|
113
121
|
normalizeJsonValue,
|
|
114
122
|
type AgentClassSchemaContract,
|
|
115
123
|
type AgentContentInput,
|
|
116
124
|
type AgentContentPart,
|
|
117
125
|
type AgentDataPart,
|
|
118
126
|
type AgentFilePart,
|
|
127
|
+
type AgentReasoningPart,
|
|
119
128
|
type AgentFinishReason,
|
|
120
129
|
type AgentImagePart,
|
|
121
130
|
type AgentInvocation,
|
|
@@ -149,8 +158,11 @@ export {
|
|
|
149
158
|
// depend on program file order and silently stop merging in some projects.
|
|
150
159
|
declare module "../di/module/types/BazisModule" {
|
|
151
160
|
interface BazisModuleMetadata {
|
|
152
|
-
/**
|
|
153
|
-
|
|
161
|
+
/**
|
|
162
|
+
* The Tools of the module: classes with `@Tool`, Tools made by `defineTool`
|
|
163
|
+
* and sets of them (`ormEntityTools(...)`). An agent definition names them in `toolNames`.
|
|
164
|
+
*/
|
|
165
|
+
readonly tools?: readonly (Class<object> | DefinedTool | ToolSet)[];
|
|
154
166
|
readonly agentToolHooks?: readonly import("./AgentToolHooks").AgentToolHookRegistrationV1[];
|
|
155
167
|
}
|
|
156
168
|
}
|