@nixet/contracts 0.0.0-stage → 0.1.1
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/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/README.md +12 -2
- package/dist/cjs/domains/agents.d.ts +284 -0
- package/dist/cjs/domains/agents.js +244 -0
- package/dist/cjs/domains/api-keys.d.ts +265 -0
- package/dist/cjs/domains/api-keys.js +87 -0
- package/dist/cjs/domains/billing.d.ts +98 -0
- package/dist/cjs/domains/billing.js +122 -0
- package/dist/cjs/domains/connections.d.ts +101 -0
- package/dist/cjs/domains/connections.js +130 -0
- package/dist/cjs/domains/conversations.d.ts +113 -0
- package/dist/cjs/domains/conversations.js +85 -0
- package/dist/cjs/domains/documents.d.ts +48 -0
- package/dist/cjs/domains/documents.js +64 -0
- package/dist/cjs/domains/guardrails.d.ts +213 -0
- package/dist/cjs/domains/guardrails.js +206 -0
- package/dist/cjs/domains/identity.d.ts +55 -0
- package/dist/cjs/domains/identity.js +26 -0
- package/dist/cjs/domains/knowledge.d.ts +167 -0
- package/dist/cjs/domains/knowledge.js +174 -0
- package/dist/cjs/domains/mcp.d.ts +189 -0
- package/dist/cjs/domains/mcp.js +183 -0
- package/dist/cjs/domains/memory.d.ts +109 -0
- package/dist/cjs/domains/memory.js +92 -0
- package/dist/cjs/domains/redaction.d.ts +181 -0
- package/dist/cjs/domains/redaction.js +166 -0
- package/dist/cjs/domains/runs.d.ts +260 -0
- package/dist/cjs/domains/runs.js +260 -0
- package/dist/cjs/domains/secrets.d.ts +56 -0
- package/dist/cjs/domains/secrets.js +72 -0
- package/dist/cjs/domains/skills.d.ts +70 -0
- package/dist/cjs/domains/skills.js +82 -0
- package/dist/cjs/domains/tasks.d.ts +111 -0
- package/dist/cjs/domains/tasks.js +113 -0
- package/dist/cjs/domains/tools.d.ts +159 -0
- package/dist/cjs/domains/tools.js +203 -0
- package/dist/cjs/domains/users.d.ts +40 -0
- package/dist/cjs/domains/users.js +48 -0
- package/dist/cjs/index.d.ts +25 -0
- package/dist/cjs/index.js +41 -0
- package/dist/cjs/primitives.d.ts +75 -0
- package/dist/cjs/primitives.js +117 -0
- package/dist/esm/domains/agents.js +239 -0
- package/dist/esm/domains/api-keys.js +83 -0
- package/dist/esm/domains/billing.js +116 -0
- package/dist/esm/domains/connections.js +127 -0
- package/dist/esm/domains/conversations.js +82 -0
- package/dist/esm/domains/documents.js +61 -0
- package/dist/esm/domains/guardrails.js +202 -0
- package/dist/esm/domains/identity.js +23 -0
- package/dist/esm/domains/knowledge.js +170 -0
- package/dist/esm/domains/mcp.js +178 -0
- package/dist/esm/domains/memory.js +89 -0
- package/dist/esm/domains/redaction.js +162 -0
- package/dist/esm/domains/runs.js +256 -0
- package/dist/esm/domains/secrets.js +68 -0
- package/dist/esm/domains/skills.js +79 -0
- package/dist/esm/domains/tasks.js +109 -0
- package/dist/esm/domains/tools.js +196 -0
- package/dist/esm/domains/users.js +45 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/primitives.js +114 -0
- package/package.json +47 -4
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* MCP servers.
|
|
4
|
+
*
|
|
5
|
+
* A customer registers a server they run, and an agent version names it. The
|
|
6
|
+
* runtime connects, discovers its tools, and the agent can call them.
|
|
7
|
+
*
|
|
8
|
+
* Remote only. `stdio` would mean spawning a process inside our runtime from
|
|
9
|
+
* customer-supplied arguments, which is arbitrary code execution as us, so the
|
|
10
|
+
* transport is not in the enum rather than being rejected later.
|
|
11
|
+
*/
|
|
12
|
+
export const mcpTransportSchema = z.enum(["http", "sse"]);
|
|
13
|
+
/**
|
|
14
|
+
* How an end user's credential for a server was obtained.
|
|
15
|
+
*
|
|
16
|
+
* The scheme decides what connecting costs the user, and what expiry means:
|
|
17
|
+
*
|
|
18
|
+
* - `api_key` the value is supplied directly and does not expire.
|
|
19
|
+
* - `oauth2` the user is sent through the provider's consent screen. The
|
|
20
|
+
* access token expires; a refresh token, where the provider
|
|
21
|
+
* issues one, is stored so it can be replaced without asking
|
|
22
|
+
* the user again.
|
|
23
|
+
* - `session` the credential is a session identifier obtained by calling a
|
|
24
|
+
* tool on the server, and there is nothing to refresh it with.
|
|
25
|
+
* Expiry means sending the user through sign-in again, which is
|
|
26
|
+
* why it is not modelled as a short-lived `oauth2`.
|
|
27
|
+
*/
|
|
28
|
+
export const mcpConnectionSchemeSchema = z.enum(["api_key", "oauth2", "session"]);
|
|
29
|
+
/**
|
|
30
|
+
* How Nixet proves who it is to the server.
|
|
31
|
+
*
|
|
32
|
+
* `none` is for a server that is open or already restricted by network. The
|
|
33
|
+
* rest reference a stored secret by name rather than carrying a credential, so
|
|
34
|
+
* a configuration can be read, copied and versioned without the token.
|
|
35
|
+
*/
|
|
36
|
+
export const mcpAuthSchema = z.discriminatedUnion("type", [
|
|
37
|
+
z.object({ type: z.literal("none") }).strict(),
|
|
38
|
+
z
|
|
39
|
+
.object({
|
|
40
|
+
type: z.literal("bearer"),
|
|
41
|
+
/** Name of a stored secret. Sent as `Authorization: Bearer …`. */
|
|
42
|
+
secret: z.string().min(1).max(64),
|
|
43
|
+
})
|
|
44
|
+
.strict(),
|
|
45
|
+
z
|
|
46
|
+
.object({
|
|
47
|
+
type: z.literal("header"),
|
|
48
|
+
/** For servers wanting their own header, e.g. `X-API-Key`. */
|
|
49
|
+
header: z
|
|
50
|
+
.string()
|
|
51
|
+
.trim()
|
|
52
|
+
.min(1)
|
|
53
|
+
.max(64)
|
|
54
|
+
.regex(/^[A-Za-z0-9-]+$/, "A header name may contain letters, numbers and hyphens."),
|
|
55
|
+
secret: z.string().min(1).max(64),
|
|
56
|
+
})
|
|
57
|
+
.strict(),
|
|
58
|
+
z
|
|
59
|
+
.object({
|
|
60
|
+
type: z.literal("oauth"),
|
|
61
|
+
/**
|
|
62
|
+
* An OAuth access token the customer obtained and stored as a secret.
|
|
63
|
+
*
|
|
64
|
+
* Nixet does not run the authorization flow: that would mean holding a
|
|
65
|
+
* client secret and a refresh token per server, which is a larger piece
|
|
66
|
+
* of work than connecting to one. A token that expires stops working and
|
|
67
|
+
* the customer replaces it.
|
|
68
|
+
*/
|
|
69
|
+
secret: z.string().min(1).max(64),
|
|
70
|
+
})
|
|
71
|
+
.strict(),
|
|
72
|
+
z
|
|
73
|
+
.object({
|
|
74
|
+
type: z.literal("per_user"),
|
|
75
|
+
/**
|
|
76
|
+
* The credential belongs to the end user, not to the workspace.
|
|
77
|
+
*
|
|
78
|
+
* Every other variant names one stored secret, which makes a server
|
|
79
|
+
* registration reachable as exactly one identity. That is right for an
|
|
80
|
+
* integration the operator owns — their GitHub, their internal API — and
|
|
81
|
+
* wrong for a product where each end user links their own account. There
|
|
82
|
+
* the URL is identical for everyone and only the token differs, so a
|
|
83
|
+
* registration per user would force an agent version per user, and
|
|
84
|
+
* version history would stop describing behaviour and become a
|
|
85
|
+
* credential registry.
|
|
86
|
+
*
|
|
87
|
+
* So this variant names no secret. The server is registered once, and
|
|
88
|
+
* the credential is resolved per run from the user the run names.
|
|
89
|
+
*/
|
|
90
|
+
scheme: mcpConnectionSchemeSchema,
|
|
91
|
+
})
|
|
92
|
+
.strict(),
|
|
93
|
+
]);
|
|
94
|
+
/**
|
|
95
|
+
* Whether a server's credential comes from the end user rather than the
|
|
96
|
+
* workspace. A per-user server cannot be reached by a run that names no user.
|
|
97
|
+
*/
|
|
98
|
+
export function isPerUserAuth(auth) {
|
|
99
|
+
return auth.type === "per_user";
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* A URL Nixet will connect to.
|
|
103
|
+
*
|
|
104
|
+
* Only the shape is checked here. Whether the host is reachable, and whether it
|
|
105
|
+
* is somewhere we are willing to go, is decided at connect time against the
|
|
106
|
+
* resolved address — a name that passes here can still resolve to a private
|
|
107
|
+
* one, and the check that matters has to happen after resolution.
|
|
108
|
+
*/
|
|
109
|
+
export const mcpUrlSchema = z
|
|
110
|
+
.string()
|
|
111
|
+
.url()
|
|
112
|
+
.max(500)
|
|
113
|
+
.refine((value) => value.startsWith("https://") || value.startsWith("http://"), {
|
|
114
|
+
message: "Only http and https URLs are supported.",
|
|
115
|
+
});
|
|
116
|
+
/**
|
|
117
|
+
* Tools are their own resource.
|
|
118
|
+
*
|
|
119
|
+
* A server can expose a hundred, each carrying its own tags and its own policy,
|
|
120
|
+
* so they are rows rather than a field on this one. See `domains/tools`.
|
|
121
|
+
*/
|
|
122
|
+
export const mcpServerSchema = z.object({
|
|
123
|
+
id: z.string().describe("Unique identifier, prefixed `mcp_`."),
|
|
124
|
+
name: z
|
|
125
|
+
.string()
|
|
126
|
+
.describe("Stable handle a version pins, e.g. `github`. Lowercase and hyphenated."),
|
|
127
|
+
description: z.string().nullable(),
|
|
128
|
+
transport: mcpTransportSchema,
|
|
129
|
+
url: z.string(),
|
|
130
|
+
auth: mcpAuthSchema,
|
|
131
|
+
/**
|
|
132
|
+
* How many tools this server offered when it was last reached.
|
|
133
|
+
*
|
|
134
|
+
* The tools themselves are listed separately, because a server can have more
|
|
135
|
+
* of them than belongs in a response about the server.
|
|
136
|
+
*/
|
|
137
|
+
tool_count: z.number(),
|
|
138
|
+
tools_refreshed_at: z.string().nullable(),
|
|
139
|
+
/** Why the last connection failed, when it did. */
|
|
140
|
+
last_error: z.string().nullable(),
|
|
141
|
+
created_at: z.string(),
|
|
142
|
+
updated_at: z.string(),
|
|
143
|
+
});
|
|
144
|
+
export const mcpNameSchema = z
|
|
145
|
+
.string()
|
|
146
|
+
.trim()
|
|
147
|
+
.min(1)
|
|
148
|
+
.max(64)
|
|
149
|
+
.regex(/^[a-z0-9]+(-[a-z0-9]+)*$/, "Use lowercase letters, numbers and hyphens, e.g. `github`.");
|
|
150
|
+
export const createMcpServerSchema = z
|
|
151
|
+
.object({
|
|
152
|
+
name: mcpNameSchema,
|
|
153
|
+
url: mcpUrlSchema,
|
|
154
|
+
transport: mcpTransportSchema.default("http"),
|
|
155
|
+
description: z.string().trim().max(200).optional(),
|
|
156
|
+
auth: mcpAuthSchema.default({ type: "none" }),
|
|
157
|
+
})
|
|
158
|
+
.strict();
|
|
159
|
+
export const updateMcpServerSchema = z
|
|
160
|
+
.object({
|
|
161
|
+
url: mcpUrlSchema.optional(),
|
|
162
|
+
transport: mcpTransportSchema.optional(),
|
|
163
|
+
description: z.string().trim().max(200).optional(),
|
|
164
|
+
auth: mcpAuthSchema.optional(),
|
|
165
|
+
})
|
|
166
|
+
.strict();
|
|
167
|
+
/**
|
|
168
|
+
* The workspace secret a server's auth refers to, if any.
|
|
169
|
+
*
|
|
170
|
+
* Null for `none`, and null for `per_user`: that credential is the end user's
|
|
171
|
+
* and is not in the workspace secret store at all, so there is no name to
|
|
172
|
+
* return. Callers resolving a per-user server read `mcp.connections` instead.
|
|
173
|
+
*/
|
|
174
|
+
export function secretForAuth(auth) {
|
|
175
|
+
if (auth.type === "none" || auth.type === "per_user")
|
|
176
|
+
return null;
|
|
177
|
+
return auth.secret;
|
|
178
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* Memory.
|
|
4
|
+
*
|
|
5
|
+
* What an agent retains about a person or a thread, as opposed to the
|
|
6
|
+
* transcript, which is the literal record of what was said.
|
|
7
|
+
*/
|
|
8
|
+
export const MAX_MEMORY_LENGTH = 4_000;
|
|
9
|
+
/**
|
|
10
|
+
* Who a memory belongs to.
|
|
11
|
+
*
|
|
12
|
+
* `conversation` is this thread only, `user` follows a person across every
|
|
13
|
+
* thread they have with an agent, and `agent` is shared by all of its users.
|
|
14
|
+
*/
|
|
15
|
+
export const memoryScopeSchema = z.enum(["conversation", "user", "agent"]);
|
|
16
|
+
export const memorySchema = z.object({
|
|
17
|
+
id: z.string(),
|
|
18
|
+
scope: memoryScopeSchema,
|
|
19
|
+
content: z.string().describe("The fact, as the agent would recall it."),
|
|
20
|
+
metadata: z.record(z.string(), z.unknown()),
|
|
21
|
+
/**
|
|
22
|
+
* Set when a later fact replaced this one. The store adds rather than
|
|
23
|
+
* updates, so both survive and the older is marked instead of removed.
|
|
24
|
+
*/
|
|
25
|
+
superseded: z.boolean(),
|
|
26
|
+
superseded_by: z.string().nullable(),
|
|
27
|
+
/** Set when a memory should stop being recalled after a date. */
|
|
28
|
+
expires_at: z.string().nullable(),
|
|
29
|
+
created_at: z.string().nullable(),
|
|
30
|
+
updated_at: z.string().nullable(),
|
|
31
|
+
});
|
|
32
|
+
/** A search hit, with how closely it matched. */
|
|
33
|
+
export const memoryMatchSchema = memorySchema.extend({
|
|
34
|
+
score: z.number().nullable().describe("Similarity, 0 to 1. Higher is closer."),
|
|
35
|
+
});
|
|
36
|
+
/** Where a request may address memory. Every read and write names one. */
|
|
37
|
+
const target = {
|
|
38
|
+
agent_id: z.string().min(1),
|
|
39
|
+
user_id: z.string().min(1).optional(),
|
|
40
|
+
conversation_id: z.string().min(1).optional(),
|
|
41
|
+
};
|
|
42
|
+
function scopedToItsSubject(schema) {
|
|
43
|
+
return schema
|
|
44
|
+
.refine((value) => value.scope !== "user" || Boolean(value.user_id), {
|
|
45
|
+
message: "user_id is required for user-scoped memory.",
|
|
46
|
+
path: ["user_id"],
|
|
47
|
+
})
|
|
48
|
+
.refine((value) => value.scope !== "conversation" || Boolean(value.conversation_id), {
|
|
49
|
+
message: "conversation_id is required for conversation-scoped memory.",
|
|
50
|
+
path: ["conversation_id"],
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
export const createMemorySchema = scopedToItsSubject(z
|
|
54
|
+
.object({
|
|
55
|
+
...target,
|
|
56
|
+
scope: memoryScopeSchema,
|
|
57
|
+
content: z.string().trim().min(1).max(MAX_MEMORY_LENGTH),
|
|
58
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
59
|
+
expires_at: z.string().datetime().optional(),
|
|
60
|
+
})
|
|
61
|
+
.strict());
|
|
62
|
+
export const searchMemorySchema = scopedToItsSubject(z
|
|
63
|
+
.object({
|
|
64
|
+
...target,
|
|
65
|
+
query: z.string().trim().min(1).max(1_000),
|
|
66
|
+
scope: memoryScopeSchema.optional(),
|
|
67
|
+
limit: z.number().int().min(1).max(50).default(10),
|
|
68
|
+
})
|
|
69
|
+
.strict());
|
|
70
|
+
export const listMemorySchema = scopedToItsSubject(z
|
|
71
|
+
.object({
|
|
72
|
+
...target,
|
|
73
|
+
scope: memoryScopeSchema.optional(),
|
|
74
|
+
limit: z.number().int().min(1).max(100).default(50),
|
|
75
|
+
})
|
|
76
|
+
.strict());
|
|
77
|
+
/** Addressing one memory. The scope is what proves it is ours to touch. */
|
|
78
|
+
export const memoryTargetSchema = z.object(target).strict();
|
|
79
|
+
export const updateMemorySchema = z
|
|
80
|
+
.object({
|
|
81
|
+
...target,
|
|
82
|
+
content: z.string().trim().min(1).max(MAX_MEMORY_LENGTH).optional(),
|
|
83
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
84
|
+
expires_at: z.string().datetime().nullable().optional(),
|
|
85
|
+
})
|
|
86
|
+
.strict()
|
|
87
|
+
.refine((value) => value.content !== undefined ||
|
|
88
|
+
value.metadata !== undefined ||
|
|
89
|
+
value.expires_at !== undefined, { message: "Nothing to update." });
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* Redaction.
|
|
4
|
+
*
|
|
5
|
+
* Personal data is masked in Nixet before a run is dispatched, so the runtime
|
|
6
|
+
* and the model provider receive `[GIVEN_NAME_1]` where a name was. When the
|
|
7
|
+
* policy is reversible the map is sealed in the vault and the reply is restored
|
|
8
|
+
* on the way back out, so the caller still reads a normal answer.
|
|
9
|
+
*
|
|
10
|
+
* Scope is deliberately narrow: run input and output only. Tool arguments and
|
|
11
|
+
* results recorded in the trace are **not** redacted, because the trace is what
|
|
12
|
+
* the product is for. Anything describing this feature has to say so.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Detection categories, as the model reports them.
|
|
16
|
+
*
|
|
17
|
+
* `ORG` is a company rather than a person, so it is excluded unless a caller
|
|
18
|
+
* asks for it. `IMEI` and the other structured labels are checksum-validated.
|
|
19
|
+
*/
|
|
20
|
+
export const redactionLabelSchema = z.enum([
|
|
21
|
+
"GIVEN_NAME",
|
|
22
|
+
"SURNAME",
|
|
23
|
+
"STREET_NAME",
|
|
24
|
+
"BUILDING_NUMBER",
|
|
25
|
+
"SECONDARY_ADDRESS",
|
|
26
|
+
"CITY",
|
|
27
|
+
"STATE",
|
|
28
|
+
"ZIP_CODE",
|
|
29
|
+
"EMAIL",
|
|
30
|
+
"PHONE",
|
|
31
|
+
"CREDIT_CARD",
|
|
32
|
+
"BANK_ACCOUNT",
|
|
33
|
+
"ROUTING_NUMBER",
|
|
34
|
+
"IP_ADDRESS",
|
|
35
|
+
"URL",
|
|
36
|
+
"GOVERNMENT_ID",
|
|
37
|
+
"PASSPORT",
|
|
38
|
+
"DRIVERS_LICENSE",
|
|
39
|
+
"TAX_ID",
|
|
40
|
+
"SSN",
|
|
41
|
+
"IMEI",
|
|
42
|
+
"ORG",
|
|
43
|
+
]);
|
|
44
|
+
/**
|
|
45
|
+
* How much to mask.
|
|
46
|
+
*
|
|
47
|
+
* A mode is all a caller needs; `labels` exists for the rare case where one
|
|
48
|
+
* specific category matters and the rest do not.
|
|
49
|
+
*
|
|
50
|
+
* | mode | masks |
|
|
51
|
+
* | ------------ | -------------------------------------------------- |
|
|
52
|
+
* | `off` | nothing |
|
|
53
|
+
* | `structured` | only checksum-validated identifiers |
|
|
54
|
+
* | `standard` | every personal category |
|
|
55
|
+
* | `strict` | every category, including the company someone works for |
|
|
56
|
+
*
|
|
57
|
+
* `structured` is the only mode whose detections are all checksum-validated —
|
|
58
|
+
* cards against Luhn, IBANs against ISO-13616, national IDs against their own
|
|
59
|
+
* country's rule. Nothing in it depends on the model guessing, and the agent
|
|
60
|
+
* still sees names, so it still works normally.
|
|
61
|
+
*
|
|
62
|
+
* `standard` is the one to reach for: it covers everything that identifies a
|
|
63
|
+
* person. `strict` adds `ORG`, which is a company rather than a person, so it
|
|
64
|
+
* is separate rather than included by default.
|
|
65
|
+
*/
|
|
66
|
+
export const redactionModeSchema = z.enum(["off", "structured", "standard", "strict"]);
|
|
67
|
+
/** Deterministic labels: every one of these has to pass a real checksum. */
|
|
68
|
+
export const STRUCTURED_LABELS = [
|
|
69
|
+
"CREDIT_CARD",
|
|
70
|
+
"BANK_ACCOUNT",
|
|
71
|
+
"ROUTING_NUMBER",
|
|
72
|
+
"GOVERNMENT_ID",
|
|
73
|
+
"PASSPORT",
|
|
74
|
+
"DRIVERS_LICENSE",
|
|
75
|
+
"TAX_ID",
|
|
76
|
+
"SSN",
|
|
77
|
+
"IMEI",
|
|
78
|
+
];
|
|
79
|
+
/** Everything a person is, excluding the company they work for. */
|
|
80
|
+
export const STANDARD_LABELS = redactionLabelSchema.options.filter((label) => label !== "ORG");
|
|
81
|
+
export const STRICT_LABELS = redactionLabelSchema.options;
|
|
82
|
+
/** The labels a mode masks. `labels` on the policy overrides this. */
|
|
83
|
+
export function labelsForMode(mode) {
|
|
84
|
+
if (mode === "structured")
|
|
85
|
+
return STRUCTURED_LABELS;
|
|
86
|
+
if (mode === "standard")
|
|
87
|
+
return STANDARD_LABELS;
|
|
88
|
+
if (mode === "strict")
|
|
89
|
+
return STRICT_LABELS;
|
|
90
|
+
return [];
|
|
91
|
+
}
|
|
92
|
+
/** How long a reversible map is kept before it is destroyed. */
|
|
93
|
+
export const retentionSchema = z.enum(["1h", "24h", "7d", "30d"]);
|
|
94
|
+
export const RETENTION_MS = {
|
|
95
|
+
"1h": 60 * 60 * 1000,
|
|
96
|
+
"24h": 24 * 60 * 60 * 1000,
|
|
97
|
+
"7d": 7 * 24 * 60 * 60 * 1000,
|
|
98
|
+
"30d": 30 * 24 * 60 * 60 * 1000,
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* A version's redaction policy.
|
|
102
|
+
*
|
|
103
|
+
* Lives on the version rather than the agent, so it is immutable, hashed into
|
|
104
|
+
* the digest and pinned per run. Changing it is a new version, exactly like
|
|
105
|
+
* changing the instructions.
|
|
106
|
+
*/
|
|
107
|
+
export const redactionPolicySchema = z
|
|
108
|
+
.object({
|
|
109
|
+
mode: redactionModeSchema.default("off"),
|
|
110
|
+
/**
|
|
111
|
+
* Keep the placeholder map so the reply can be restored.
|
|
112
|
+
*
|
|
113
|
+
* Off by default. A reversible run stores the real values, encrypted, for
|
|
114
|
+
* as long as `retain_for`; an irreversible one keeps nothing, so there is
|
|
115
|
+
* nothing to leak and nothing to erase.
|
|
116
|
+
*/
|
|
117
|
+
reversible: z.boolean().default(false),
|
|
118
|
+
/**
|
|
119
|
+
* Neural confidence floor, `0..1`.
|
|
120
|
+
*
|
|
121
|
+
* Only affects model-inferred labels such as names and cities. The
|
|
122
|
+
* structured labels are checksum-validated and always apply. Raise this to
|
|
123
|
+
* over-mask less; lowering it will not surface a detection the model did
|
|
124
|
+
* not make.
|
|
125
|
+
*/
|
|
126
|
+
minimum_confidence: z.number().min(0).max(1).default(0.6),
|
|
127
|
+
/**
|
|
128
|
+
* Narrows the mode to these categories.
|
|
129
|
+
*
|
|
130
|
+
* Rarely needed: a mode already covers a sensible set. This is for the case
|
|
131
|
+
* where a caller wants cards masked and nothing else.
|
|
132
|
+
*/
|
|
133
|
+
labels: z.array(redactionLabelSchema).min(1).max(22).optional(),
|
|
134
|
+
retain_for: retentionSchema.default("24h"),
|
|
135
|
+
})
|
|
136
|
+
.strict()
|
|
137
|
+
.refine((policy) => policy.mode !== "off" || !policy.reversible, {
|
|
138
|
+
message: "reversible requires a redaction mode other than off.",
|
|
139
|
+
path: ["reversible"],
|
|
140
|
+
});
|
|
141
|
+
export const REDACTION_OFF = {
|
|
142
|
+
mode: "off",
|
|
143
|
+
reversible: false,
|
|
144
|
+
minimum_confidence: 0.6,
|
|
145
|
+
retain_for: "24h",
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* What a run reports about its own redaction.
|
|
149
|
+
*
|
|
150
|
+
* Counts per label, never the values: the masked values are the personal data
|
|
151
|
+
* that was just removed, and storing them beside the masked copy would produce
|
|
152
|
+
* a labelled index of every sensitive item in the run.
|
|
153
|
+
*/
|
|
154
|
+
export const redactionSummarySchema = z.object({
|
|
155
|
+
mode: redactionModeSchema,
|
|
156
|
+
reversible: z.boolean(),
|
|
157
|
+
/** Detections per label, e.g. `{ "EMAIL": 2, "GIVEN_NAME": 3 }`. */
|
|
158
|
+
counts: z.record(redactionLabelSchema, z.number().int().positive()),
|
|
159
|
+
item_count: z.number().int().nonnegative(),
|
|
160
|
+
/** When the stored map is destroyed. Null when the run kept none. */
|
|
161
|
+
expires_at: z.string().nullable(),
|
|
162
|
+
});
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { redactionSummarySchema } from "./redaction.js";
|
|
3
|
+
import { MAX_VARIABLE_VALUE, providerSchema } from "./agents.js";
|
|
4
|
+
/**
|
|
5
|
+
* Runs and their event stream.
|
|
6
|
+
*
|
|
7
|
+
* A run is the durable record of one execution: which version produced it, what
|
|
8
|
+
* it returned, what it cost. Events are the live view of the same execution.
|
|
9
|
+
*/
|
|
10
|
+
export const runStatusSchema = z.enum([
|
|
11
|
+
"queued",
|
|
12
|
+
"running",
|
|
13
|
+
"completed",
|
|
14
|
+
"failed",
|
|
15
|
+
"cancelled",
|
|
16
|
+
]);
|
|
17
|
+
export const usageSchema = z.object({
|
|
18
|
+
input_tokens: z.number().int().nonnegative(),
|
|
19
|
+
output_tokens: z.number().int().nonnegative(),
|
|
20
|
+
});
|
|
21
|
+
export const runSchema = z.object({
|
|
22
|
+
id: z.string().describe("Unique identifier, prefixed `run_`."),
|
|
23
|
+
agent_id: z.string().describe("Agent that produced this run."),
|
|
24
|
+
agent_version: z
|
|
25
|
+
.string()
|
|
26
|
+
.describe("Version pinned at admission. It cannot change mid-run."),
|
|
27
|
+
variables: z
|
|
28
|
+
.record(z.string(), z.string())
|
|
29
|
+
.nullable()
|
|
30
|
+
.describe("Placeholder values this run was given. Null when the version declared none."),
|
|
31
|
+
version_digest: z
|
|
32
|
+
.string()
|
|
33
|
+
.describe("Content hash of the configuration that produced this run."),
|
|
34
|
+
status: runStatusSchema.describe("Lifecycle state."),
|
|
35
|
+
provider: providerSchema.describe("Provider that served the run. The model behind `nix` is not disclosed."),
|
|
36
|
+
input: z
|
|
37
|
+
.string()
|
|
38
|
+
.describe("Message sent to the agent. Masked when the version redacts, so this is what was stored rather than what the caller sent."),
|
|
39
|
+
output: z
|
|
40
|
+
.string()
|
|
41
|
+
.nullable()
|
|
42
|
+
.describe("Final text, present once the run reaches a terminal state. Masked when the version redacts."),
|
|
43
|
+
redaction: redactionSummarySchema
|
|
44
|
+
.nullable()
|
|
45
|
+
.describe("What was masked, as counts. Null when the version does not redact."),
|
|
46
|
+
error: z
|
|
47
|
+
.string()
|
|
48
|
+
.nullable()
|
|
49
|
+
.describe("Safe failure reason. Never a stack trace or provider payload."),
|
|
50
|
+
usage: usageSchema.nullable().describe("Token counts, present on completion."),
|
|
51
|
+
latency_ms: z
|
|
52
|
+
.number()
|
|
53
|
+
.int()
|
|
54
|
+
.nonnegative()
|
|
55
|
+
.nullable()
|
|
56
|
+
.describe("Wall-clock duration of the run."),
|
|
57
|
+
trace_id: z.string().nullable().describe("Correlates this run with its trace."),
|
|
58
|
+
cost_usd: z
|
|
59
|
+
.number()
|
|
60
|
+
.nullable()
|
|
61
|
+
.describe("Total cost in USD, summed from the run's model calls."),
|
|
62
|
+
session_id: z.string().nullable().describe("Conversation this run belongs to."),
|
|
63
|
+
created_at: z.string().describe("RFC 3339 admission timestamp."),
|
|
64
|
+
completed_at: z.string().nullable().describe("RFC 3339 completion timestamp."),
|
|
65
|
+
});
|
|
66
|
+
export const MAX_INPUT_LENGTH = 100_000;
|
|
67
|
+
export const createRunSchema = z
|
|
68
|
+
.object({
|
|
69
|
+
agent_id: z.string().min(1).describe("Agent to run. Taken from the path."),
|
|
70
|
+
input: z.string().min(1).max(MAX_INPUT_LENGTH).describe("Message sent to the agent."),
|
|
71
|
+
agent_version: z
|
|
72
|
+
.string()
|
|
73
|
+
.optional()
|
|
74
|
+
.describe("Version to pin. Defaults to the agent's published version."),
|
|
75
|
+
provider: providerSchema
|
|
76
|
+
.optional()
|
|
77
|
+
.describe("Override the version's provider for this run only."),
|
|
78
|
+
session_id: z
|
|
79
|
+
.string()
|
|
80
|
+
.max(128)
|
|
81
|
+
.optional()
|
|
82
|
+
.describe("Groups related runs into one conversation."),
|
|
83
|
+
stream: z.boolean().default(false).describe("Set by the streaming endpoint."),
|
|
84
|
+
conversation_id: z
|
|
85
|
+
.string()
|
|
86
|
+
.max(64)
|
|
87
|
+
.nullish()
|
|
88
|
+
.describe("Set when a conversation dispatched this run."),
|
|
89
|
+
user_id: z
|
|
90
|
+
.string()
|
|
91
|
+
.max(64)
|
|
92
|
+
.nullish()
|
|
93
|
+
.describe("Who the agent is serving."),
|
|
94
|
+
/**
|
|
95
|
+
* Narrow this run to some of the version's MCP servers.
|
|
96
|
+
*
|
|
97
|
+
* A subset, never an addition. The version's list is the boundary of what
|
|
98
|
+
* the agent may reach, and a run that could name a server outside it would
|
|
99
|
+
* be a run that could grant itself a tool the agent was never published
|
|
100
|
+
* with — an unannotated tool defaults to `allow`, so the escalation would
|
|
101
|
+
* be silent.
|
|
102
|
+
*
|
|
103
|
+
* Omitted means every server the version named, which is the common case:
|
|
104
|
+
* a caller sends a user and lets their connections decide what is
|
|
105
|
+
* reachable.
|
|
106
|
+
*/
|
|
107
|
+
mcp_servers: z
|
|
108
|
+
.array(z.string().min(1).max(64))
|
|
109
|
+
.max(10)
|
|
110
|
+
.optional()
|
|
111
|
+
.describe("Subset of the version's MCP servers to use for this run."),
|
|
112
|
+
/**
|
|
113
|
+
* Values for the placeholders the version declared.
|
|
114
|
+
*
|
|
115
|
+
* Every declared variable must be supplied and nothing else may be: an
|
|
116
|
+
* unknown key is refused rather than ignored, so a caller cannot reach a
|
|
117
|
+
* slot the version never named, and a missing one is refused rather than
|
|
118
|
+
* left blank, because a prompt with a hole in it is worse than a run that
|
|
119
|
+
* did not start.
|
|
120
|
+
*
|
|
121
|
+
* The resolved values are recorded on the run. Without that, the
|
|
122
|
+
* instructions a run actually used are unrecoverable, and a version digest
|
|
123
|
+
* would no longer explain what the agent was told.
|
|
124
|
+
*/
|
|
125
|
+
variables: z
|
|
126
|
+
.record(z.string(), z.string().max(MAX_VARIABLE_VALUE))
|
|
127
|
+
.optional()
|
|
128
|
+
.describe("Values for the version's declared instruction placeholders."),
|
|
129
|
+
/**
|
|
130
|
+
* Prior turns, oldest first.
|
|
131
|
+
*
|
|
132
|
+
* Supplied by the conversation rather than stored on the run: the runtime
|
|
133
|
+
* is stateless, so history has to travel with the request that needs it.
|
|
134
|
+
*/
|
|
135
|
+
history: z
|
|
136
|
+
.array(z.object({
|
|
137
|
+
role: z.string().max(16),
|
|
138
|
+
content: z.string(),
|
|
139
|
+
}))
|
|
140
|
+
.optional(),
|
|
141
|
+
/** What this run may recall and retain. Supplied by a conversation. */
|
|
142
|
+
memory: z
|
|
143
|
+
.object({
|
|
144
|
+
conversation: z.boolean(),
|
|
145
|
+
user: z.boolean(),
|
|
146
|
+
agent: z.boolean(),
|
|
147
|
+
})
|
|
148
|
+
.optional(),
|
|
149
|
+
})
|
|
150
|
+
.strict();
|
|
151
|
+
/**
|
|
152
|
+
* What a trace event describes.
|
|
153
|
+
*
|
|
154
|
+
* Coarser than the event type, so a timeline can be grouped or filtered without
|
|
155
|
+
* knowing every individual type. Adding a capability adds types within an
|
|
156
|
+
* existing category far more often than it adds a category.
|
|
157
|
+
*/
|
|
158
|
+
export const eventCategorySchema = z.enum([
|
|
159
|
+
"run",
|
|
160
|
+
"model",
|
|
161
|
+
"tool",
|
|
162
|
+
"output",
|
|
163
|
+
"memory",
|
|
164
|
+
"knowledge",
|
|
165
|
+
"reasoning",
|
|
166
|
+
"approval",
|
|
167
|
+
"subagent",
|
|
168
|
+
"workflow",
|
|
169
|
+
"task",
|
|
170
|
+
"session",
|
|
171
|
+
"context",
|
|
172
|
+
"skill",
|
|
173
|
+
]);
|
|
174
|
+
/**
|
|
175
|
+
* Public event types.
|
|
176
|
+
*
|
|
177
|
+
* A stable surface mapped from the framework's much larger internal vocabulary,
|
|
178
|
+
* so the internals can change without breaking customers. Unmapped internal
|
|
179
|
+
* events are dropped rather than forwarded.
|
|
180
|
+
*/
|
|
181
|
+
export const runEventTypeSchema = z.enum([
|
|
182
|
+
"run.started",
|
|
183
|
+
"run.completed",
|
|
184
|
+
"run.failed",
|
|
185
|
+
"run.cancelled",
|
|
186
|
+
"run.paused",
|
|
187
|
+
"run.continued",
|
|
188
|
+
"run.output",
|
|
189
|
+
"run.model.started",
|
|
190
|
+
"run.model.completed",
|
|
191
|
+
"run.tool.started",
|
|
192
|
+
"run.tool.completed",
|
|
193
|
+
"run.tool.failed",
|
|
194
|
+
"run.knowledge.started",
|
|
195
|
+
"run.knowledge.completed",
|
|
196
|
+
"run.memory.recall.started",
|
|
197
|
+
"run.memory.recall.completed",
|
|
198
|
+
"run.memory.update.started",
|
|
199
|
+
"run.memory.update.completed",
|
|
200
|
+
"run.reasoning.started",
|
|
201
|
+
"run.reasoning.completed",
|
|
202
|
+
"run.subagent.spawned",
|
|
203
|
+
"run.subagent.completed",
|
|
204
|
+
"run.subagent.failed",
|
|
205
|
+
"run.subagent.killed",
|
|
206
|
+
"run.workflow.step.started",
|
|
207
|
+
"run.workflow.step.completed",
|
|
208
|
+
"run.workflow.step.failed",
|
|
209
|
+
"run.workflow.step.skipped",
|
|
210
|
+
"run.task.created",
|
|
211
|
+
"run.task.status",
|
|
212
|
+
"run.session.summarised",
|
|
213
|
+
"run.context.compressed",
|
|
214
|
+
/** Emitted by Nixet: the runtime cannot name a skill once it is merged. */
|
|
215
|
+
"run.skill.loaded",
|
|
216
|
+
]);
|
|
217
|
+
/** Where a tool came from: `agent`, or `skill:<name>` if a skill supplied it. */
|
|
218
|
+
export const TOOL_SOURCE_AGENT = "agent";
|
|
219
|
+
export const skillToolSource = (name) => `skill:${name}`;
|
|
220
|
+
/** Token counts for one span, where the provider reports them. */
|
|
221
|
+
export const eventTokensSchema = z.object({
|
|
222
|
+
input: z.number().int().nonnegative().optional(),
|
|
223
|
+
output: z.number().int().nonnegative().optional(),
|
|
224
|
+
cache_read: z.number().int().nonnegative().optional(),
|
|
225
|
+
reasoning: z.number().int().nonnegative().optional(),
|
|
226
|
+
});
|
|
227
|
+
export const runEventSchema = z.object({
|
|
228
|
+
sequence: z
|
|
229
|
+
.number()
|
|
230
|
+
.int()
|
|
231
|
+
.nonnegative()
|
|
232
|
+
.describe("Monotonic within a run, so a dropped stream can resume."),
|
|
233
|
+
type: runEventTypeSchema.describe("Event type."),
|
|
234
|
+
category: eventCategorySchema.describe("What this event describes."),
|
|
235
|
+
run_id: z.string().describe("Run this event belongs to."),
|
|
236
|
+
data: z
|
|
237
|
+
.string()
|
|
238
|
+
.nullable()
|
|
239
|
+
.describe("Short label: a text delta, a tool name, or a failure reason."),
|
|
240
|
+
/**
|
|
241
|
+
* Fields specific to this event type. Shapes differ by category, so this is
|
|
242
|
+
* an object rather than columns: a knowledge event reports documents found,
|
|
243
|
+
* a tool event reports arguments and a result.
|
|
244
|
+
*/
|
|
245
|
+
payload: z.record(z.string(), z.unknown()).nullable(),
|
|
246
|
+
duration_ms: z.number().int().nonnegative().nullable(),
|
|
247
|
+
tokens: eventTokensSchema.nullable(),
|
|
248
|
+
/** Computed from tokens; null when the model has no published rate. */
|
|
249
|
+
cost_usd: z.number().nullable(),
|
|
250
|
+
/**
|
|
251
|
+
* The event that produced this one. A sub-agent's events point at the event
|
|
252
|
+
* that spawned it, so a trace is a tree rather than a flat list.
|
|
253
|
+
*/
|
|
254
|
+
parent_event_id: z.string().nullable(),
|
|
255
|
+
created_at: z.string().describe("RFC 3339 timestamp."),
|
|
256
|
+
});
|