@rivus/agent 0.14.2 → 0.14.4
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 +1 -1
- package/dist/acp.js +1 -2
- package/dist/bootstrap/pi-feishu.d.ts +1 -1
- package/dist/bootstrap/pi-feishu.js +4 -4
- package/dist/chunks/agent-loop.d.ts +55 -300
- package/dist/chunks/agent-loop.js +3 -1123
- package/dist/chunks/background-session-authority.js +230 -0
- package/dist/chunks/background-session-control-input.js +51 -0
- package/dist/chunks/background-session-service.d.ts +382 -0
- package/dist/chunks/index.d.ts +1201 -538
- package/dist/chunks/pi-tool-proxy.d.ts +22 -90
- package/dist/chunks/pi.js +5 -2
- package/dist/chunks/rivus-agent-definition-resolver.js +508 -0
- package/dist/chunks/rivus-daemon-cli.js +2776 -3244
- package/dist/chunks/rivus-plugin-testkit.d.ts +175 -2
- package/dist/chunks/rivus-plugin-testkit.js +11 -4
- package/dist/chunks/rivus-skill.d.ts +95 -0
- package/dist/chunks/sha256-digest.js +2 -7
- package/dist/chunks/src.js +11941 -7001
- package/dist/chunks/tool-input-digest.js +158 -0
- package/dist/cli.js +764 -712
- package/dist/index.d.ts +6 -7
- package/dist/index.js +7 -8
- package/dist/mcp.d.ts +48 -9
- package/dist/mcp.js +146 -20
- package/dist/pi.d.ts +3 -4
- package/dist/pi.js +1 -1
- package/package.json +5 -5
- package/dist/chunks/api.d.ts +0 -70
- package/dist/chunks/api.js +0 -471
- package/dist/chunks/api2.d.ts +0 -387
- package/dist/chunks/api2.js +0 -1331
- package/dist/chunks/api3.d.ts +0 -402
- package/dist/chunks/module.js +0 -267
- package/dist/chunks/pi-skill-tool.js +0 -460
- package/dist/chunks/spi.d.ts +0 -1
- package/dist/chunks/spi.js +0 -2
|
@@ -1,9 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ot as RivusToolRisk, rt as RivusToolGrantSet } from "./api2.js";
|
|
1
|
+
import { y as RivusToolRisk } from "./rivus-skill.js";
|
|
3
2
|
import { Effect } from "effect";
|
|
4
3
|
import { TSchema } from "typebox";
|
|
5
4
|
|
|
6
|
-
//#region src/
|
|
5
|
+
//#region src/core/application/tool-execution/authority/invocation-authority-registry.d.ts
|
|
6
|
+
type InvocationMemoryScope = "conversation" | "agent-private" | "project" | "shared-user-profile";
|
|
7
|
+
interface InvocationMemoryAuthority {
|
|
8
|
+
readonly audience: "group" | "private";
|
|
9
|
+
readonly conversationId?: string;
|
|
10
|
+
readonly projectId?: string;
|
|
11
|
+
readonly scopes: ReadonlyArray<InvocationMemoryScope>;
|
|
12
|
+
readonly subjectId: string;
|
|
13
|
+
readonly tenantId: string;
|
|
14
|
+
}
|
|
15
|
+
interface InvocationToolGrantSet {
|
|
16
|
+
readonly revision: string;
|
|
17
|
+
readonly toolIds: ReadonlyArray<string>;
|
|
18
|
+
}
|
|
7
19
|
interface InvocationAuthorityRef {
|
|
8
20
|
readonly id: string;
|
|
9
21
|
}
|
|
@@ -13,84 +25,21 @@ interface InvocationAuthority {
|
|
|
13
25
|
readonly conversationId?: string;
|
|
14
26
|
readonly endpointId?: string;
|
|
15
27
|
readonly instanceId: string;
|
|
16
|
-
readonly memory?:
|
|
28
|
+
readonly memory?: InvocationMemoryAuthority;
|
|
17
29
|
readonly runId: string;
|
|
18
30
|
readonly sessionKey: string;
|
|
19
31
|
readonly sourceMessageId: string;
|
|
20
32
|
readonly tenantKey: string;
|
|
21
|
-
readonly toolGrantSet:
|
|
33
|
+
readonly toolGrantSet: InvocationToolGrantSet;
|
|
22
34
|
}
|
|
23
35
|
declare class InvalidInvocationAuthority extends Error {
|
|
24
36
|
readonly name = "InvalidInvocationAuthority";
|
|
25
37
|
}
|
|
26
38
|
//#endregion
|
|
27
|
-
//#region src/
|
|
28
|
-
|
|
29
|
-
//#endregion
|
|
30
|
-
//#region src/modules/tool-execution/domain/operation/tool-operation.d.ts
|
|
31
|
-
interface ToolOperationBinding {
|
|
32
|
-
readonly agentId: string;
|
|
33
|
-
readonly inputDigest: string;
|
|
34
|
-
readonly instanceId: string;
|
|
35
|
-
readonly sourceMessageId: string;
|
|
36
|
-
readonly toolId: string;
|
|
37
|
-
readonly toolVersion: string;
|
|
38
|
-
}
|
|
39
|
-
type ToolOperationState = {
|
|
40
|
-
readonly status: "pending";
|
|
41
|
-
} | {
|
|
42
|
-
readonly result: unknown;
|
|
43
|
-
readonly status: "completed";
|
|
44
|
-
} | {
|
|
45
|
-
readonly reason: string;
|
|
46
|
-
readonly status: "reconciliation-required";
|
|
47
|
-
} | {
|
|
48
|
-
readonly status: "aborted";
|
|
49
|
-
};
|
|
50
|
-
interface ToolOperationReconciliationAction {
|
|
51
|
-
readonly actorId: string;
|
|
52
|
-
readonly at: string;
|
|
53
|
-
readonly note: string;
|
|
54
|
-
}
|
|
55
|
-
interface ToolOperationReconciliation extends ToolOperationReconciliationAction {
|
|
56
|
-
readonly outcome: "applied" | "not-applied";
|
|
57
|
-
}
|
|
58
|
-
interface ToolOperationRecord {
|
|
59
|
-
readonly binding: ToolOperationBinding;
|
|
60
|
-
readonly operationId: string;
|
|
61
|
-
readonly reconciliation?: ToolOperationReconciliation;
|
|
62
|
-
readonly revision: number;
|
|
63
|
-
readonly state: ToolOperationState;
|
|
64
|
-
}
|
|
65
|
-
type ToolOperationReconciliationOutcome = {
|
|
66
|
-
readonly result: unknown;
|
|
67
|
-
readonly status: "applied";
|
|
68
|
-
} | {
|
|
69
|
-
readonly status: "not-applied";
|
|
70
|
-
};
|
|
71
|
-
type ToolOperationBeginResult = {
|
|
72
|
-
readonly status: "acquired";
|
|
73
|
-
} | {
|
|
74
|
-
readonly result: unknown;
|
|
75
|
-
readonly status: "completed";
|
|
76
|
-
} | {
|
|
77
|
-
readonly status: "blocked";
|
|
78
|
-
readonly reason: string;
|
|
79
|
-
};
|
|
80
|
-
type ToolOperationInspectResult = {
|
|
81
|
-
readonly status: "missing";
|
|
82
|
-
} | {
|
|
83
|
-
readonly result: unknown;
|
|
84
|
-
readonly status: "completed";
|
|
85
|
-
} | {
|
|
86
|
-
readonly status: "blocked";
|
|
87
|
-
readonly reason: string;
|
|
88
|
-
};
|
|
89
|
-
//#endregion
|
|
90
|
-
//#region src/modules/tool-execution/application/authority/invocation-authority-registry.d.ts
|
|
91
|
-
declare function createInvocationAuthority(authority: InvocationAuthority): InvocationAuthorityRef;
|
|
39
|
+
//#region src/core/application/tool-execution/authority/tool-risk-policy.d.ts
|
|
40
|
+
type ToolRisk = "observe" | "mutate" | "irreversible" | "host-control";
|
|
92
41
|
//#endregion
|
|
93
|
-
//#region src/
|
|
42
|
+
//#region src/core/application/tool-execution/brokerage/tool-broker-ports.d.ts
|
|
94
43
|
interface AuthorizationPolicyState {
|
|
95
44
|
readonly epoch: number;
|
|
96
45
|
readonly revokedToolIds: ReadonlyArray<string>;
|
|
@@ -107,10 +56,8 @@ interface ToolApprovalRequest {
|
|
|
107
56
|
readonly callId: string;
|
|
108
57
|
readonly toolId: string;
|
|
109
58
|
readonly toolVersion: string;
|
|
110
|
-
readonly risk:
|
|
59
|
+
readonly risk: ToolRisk;
|
|
111
60
|
}
|
|
112
|
-
//#endregion
|
|
113
|
-
//#region src/modules/tool-execution/application/brokerage/tool-broker.d.ts
|
|
114
61
|
interface ToolExecutionRequest {
|
|
115
62
|
readonly approvalId?: string;
|
|
116
63
|
readonly authority: InvocationAuthorityRef;
|
|
@@ -120,21 +67,6 @@ interface ToolExecutionRequest {
|
|
|
120
67
|
readonly toolId: string;
|
|
121
68
|
readonly version: string;
|
|
122
69
|
}
|
|
123
|
-
declare class ToolInvocationDenied extends Error {
|
|
124
|
-
readonly name = "ToolInvocationDenied";
|
|
125
|
-
}
|
|
126
|
-
//#endregion
|
|
127
|
-
//#region src/modules/tool-execution/domain/input/stable-tool-input.d.ts
|
|
128
|
-
declare class InvalidStableJson extends Error {
|
|
129
|
-
readonly name: string;
|
|
130
|
-
}
|
|
131
|
-
declare class InvalidToolInput extends InvalidStableJson {
|
|
132
|
-
readonly name = "InvalidToolInput";
|
|
133
|
-
}
|
|
134
|
-
declare function normalizeStableJson(value: unknown): unknown;
|
|
135
|
-
//#endregion
|
|
136
|
-
//#region src/modules/tool-execution/application/brokerage/tool-input-digest.d.ts
|
|
137
|
-
declare function createToolInputDigest(input: unknown): string;
|
|
138
70
|
//#endregion
|
|
139
71
|
//#region src/adapters/pi/tool-execution/pi-tool-definition.d.ts
|
|
140
72
|
type PiToolContent = {
|
|
@@ -185,4 +117,4 @@ interface PiToolApprovalGateway {
|
|
|
185
117
|
requestApproval(request: PiToolApprovalRequest): Promise<void>;
|
|
186
118
|
}
|
|
187
119
|
//#endregion
|
|
188
|
-
export {
|
|
120
|
+
export { PiToolResult as a, ToolExecutionRequest as c, InvocationAuthorityRef as d, PiToolDefinition as i, InvalidInvocationAuthority as l, PiToolApprovalRequest as n, AuthorizationPolicyState as o, PiToolContent as r, ToolApprovalRequest as s, PiToolApprovalGateway as t, InvocationAuthority as u };
|
package/dist/chunks/pi.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { l as toEffectAgentLoopInput } from "./agent-loop.js";
|
|
2
|
-
import "./
|
|
3
|
-
import {
|
|
2
|
+
import { t as createSha256Digest } from "./sha256-digest.js";
|
|
3
|
+
import { a as requiresToolApproval, r as createToolInputDigest$1, s as createInvocationAuthority } from "./tool-input-digest.js";
|
|
4
4
|
import { createHash } from "node:crypto";
|
|
5
5
|
import { readFile, realpath, stat } from "node:fs/promises";
|
|
6
6
|
import { isAbsolute, join, relative } from "node:path";
|
|
@@ -294,6 +294,9 @@ function createPiToolProxyDefinitions$1(options) {
|
|
|
294
294
|
};
|
|
295
295
|
});
|
|
296
296
|
}
|
|
297
|
+
function createToolInputDigest(input) {
|
|
298
|
+
return createToolInputDigest$1(input, createSha256Digest);
|
|
299
|
+
}
|
|
297
300
|
function createPiToolNameResolver$1(tools) {
|
|
298
301
|
const toolIdsByPiName = new Map(tools.map((tool) => [toPiToolName(tool.id), tool.id]));
|
|
299
302
|
return (toolName) => toolIdsByPiName.get(toolName) ?? toolName;
|
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import { o as createBackgroundSessionToolContracts } from "./background-session-authority.js";
|
|
2
|
+
//#region src/platform/values/deep-freeze.ts
|
|
3
|
+
function deepFreeze(value) {
|
|
4
|
+
if (value !== null && typeof value === "object" && !Object.isFrozen(value)) {
|
|
5
|
+
Object.freeze(value);
|
|
6
|
+
for (const child of Object.values(value)) deepFreeze(child);
|
|
7
|
+
}
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/core/application/agent-catalog/contracts/rivus-runtime-tool.ts
|
|
12
|
+
const RIVUS_RUNTIME_TOOL_IDS = Object.freeze([
|
|
13
|
+
"read",
|
|
14
|
+
"bash",
|
|
15
|
+
"edit",
|
|
16
|
+
"write",
|
|
17
|
+
"grep",
|
|
18
|
+
"find",
|
|
19
|
+
"ls"
|
|
20
|
+
]);
|
|
21
|
+
function isRivusRuntimeToolId(value) {
|
|
22
|
+
return RIVUS_RUNTIME_TOOL_IDS.includes(value);
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/adapters/agent/memory/rivus-memory-tool-contract.ts
|
|
26
|
+
const RIVUS_MEMORY_TOOL_ID = "memory";
|
|
27
|
+
const RIVUS_MEMORY_TOOL_PLUGIN_ID = "rivus-core";
|
|
28
|
+
const RIVUS_MEMORY_TOOL_VERSION = "1.0.0";
|
|
29
|
+
function createRivusMemoryToolContract(scopes) {
|
|
30
|
+
return Object.freeze({
|
|
31
|
+
description: "Search and read Memory inside Host-bound scopes; propose or request forgetting only in writable private scopes.",
|
|
32
|
+
digest: "sha256:rivus-memory-v3",
|
|
33
|
+
id: RIVUS_MEMORY_TOOL_ID,
|
|
34
|
+
idempotency: "required",
|
|
35
|
+
inputSchema: Object.freeze({
|
|
36
|
+
additionalProperties: false,
|
|
37
|
+
properties: {
|
|
38
|
+
command: {
|
|
39
|
+
description: "One of search, read, propose, or forget_request.",
|
|
40
|
+
enum: [
|
|
41
|
+
"search",
|
|
42
|
+
"read",
|
|
43
|
+
"propose",
|
|
44
|
+
"forget_request"
|
|
45
|
+
],
|
|
46
|
+
type: "string"
|
|
47
|
+
},
|
|
48
|
+
id: {
|
|
49
|
+
description: "Required for read and forget_request.",
|
|
50
|
+
minLength: 1,
|
|
51
|
+
type: "string"
|
|
52
|
+
},
|
|
53
|
+
input: {
|
|
54
|
+
additionalProperties: false,
|
|
55
|
+
description: "Required for propose.",
|
|
56
|
+
properties: { content: {
|
|
57
|
+
minLength: 1,
|
|
58
|
+
type: "string"
|
|
59
|
+
} },
|
|
60
|
+
required: ["content"],
|
|
61
|
+
type: "object"
|
|
62
|
+
},
|
|
63
|
+
query: {
|
|
64
|
+
additionalProperties: false,
|
|
65
|
+
description: "Required for search.",
|
|
66
|
+
properties: { query: { type: "string" } },
|
|
67
|
+
required: ["query"],
|
|
68
|
+
type: "object"
|
|
69
|
+
},
|
|
70
|
+
reason: {
|
|
71
|
+
description: "Optional reason for forget_request.",
|
|
72
|
+
type: "string"
|
|
73
|
+
},
|
|
74
|
+
...scopes.length === 0 ? {} : { scope: {
|
|
75
|
+
description: "Optional Host-granted scope for search or propose. Confirmed Project and Shared User Profile Memory are read-only to the model.",
|
|
76
|
+
enum: [...scopes],
|
|
77
|
+
type: "string"
|
|
78
|
+
} }
|
|
79
|
+
},
|
|
80
|
+
required: ["command"],
|
|
81
|
+
type: "object"
|
|
82
|
+
}),
|
|
83
|
+
risk: "mutate",
|
|
84
|
+
version: RIVUS_MEMORY_TOOL_VERSION
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/adapters/agent/catalog/rivus-host-tool-descriptor-provider.ts
|
|
89
|
+
function createRivusHostToolDescriptorProvider(options) {
|
|
90
|
+
return Object.freeze({ provide: ({ deployment, memoryScopes }) => {
|
|
91
|
+
const contributions = [];
|
|
92
|
+
if (deployment.memory?.tool === true) contributions.push({
|
|
93
|
+
scope: "profile",
|
|
94
|
+
tool: Object.freeze({
|
|
95
|
+
...createRivusMemoryToolContract(memoryScopes),
|
|
96
|
+
pluginId: RIVUS_MEMORY_TOOL_PLUGIN_ID
|
|
97
|
+
})
|
|
98
|
+
});
|
|
99
|
+
if (options.backgroundSessions) contributions.push(...createBackgroundSessionToolContracts().map(toRuntimeContribution));
|
|
100
|
+
return Object.freeze(contributions);
|
|
101
|
+
} });
|
|
102
|
+
}
|
|
103
|
+
function toRuntimeContribution(tool) {
|
|
104
|
+
return Object.freeze({
|
|
105
|
+
scope: "runtime",
|
|
106
|
+
tool: Object.freeze({ ...tool })
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region src/core/application/agent-catalog/contracts/rivus-plugin.ts
|
|
111
|
+
const RIVUS_PLUGIN_API_VERSION = "1";
|
|
112
|
+
var InvalidRivusPlugin = class extends Error {
|
|
113
|
+
name = "InvalidRivusPlugin";
|
|
114
|
+
};
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/core/application/agent-catalog/catalog/rivus-catalog-validation.ts
|
|
117
|
+
const MEMORY_SCOPES = [
|
|
118
|
+
"conversation",
|
|
119
|
+
"agent-private",
|
|
120
|
+
"project",
|
|
121
|
+
"shared-user-profile"
|
|
122
|
+
];
|
|
123
|
+
function validateRivusPluginManifest(manifest) {
|
|
124
|
+
validateRivusCatalogIdentifier(manifest.id, "plugin");
|
|
125
|
+
if (manifest.apiVersion !== "1") throw new InvalidRivusPlugin(`unsupported plugin API version ${manifest.apiVersion}; expected 1`);
|
|
126
|
+
if (manifest.version.trim() === "") throw new InvalidRivusPlugin("plugin version must not be empty");
|
|
127
|
+
}
|
|
128
|
+
function validateRivusCatalogRegistration(item, existing, pending) {
|
|
129
|
+
validateRivusCatalogIdentifier(item.id, item.kind);
|
|
130
|
+
if (item.namespaced && !item.id.startsWith(`${item.pluginId}/`)) throw new InvalidRivusPlugin(`${item.kind} id ${item.id} must use plugin namespace ${item.pluginId}/`);
|
|
131
|
+
const key = `${item.kind}:${item.id}`;
|
|
132
|
+
if (pending.has(key) || existing.has(item.id)) throw new InvalidRivusPlugin(`duplicate ${item.kind} id: ${item.id}`);
|
|
133
|
+
}
|
|
134
|
+
function validateRivusCatalogIdentifier(id, kind) {
|
|
135
|
+
if (!/^[a-z0-9][a-z0-9._/-]*$/.test(id) || id.includes("*") || id.includes("//")) throw new InvalidRivusPlugin(`invalid ${kind} id: ${id}`);
|
|
136
|
+
}
|
|
137
|
+
function uniqueRivusCatalogIds(ids, label) {
|
|
138
|
+
const result = /* @__PURE__ */ new Set();
|
|
139
|
+
for (const id of ids) {
|
|
140
|
+
if (id.includes("*")) throw new InvalidRivusPlugin(`${label} does not support wildcard id: ${id}`);
|
|
141
|
+
validateRivusCatalogIdentifier(id, label);
|
|
142
|
+
if (result.has(id)) throw new InvalidRivusPlugin(`duplicate id in ${label}: ${id}`);
|
|
143
|
+
result.add(id);
|
|
144
|
+
}
|
|
145
|
+
return [...result];
|
|
146
|
+
}
|
|
147
|
+
function validateRivusCatalogReferences(ids, available, owner, kind) {
|
|
148
|
+
for (const id of uniqueRivusCatalogIds(ids, `${owner} ${kind} references`)) validateRivusCatalogReference(id, available, owner, kind);
|
|
149
|
+
}
|
|
150
|
+
function validateRivusCatalogReference(id, available, owner, kind) {
|
|
151
|
+
if (!available.has(id)) throw new InvalidRivusPlugin(`${owner} references unknown ${kind}: ${id}`);
|
|
152
|
+
}
|
|
153
|
+
function validateRivusMemoryScopes(scopes, owner) {
|
|
154
|
+
const result = /* @__PURE__ */ new Set();
|
|
155
|
+
for (const scope of scopes) {
|
|
156
|
+
if (!MEMORY_SCOPES.includes(scope)) throw new InvalidRivusPlugin(`${owner} references unsupported Memory scope: ${String(scope)}`);
|
|
157
|
+
if (result.has(scope)) throw new InvalidRivusPlugin(`${owner} contains duplicate Memory scope: ${scope}`);
|
|
158
|
+
result.add(scope);
|
|
159
|
+
}
|
|
160
|
+
return [...result];
|
|
161
|
+
}
|
|
162
|
+
function uniqueRivusRuntimeToolIds(ids, owner) {
|
|
163
|
+
const result = /* @__PURE__ */ new Set();
|
|
164
|
+
for (const id of ids) {
|
|
165
|
+
if (typeof id === "string" && id.includes("*")) throw new InvalidRivusPlugin(`${owner} does not support wildcard Runtime Tool: ${id}`);
|
|
166
|
+
if (typeof id !== "string" || !isRivusRuntimeToolId(id)) throw new InvalidRivusPlugin(`${owner} references unknown Runtime Tool: ${String(id)}`);
|
|
167
|
+
if (result.has(id)) throw new InvalidRivusPlugin(`${owner} contains duplicate Runtime Tool: ${id}`);
|
|
168
|
+
result.add(id);
|
|
169
|
+
}
|
|
170
|
+
return [...result];
|
|
171
|
+
}
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/core/application/agent-catalog/catalog/rivus-plugin-catalog.ts
|
|
174
|
+
function createRivusPluginCatalog(runtime) {
|
|
175
|
+
const plugins = /* @__PURE__ */ new Map();
|
|
176
|
+
const profiles = /* @__PURE__ */ new Map();
|
|
177
|
+
const tools = /* @__PURE__ */ new Map();
|
|
178
|
+
const skills = /* @__PURE__ */ new Map();
|
|
179
|
+
const automations = /* @__PURE__ */ new Map();
|
|
180
|
+
return {
|
|
181
|
+
registerPlugin: (plugin) => {
|
|
182
|
+
validateRivusPluginManifest(plugin.manifest);
|
|
183
|
+
if (plugins.has(plugin.manifest.id)) throw new InvalidRivusPlugin(`duplicate plugin id: ${plugin.manifest.id}`);
|
|
184
|
+
const pendingProfiles = [];
|
|
185
|
+
const pendingTools = [];
|
|
186
|
+
const pendingSkills = [];
|
|
187
|
+
const pendingAutomations = [];
|
|
188
|
+
const pendingIds = /* @__PURE__ */ new Set();
|
|
189
|
+
const register = (kind, definition, catalog, destination, namespaced) => {
|
|
190
|
+
validateRivusCatalogRegistration({
|
|
191
|
+
id: definition.id,
|
|
192
|
+
kind,
|
|
193
|
+
namespaced,
|
|
194
|
+
pluginId: plugin.manifest.id
|
|
195
|
+
}, catalog, pendingIds);
|
|
196
|
+
const key = `${kind}:${definition.id}`;
|
|
197
|
+
pendingIds.add(key);
|
|
198
|
+
destination.push(runtime.deepFreeze({
|
|
199
|
+
...definition,
|
|
200
|
+
pluginId: plugin.manifest.id
|
|
201
|
+
}));
|
|
202
|
+
};
|
|
203
|
+
plugin.register({
|
|
204
|
+
registerAgentProfile: (profile) => register("profile", profile, profiles, pendingProfiles, false),
|
|
205
|
+
registerAutomation: (automation) => register("automation", automation, automations, pendingAutomations, true),
|
|
206
|
+
registerSkill: (skill) => register("skill", skill, skills, pendingSkills, true),
|
|
207
|
+
registerTool: (tool) => register("tool", tool, tools, pendingTools, true)
|
|
208
|
+
});
|
|
209
|
+
const availableToolIds = /* @__PURE__ */ new Set([...tools.keys(), ...pendingTools.map(({ id }) => id)]);
|
|
210
|
+
const availableSkillIds = /* @__PURE__ */ new Set([...skills.keys(), ...pendingSkills.map(({ id }) => id)]);
|
|
211
|
+
const availableProfileIds = /* @__PURE__ */ new Set([...profiles.keys(), ...pendingProfiles.map(({ id }) => id)]);
|
|
212
|
+
for (const profile of pendingProfiles) {
|
|
213
|
+
validateRivusMemoryScopes(profile.memory.scopes, `profile ${profile.id}`);
|
|
214
|
+
uniqueRivusRuntimeToolIds(profile.runtimeTools?.allow ?? [], `profile ${profile.id}`);
|
|
215
|
+
validateRivusCatalogReferences(profile.tools.allow, availableToolIds, `profile ${profile.id}`, "tool");
|
|
216
|
+
validateRivusCatalogReferences(profile.skills.allow, availableSkillIds, `profile ${profile.id}`, "skill");
|
|
217
|
+
}
|
|
218
|
+
for (const automation of pendingAutomations) {
|
|
219
|
+
validateRivusCatalogReference(automation.profileId, availableProfileIds, `automation ${automation.id}`, "profile");
|
|
220
|
+
validateRivusCatalogReferences(automation.requestedToolIds, availableToolIds, `automation ${automation.id}`, "tool");
|
|
221
|
+
validateRivusCatalogReferences(automation.requestedSkillIds, availableSkillIds, `automation ${automation.id}`, "skill");
|
|
222
|
+
}
|
|
223
|
+
plugins.set(plugin.manifest.id, runtime.deepFreeze({ ...plugin.manifest }));
|
|
224
|
+
for (const profile of pendingProfiles) profiles.set(profile.id, profile);
|
|
225
|
+
for (const tool of pendingTools) tools.set(tool.id, tool);
|
|
226
|
+
for (const skill of pendingSkills) skills.set(skill.id, skill);
|
|
227
|
+
for (const automation of pendingAutomations) automations.set(automation.id, automation);
|
|
228
|
+
},
|
|
229
|
+
snapshot: () => runtime.deepFreeze({
|
|
230
|
+
automations: [...automations.values()],
|
|
231
|
+
plugins: [...plugins.values()],
|
|
232
|
+
profiles: [...profiles.values()],
|
|
233
|
+
skills: [...skills.values()],
|
|
234
|
+
tools: [...tools.values()]
|
|
235
|
+
})
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
//#endregion
|
|
239
|
+
//#region src/core/application/agent-catalog/resolution/rivus-tool-grant-set.ts
|
|
240
|
+
function narrowRivusToolGrantSet(parent, restrictions, runtime) {
|
|
241
|
+
const toolIds = intersectToolIds([parent.toolIds, ...restrictions]);
|
|
242
|
+
return runtime.deepFreeze({
|
|
243
|
+
revision: runtime.digest(JSON.stringify({
|
|
244
|
+
parentRevision: parent.revision,
|
|
245
|
+
toolIds
|
|
246
|
+
})),
|
|
247
|
+
toolIds
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
function intersectRivusToolIds(sets, runtime) {
|
|
251
|
+
const toolIds = intersectToolIds(sets);
|
|
252
|
+
return runtime.deepFreeze({
|
|
253
|
+
revision: runtime.digest(JSON.stringify(toolIds)),
|
|
254
|
+
toolIds
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
function createRivusToolGrantSetOperations(runtime) {
|
|
258
|
+
return Object.freeze({
|
|
259
|
+
intersect: (sets) => intersectRivusToolIds(sets, runtime),
|
|
260
|
+
narrow: (parent, restrictions) => narrowRivusToolGrantSet(parent, restrictions, runtime)
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
function intersectToolIds(sets) {
|
|
264
|
+
const [first = [], ...rest] = sets;
|
|
265
|
+
return [...new Set(first)].filter((id) => rest.every((set) => set.includes(id))).sort();
|
|
266
|
+
}
|
|
267
|
+
//#endregion
|
|
268
|
+
//#region src/core/application/agent-catalog/resolution/rivus-agent-grant-restriction.ts
|
|
269
|
+
function restrictRivusAgentDefinitionGrants(definition, restriction, runtime) {
|
|
270
|
+
const toolIds = uniqueIds(restriction.toolIds);
|
|
271
|
+
const skillIds = uniqueIds(restriction.skillIds);
|
|
272
|
+
const runtimeToolIds = uniqueRuntimeToolIds(restriction.runtimeToolIds);
|
|
273
|
+
const memoryScopes = uniqueScopes(restriction.memory.scopes);
|
|
274
|
+
const toolsById = new Map(definition.tools.map((tool) => [tool.id, tool]));
|
|
275
|
+
const skillsById = new Map(definition.skills.map((skill) => [skill.id, skill]));
|
|
276
|
+
const tools = toolIds.map((toolId) => {
|
|
277
|
+
const tool = toolsById.get(toolId);
|
|
278
|
+
if (!tool) throw new Error(`Agent grant restriction requests ungranted Tool: ${toolId}`);
|
|
279
|
+
return tool;
|
|
280
|
+
});
|
|
281
|
+
const skills = skillIds.map((skillId) => {
|
|
282
|
+
const skill = skillsById.get(skillId);
|
|
283
|
+
if (!skill) throw new Error(`Agent grant restriction requests ungranted Skill: ${skillId}`);
|
|
284
|
+
return skill;
|
|
285
|
+
});
|
|
286
|
+
for (const scope of memoryScopes) if (!definition.memory.scopes.includes(scope)) throw new Error(`Agent grant restriction requests ungranted Memory scope: ${scope}`);
|
|
287
|
+
for (const runtimeToolId of runtimeToolIds) if (!definition.runtimeToolGrantSet.toolIds.includes(runtimeToolId)) throw new Error(`Agent grant restriction requests ungranted Runtime Tool: ${runtimeToolId}`);
|
|
288
|
+
if (restriction.memory.tool && !definition.memory.tool) throw new Error("Agent grant restriction cannot enable an ungranted Memory Tool");
|
|
289
|
+
const skillGrantSet = runtime.deepFreeze({
|
|
290
|
+
revision: runtime.digest(JSON.stringify({
|
|
291
|
+
parentRevision: definition.skillGrantSet.revision,
|
|
292
|
+
skillIds
|
|
293
|
+
})),
|
|
294
|
+
skillIds
|
|
295
|
+
});
|
|
296
|
+
const runtimeToolGrantSet = runtime.deepFreeze({
|
|
297
|
+
revision: runtime.digest(JSON.stringify({
|
|
298
|
+
parentRevision: definition.runtimeToolGrantSet.revision,
|
|
299
|
+
toolIds: runtimeToolIds
|
|
300
|
+
})),
|
|
301
|
+
toolIds: runtimeToolIds
|
|
302
|
+
});
|
|
303
|
+
return runtime.deepFreeze({
|
|
304
|
+
...definition,
|
|
305
|
+
memory: {
|
|
306
|
+
scopes: memoryScopes,
|
|
307
|
+
tool: restriction.memory.tool
|
|
308
|
+
},
|
|
309
|
+
runtimeToolGrantSet,
|
|
310
|
+
skillGrantSet,
|
|
311
|
+
skills,
|
|
312
|
+
toolGrantSet: narrowRivusToolGrantSet(definition.toolGrantSet, [toolIds], runtime),
|
|
313
|
+
tools
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
function uniqueRuntimeToolIds(ids) {
|
|
317
|
+
const requested = /* @__PURE__ */ new Set();
|
|
318
|
+
for (const id of ids) {
|
|
319
|
+
if (!isRivusRuntimeToolId(id)) throw new Error(`Agent grant restriction requests unknown Runtime Tool: ${id}`);
|
|
320
|
+
requested.add(id);
|
|
321
|
+
}
|
|
322
|
+
return RIVUS_RUNTIME_TOOL_IDS.filter((id) => requested.has(id));
|
|
323
|
+
}
|
|
324
|
+
function uniqueIds(ids) {
|
|
325
|
+
return [...new Set(ids)].sort();
|
|
326
|
+
}
|
|
327
|
+
function uniqueScopes(scopes) {
|
|
328
|
+
return [...new Set(scopes)].sort();
|
|
329
|
+
}
|
|
330
|
+
//#endregion
|
|
331
|
+
//#region src/core/application/agent-catalog/resolution/rivus-agent-definition-resolver.ts
|
|
332
|
+
function createRivusAgentDefinitionResolver(providers, runtime) {
|
|
333
|
+
return Object.freeze({ resolve: (catalog, deployment) => resolveRivusAgentDefinition(catalog, deployment, providers, runtime) });
|
|
334
|
+
}
|
|
335
|
+
function createRivusAgentCatalog(providers, runtime) {
|
|
336
|
+
const resolver = createRivusAgentDefinitionResolver(providers, runtime);
|
|
337
|
+
return Object.freeze({
|
|
338
|
+
createPluginCatalog: () => createRivusPluginCatalog(runtime),
|
|
339
|
+
resolve: (catalog, deployment) => resolver.resolve(catalog, deployment),
|
|
340
|
+
restrictGrants: (definition, restriction) => restrictRivusAgentDefinitionGrants(definition, restriction, runtime)
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
function resolveRivusAgentDefinition(catalog, deployment, providers, runtime) {
|
|
344
|
+
const snapshot = catalog.snapshot();
|
|
345
|
+
const plugin = snapshot.plugins.find((candidate) => candidate.id === deployment.pluginId);
|
|
346
|
+
if (!plugin) throw new InvalidRivusPlugin(`unknown deployment plugin: ${deployment.pluginId}`);
|
|
347
|
+
const profile = snapshot.profiles.find((candidate) => candidate.id === deployment.profileId && candidate.pluginId === deployment.pluginId);
|
|
348
|
+
if (!profile) throw new InvalidRivusPlugin(`unknown profile ${deployment.profileId} for plugin ${deployment.pluginId}`);
|
|
349
|
+
const memoryScopes = resolveMemoryScopes(profile, deployment);
|
|
350
|
+
const runtimeToolIds = resolveRuntimeTools(profile, deployment);
|
|
351
|
+
const memoryTool = deployment.memory?.tool === true;
|
|
352
|
+
if (memoryTool && memoryScopes.length === 0) throw new InvalidRivusPlugin(`deployment ${deployment.agentId} Memory Tool requires at least one granted scope`);
|
|
353
|
+
const contributions = providers.flatMap((provider) => provider.provide({
|
|
354
|
+
deployment,
|
|
355
|
+
memoryScopes,
|
|
356
|
+
profile
|
|
357
|
+
}));
|
|
358
|
+
const profileContributions = contributions.filter(({ scope }) => scope === "profile");
|
|
359
|
+
const runtimeContributions = contributions.filter(({ scope }) => scope === "runtime");
|
|
360
|
+
validateToolContributions(snapshot.tools, contributions);
|
|
361
|
+
if (memoryTool && !profileContributions.some(({ tool }) => tool.id === "memory")) throw new InvalidRivusPlugin(`deployment ${deployment.agentId} Memory Tool descriptor is unavailable`);
|
|
362
|
+
const pluginTools = resolvePluginTools(snapshot.tools, profile, deployment);
|
|
363
|
+
const profileToolsById = new Map(profileContributions.map(({ tool }) => [tool.id, runtime.deepFreeze({ ...tool })]));
|
|
364
|
+
const resolvedProfileToolIds = [...pluginTools.keys(), ...profileToolsById.keys()].sort();
|
|
365
|
+
const tools = resolvedProfileToolIds.map((id) => pluginTools.get(id) ?? profileToolsById.get(id));
|
|
366
|
+
const skills = resolveSkills(snapshot.skills, profile, deployment);
|
|
367
|
+
const skillIds = skills.map(({ id }) => id);
|
|
368
|
+
const profileRevision = digest(runtime, {
|
|
369
|
+
memory: {
|
|
370
|
+
scopes: memoryScopes,
|
|
371
|
+
tool: memoryTool
|
|
372
|
+
},
|
|
373
|
+
model: profile.model,
|
|
374
|
+
plugin,
|
|
375
|
+
profileId: profile.id,
|
|
376
|
+
runtimeTools: runtimeToolIds,
|
|
377
|
+
skills: skills.map(({ content, digest: skillDigest, id, pluginId, title, version }) => ({
|
|
378
|
+
content,
|
|
379
|
+
digest: skillDigest,
|
|
380
|
+
id,
|
|
381
|
+
pluginId,
|
|
382
|
+
title,
|
|
383
|
+
version
|
|
384
|
+
})),
|
|
385
|
+
systemPrompt: profile.systemPrompt,
|
|
386
|
+
tools
|
|
387
|
+
});
|
|
388
|
+
const toolGrantSet = runtime.deepFreeze({
|
|
389
|
+
revision: digest(runtime, {
|
|
390
|
+
profileRevision,
|
|
391
|
+
toolIds: resolvedProfileToolIds
|
|
392
|
+
}),
|
|
393
|
+
toolIds: resolvedProfileToolIds
|
|
394
|
+
});
|
|
395
|
+
const skillGrantSet = runtime.deepFreeze({
|
|
396
|
+
revision: digest(runtime, {
|
|
397
|
+
profileRevision,
|
|
398
|
+
skillIds
|
|
399
|
+
}),
|
|
400
|
+
skillIds
|
|
401
|
+
});
|
|
402
|
+
const runtimeToolGrantSet = runtime.deepFreeze({
|
|
403
|
+
revision: digest(runtime, {
|
|
404
|
+
profileRevision,
|
|
405
|
+
toolIds: runtimeToolIds
|
|
406
|
+
}),
|
|
407
|
+
toolIds: runtimeToolIds
|
|
408
|
+
});
|
|
409
|
+
return extendRuntimeTools(runtime.deepFreeze({
|
|
410
|
+
agentId: deployment.agentId,
|
|
411
|
+
endpointIds: [...deployment.endpointIds],
|
|
412
|
+
memory: {
|
|
413
|
+
scopes: memoryScopes,
|
|
414
|
+
tool: memoryTool
|
|
415
|
+
},
|
|
416
|
+
model: profile.model,
|
|
417
|
+
pluginId: deployment.pluginId,
|
|
418
|
+
profileId: deployment.profileId,
|
|
419
|
+
...deployment.projectSpaceId ? { projectSpaceId: deployment.projectSpaceId } : {},
|
|
420
|
+
profileRevision,
|
|
421
|
+
runtimeToolGrantSet,
|
|
422
|
+
skillGrantSet,
|
|
423
|
+
skills,
|
|
424
|
+
systemPrompt: profile.systemPrompt,
|
|
425
|
+
toolGrantSet,
|
|
426
|
+
tools
|
|
427
|
+
}), runtimeContributions, runtime);
|
|
428
|
+
}
|
|
429
|
+
function resolveRuntimeTools(profile, deployment) {
|
|
430
|
+
const profileToolIds = uniqueRivusRuntimeToolIds(profile.runtimeTools?.allow ?? [], `profile ${profile.id}`);
|
|
431
|
+
const requestedToolIds = uniqueRivusRuntimeToolIds(deployment.runtimeTools?.allow ?? [], `deployment ${deployment.agentId}`);
|
|
432
|
+
const profileToolIdSet = new Set(profileToolIds);
|
|
433
|
+
const requestedToolIdSet = new Set(requestedToolIds);
|
|
434
|
+
return RIVUS_RUNTIME_TOOL_IDS.filter((id) => profileToolIdSet.has(id) && requestedToolIdSet.has(id));
|
|
435
|
+
}
|
|
436
|
+
function resolveMemoryScopes(profile, deployment) {
|
|
437
|
+
const profileScopes = validateRivusMemoryScopes(profile.memory.scopes, `profile ${profile.id}`);
|
|
438
|
+
const requestedScopes = validateRivusMemoryScopes(deployment.memory?.scopes ?? [], `deployment ${deployment.agentId}`);
|
|
439
|
+
return profileScopes.filter((scope) => requestedScopes.includes(scope));
|
|
440
|
+
}
|
|
441
|
+
function resolvePluginTools(registeredTools, profile, deployment) {
|
|
442
|
+
const requestedTools = uniqueRivusCatalogIds(deployment.tools.allow, "deployment tool allowlist");
|
|
443
|
+
const catalogTools = new Map(registeredTools.map((tool) => [tool.id, tool]));
|
|
444
|
+
for (const id of requestedTools) if (!catalogTools.has(id)) throw new InvalidRivusPlugin(`unknown deployment tool: ${id}`);
|
|
445
|
+
const profileToolIds = uniqueRivusCatalogIds(profile.tools.allow, "profile tool allowlist");
|
|
446
|
+
for (const id of profileToolIds) if (!catalogTools.has(id)) throw new InvalidRivusPlugin(`profile ${profile.id} references unknown tool: ${id}`);
|
|
447
|
+
const grantedIds = profileToolIds.filter((id) => requestedTools.includes(id)).sort();
|
|
448
|
+
return new Map(grantedIds.map((id) => [id, toResolvedTool(catalogTools.get(id))]));
|
|
449
|
+
}
|
|
450
|
+
function resolveSkills(registeredSkills, profile, deployment) {
|
|
451
|
+
const requestedSkills = uniqueRivusCatalogIds(deployment.skills.allow, "deployment skill allowlist");
|
|
452
|
+
const catalogSkills = new Map(registeredSkills.map((skill) => [skill.id, skill]));
|
|
453
|
+
for (const id of requestedSkills) if (!catalogSkills.has(id)) throw new InvalidRivusPlugin(`unknown deployment skill: ${id}`);
|
|
454
|
+
return uniqueRivusCatalogIds(profile.skills.allow, "profile skill allowlist").filter((id) => requestedSkills.includes(id)).sort().map((id) => {
|
|
455
|
+
const skill = catalogSkills.get(id);
|
|
456
|
+
if (!skill) throw new InvalidRivusPlugin(`profile ${profile.id} references unknown skill: ${id}`);
|
|
457
|
+
return skill;
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
function validateToolContributions(registeredTools, contributions) {
|
|
461
|
+
const knownIds = new Set(registeredTools.map(({ id }) => id));
|
|
462
|
+
for (const { tool } of contributions) {
|
|
463
|
+
validateRivusCatalogIdentifier(tool.id, "contributed tool");
|
|
464
|
+
validateRivusCatalogIdentifier(tool.pluginId, "contributed tool plugin");
|
|
465
|
+
if (knownIds.has(tool.id)) throw new InvalidRivusPlugin(`duplicate contributed tool id: ${tool.id}`);
|
|
466
|
+
if (tool.version.trim() === "" || tool.digest.trim() === "") throw new InvalidRivusPlugin(`contributed tool ${tool.id} requires a version and digest`);
|
|
467
|
+
knownIds.add(tool.id);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
function extendRuntimeTools(definition, contributions, runtime) {
|
|
471
|
+
if (contributions.length === 0) return definition;
|
|
472
|
+
const additions = contributions.map(({ tool }) => runtime.deepFreeze({ ...tool }));
|
|
473
|
+
const additionIds = additions.map(({ id }) => id).sort();
|
|
474
|
+
return runtime.deepFreeze({
|
|
475
|
+
...definition,
|
|
476
|
+
toolGrantSet: {
|
|
477
|
+
revision: digest(runtime, {
|
|
478
|
+
parentRevision: definition.toolGrantSet.revision,
|
|
479
|
+
toolIds: additionIds
|
|
480
|
+
}),
|
|
481
|
+
toolIds: [...definition.toolGrantSet.toolIds, ...additionIds].sort()
|
|
482
|
+
},
|
|
483
|
+
tools: [...definition.tools, ...additions]
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
function toResolvedTool(tool) {
|
|
487
|
+
const { createExecutor: _createExecutor, description, digest: toolDigest, id, idempotency, inputSchema, pluginId, risk, version } = tool;
|
|
488
|
+
return {
|
|
489
|
+
description,
|
|
490
|
+
digest: toolDigest,
|
|
491
|
+
id,
|
|
492
|
+
idempotency,
|
|
493
|
+
inputSchema,
|
|
494
|
+
pluginId,
|
|
495
|
+
risk,
|
|
496
|
+
version
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
function digest(runtime, value) {
|
|
500
|
+
return runtime.digest(stableJson(value));
|
|
501
|
+
}
|
|
502
|
+
function stableJson(value) {
|
|
503
|
+
if (Array.isArray(value)) return `[${value.map(stableJson).join(",")}]`;
|
|
504
|
+
if (value !== null && typeof value === "object") return `{${Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, child]) => `${JSON.stringify(key)}:${stableJson(child)}`).join(",")}}`;
|
|
505
|
+
return JSON.stringify(value);
|
|
506
|
+
}
|
|
507
|
+
//#endregion
|
|
508
|
+
export { RIVUS_PLUGIN_API_VERSION as a, RIVUS_MEMORY_TOOL_PLUGIN_ID as c, RIVUS_RUNTIME_TOOL_IDS as d, isRivusRuntimeToolId as f, InvalidRivusPlugin as i, RIVUS_MEMORY_TOOL_VERSION as l, createRivusToolGrantSetOperations as n, createRivusHostToolDescriptorProvider as o, deepFreeze as p, createRivusPluginCatalog as r, RIVUS_MEMORY_TOOL_ID as s, createRivusAgentCatalog as t, createRivusMemoryToolContract as u };
|