@ai-matrx/agents 0.59.176 → 0.59.177
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 +8 -1
- package/dist/content-transfer/index.cjs +1 -1
- package/dist/content-transfer/index.js +1 -1
- package/dist/content-transfer/react/index.cjs +1 -1
- package/dist/content-transfer/react/index.js +1 -1
- package/dist/mandates/index.cjs +4 -4
- package/dist/mandates/index.d.cts +6 -6
- package/dist/mandates/index.d.ts +6 -6
- package/dist/mandates/index.js +4 -4
- package/dist/versions/index.cjs +231 -0
- package/dist/versions/index.d.cts +158 -0
- package/dist/versions/index.d.ts +158 -0
- package/dist/versions/index.js +208 -0
- package/dist/versions/react/index.cjs +1305 -0
- package/dist/versions/react/index.d.cts +137 -0
- package/dist/versions/react/index.d.ts +137 -0
- package/dist/versions/react/index.js +1302 -0
- package/package.json +23 -2
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { DiffOptions, DiffResult, DiffNode } from '@ai-matrx/diff/structural';
|
|
2
|
+
|
|
3
|
+
/** Internal/runtime fields that should never appear in diffs */
|
|
4
|
+
declare const AGENT_EXCLUDE_PATHS: Set<string>;
|
|
5
|
+
/** Identity keys for array matching — prevents false diffs when items shift position */
|
|
6
|
+
declare const AGENT_IDENTITY_KEYS: Record<string, string>;
|
|
7
|
+
declare const AGENT_DIFF_OPTIONS: DiffOptions;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Any agent record the diff can compare: the web's Redux `AgentDefinition`, a version snapshot
|
|
11
|
+
* mapped by `snapshotToComparable`, or a fixture. Only top-level keys matter to the comparison.
|
|
12
|
+
*/
|
|
13
|
+
type ComparableAgent = object;
|
|
14
|
+
interface AgentDefinitionComparison {
|
|
15
|
+
diffResult: DiffResult;
|
|
16
|
+
changedFields: DiffNode[];
|
|
17
|
+
behaviorFields: DiffNode[];
|
|
18
|
+
profileFields: DiffNode[];
|
|
19
|
+
localStateFields: DiffNode[];
|
|
20
|
+
behaviorMatches: boolean;
|
|
21
|
+
comparedConfigurationMatches: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Compare two live agents through the same exclusions and identity rules as
|
|
25
|
+
* the canonical AgentDiffViewer, while separating behavior from expected
|
|
26
|
+
* personal-copy profile differences.
|
|
27
|
+
*/
|
|
28
|
+
declare function compareAgentDefinitions(systemAgent: ComparableAgent, personalAgent: ComparableAgent): AgentDefinitionComparison;
|
|
29
|
+
|
|
30
|
+
declare function promoteFailureMessage(err: unknown): string;
|
|
31
|
+
|
|
32
|
+
/** One row of `agx_get_version_history`: a saved version of an agent (newest first). */
|
|
33
|
+
interface AgentVersionHistoryEntry {
|
|
34
|
+
version_id: string;
|
|
35
|
+
version_number: number;
|
|
36
|
+
name: string;
|
|
37
|
+
changed_at: string;
|
|
38
|
+
change_note: string | null;
|
|
39
|
+
contract_change: string | null;
|
|
40
|
+
contract_break_declared: string | null;
|
|
41
|
+
input_contract_hash: string | null;
|
|
42
|
+
output_contract_hash: string | null;
|
|
43
|
+
}
|
|
44
|
+
/** A version id resolved to its agent and number (`agent.definition_version`). */
|
|
45
|
+
interface AgentVersionLookup {
|
|
46
|
+
versionId: string;
|
|
47
|
+
agentId: string;
|
|
48
|
+
versionNumber: number;
|
|
49
|
+
agentName: string | null;
|
|
50
|
+
}
|
|
51
|
+
/** What `agx_promote_version` answers. */
|
|
52
|
+
interface PromoteVersionResult {
|
|
53
|
+
success: boolean;
|
|
54
|
+
error?: string;
|
|
55
|
+
promoted_version?: number;
|
|
56
|
+
agent_id?: string;
|
|
57
|
+
}
|
|
58
|
+
/** A contract break a person declares on a version (null clears the declaration). */
|
|
59
|
+
type ContractBreakKind = "input" | "output" | "both";
|
|
60
|
+
/** A history entry with what changed since the version before it, once both snapshots were read. */
|
|
61
|
+
interface EnrichedVersion extends AgentVersionHistoryEntry {
|
|
62
|
+
diffSummary?: DiffResult;
|
|
63
|
+
snapshotLoaded: boolean;
|
|
64
|
+
}
|
|
65
|
+
/** The load state the version timeline renders (the host owns where snapshots are kept). */
|
|
66
|
+
interface VersionHistoryLoad {
|
|
67
|
+
enrichedVersions: EnrichedVersion[];
|
|
68
|
+
loading: boolean;
|
|
69
|
+
progress: {
|
|
70
|
+
fetched: number;
|
|
71
|
+
total: number;
|
|
72
|
+
};
|
|
73
|
+
/** Versions whose snapshot read failed — never invisible. */
|
|
74
|
+
failedVersions: number[];
|
|
75
|
+
failureReason: string | null;
|
|
76
|
+
fetchEnrichedHistory: () => void | Promise<void>;
|
|
77
|
+
/** Read the named versions' snapshots (the timeline's Load on a gap). */
|
|
78
|
+
fetchGap: (versionNumbers: number[]) => void | Promise<void>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The versions a "Load Full History" reads: the 10 newest, the oldest, and about every 10th in
|
|
83
|
+
* between — enough to say what changed across a long history without reading every snapshot.
|
|
84
|
+
*/
|
|
85
|
+
declare function computeVersionFetchTargets(versions: readonly Pick<AgentVersionHistoryEntry, "version_number">[]): number[];
|
|
86
|
+
/**
|
|
87
|
+
* Each version (newest first) with `snapshotLoaded` and, when it and the version before it were
|
|
88
|
+
* both read, the diff between them. `snapshotOf` answers the host's stored snapshot or undefined.
|
|
89
|
+
*/
|
|
90
|
+
declare function enrichVersions<V extends AgentVersionHistoryEntry>(versions: readonly V[], snapshotOf: (versionNumber: number) => ComparableAgent | undefined): (V & EnrichedVersion)[];
|
|
91
|
+
/**
|
|
92
|
+
* A raw `agx_get_version_snapshot` row in the agent-definition key names the diff adapters read
|
|
93
|
+
* (`model_id` → `modelId`, `variable_definitions` → `variableDefinitions`, …). Top-level keys only;
|
|
94
|
+
* values pass through untouched, so both sides of a comparison made this way read alike. A host
|
|
95
|
+
* with its own parsed agent records (the web's Redux store) compares those instead.
|
|
96
|
+
*/
|
|
97
|
+
declare function snapshotToComparable(row: Record<string, unknown>): Record<string, unknown>;
|
|
98
|
+
|
|
99
|
+
/** The database's refusal, as supabase-js reports it. */
|
|
100
|
+
interface VersionsDbError {
|
|
101
|
+
message: string;
|
|
102
|
+
code?: string | null;
|
|
103
|
+
details?: string | null;
|
|
104
|
+
hint?: string | null;
|
|
105
|
+
}
|
|
106
|
+
type Answer = PromiseLike<{
|
|
107
|
+
data: unknown;
|
|
108
|
+
error: VersionsDbError | null;
|
|
109
|
+
}>;
|
|
110
|
+
/** The calls these reads make: a supabase-js client fits it as is. */
|
|
111
|
+
interface VersionsClient {
|
|
112
|
+
rpc(fn: string, args: Record<string, unknown>): Answer;
|
|
113
|
+
schema(name: "agent"): {
|
|
114
|
+
from(table: "definition_version"): {
|
|
115
|
+
select(columns: string): {
|
|
116
|
+
eq(column: string, value: string): {
|
|
117
|
+
maybeSingle(): Answer;
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/** A refused or failed read, carrying the database's own sentence. */
|
|
124
|
+
declare class AgentVersionsError extends Error {
|
|
125
|
+
readonly code: string | null;
|
|
126
|
+
readonly detail: string | null;
|
|
127
|
+
readonly hint: string | null;
|
|
128
|
+
constructor(error: VersionsDbError);
|
|
129
|
+
}
|
|
130
|
+
/** One page of an agent's saved versions, newest first. */
|
|
131
|
+
declare function listAgentVersions(client: VersionsClient, query: {
|
|
132
|
+
agentId: string;
|
|
133
|
+
limit?: number;
|
|
134
|
+
offset?: number;
|
|
135
|
+
}): Promise<AgentVersionHistoryEntry[]>;
|
|
136
|
+
/** The raw snapshot row of one version (null: the database has no such version for this agent). */
|
|
137
|
+
declare function readAgentVersionSnapshot(client: VersionsClient, query: {
|
|
138
|
+
agentId: string;
|
|
139
|
+
version: number;
|
|
140
|
+
}): Promise<Record<string, unknown> | null>;
|
|
141
|
+
/** An immutable version id → its agent and number. RLS on agent.definition_version is the boundary. */
|
|
142
|
+
declare function resolveAgentVersionId(client: VersionsClient, versionId: string): Promise<AgentVersionLookup | null>;
|
|
143
|
+
/**
|
|
144
|
+
* Restore: make a past version the agent's current definition (`agx_promote_version`). A refusal
|
|
145
|
+
* throws with the database's sentence — show it with `promoteFailureMessage`.
|
|
146
|
+
*/
|
|
147
|
+
declare function promoteAgentVersion(client: VersionsClient, query: {
|
|
148
|
+
agentId: string;
|
|
149
|
+
version: number;
|
|
150
|
+
}): Promise<PromoteVersionResult>;
|
|
151
|
+
/** Declare (or clear, with null) a contract break the hashes cannot see. */
|
|
152
|
+
declare function declareContractBreak(client: VersionsClient, query: {
|
|
153
|
+
agentId: string;
|
|
154
|
+
version: number;
|
|
155
|
+
kind: ContractBreakKind | null;
|
|
156
|
+
}): Promise<void>;
|
|
157
|
+
|
|
158
|
+
export { AGENT_DIFF_OPTIONS, AGENT_EXCLUDE_PATHS, AGENT_IDENTITY_KEYS, type AgentDefinitionComparison, type AgentVersionHistoryEntry, type AgentVersionLookup, AgentVersionsError, type ComparableAgent, type ContractBreakKind, type EnrichedVersion, type PromoteVersionResult, type VersionHistoryLoad, type VersionsClient, type VersionsDbError, compareAgentDefinitions, computeVersionFetchTargets, declareContractBreak, enrichVersions, listAgentVersions, promoteAgentVersion, promoteFailureMessage, readAgentVersionSnapshot, resolveAgentVersionId, snapshotToComparable };
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// versions/constants.ts
|
|
2
|
+
var AGENT_EXCLUDE_PATHS = /* @__PURE__ */ new Set([
|
|
3
|
+
"id",
|
|
4
|
+
"userId",
|
|
5
|
+
"organizationId",
|
|
6
|
+
"taskId",
|
|
7
|
+
"createdAt",
|
|
8
|
+
"updatedAt",
|
|
9
|
+
"isVersion",
|
|
10
|
+
"parentAgentId",
|
|
11
|
+
"sourceAgentId",
|
|
12
|
+
"sourceSnapshotAt",
|
|
13
|
+
// Lineage and record bookkeeping: who made the row and which version it was
|
|
14
|
+
// copied from never change what the agent does, so a copy never "differs" here.
|
|
15
|
+
"sourceVersion",
|
|
16
|
+
"followsSource",
|
|
17
|
+
"createdBy",
|
|
18
|
+
"runCounts",
|
|
19
|
+
"isOwner",
|
|
20
|
+
"accessLevel",
|
|
21
|
+
"sharedByEmail",
|
|
22
|
+
"agentType",
|
|
23
|
+
"version",
|
|
24
|
+
"changeNote",
|
|
25
|
+
"changedAt"
|
|
26
|
+
]);
|
|
27
|
+
var AGENT_IDENTITY_KEYS = {
|
|
28
|
+
variableDefinitions: "name",
|
|
29
|
+
contextPolicies: "key",
|
|
30
|
+
customTools: "name"
|
|
31
|
+
};
|
|
32
|
+
var AGENT_DIFF_OPTIONS = {
|
|
33
|
+
excludePaths: AGENT_EXCLUDE_PATHS,
|
|
34
|
+
identityKeys: AGENT_IDENTITY_KEYS,
|
|
35
|
+
// Output-schema property order is authored behavior: model providers often
|
|
36
|
+
// emit fields in schema order, so a reorder must be visible in version diffs.
|
|
37
|
+
orderSensitiveObjectPaths: /* @__PURE__ */ new Set(["outputSchema"]),
|
|
38
|
+
// Agent configuration is executable data. `_`/`__` keys (especially
|
|
39
|
+
// output-schema `__kind`) are contract fields, never hidden metadata.
|
|
40
|
+
skipUnderscorePrefix: false
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// versions/compare.ts
|
|
44
|
+
import { computeDiff } from "@ai-matrx/diff/structural";
|
|
45
|
+
var AGENT_PROFILE_FIELDS = /* @__PURE__ */ new Set(["name", "description", "category", "tags"]);
|
|
46
|
+
var AGENT_LOCAL_STATE_FIELDS = /* @__PURE__ */ new Set([
|
|
47
|
+
"isActive",
|
|
48
|
+
"isArchived",
|
|
49
|
+
"isFavorite"
|
|
50
|
+
]);
|
|
51
|
+
function toComparableAgentRecord(agent) {
|
|
52
|
+
return Object.fromEntries(
|
|
53
|
+
Object.entries(agent).filter(([key]) => !key.startsWith("_"))
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
function compareAgentDefinitions(systemAgent, personalAgent) {
|
|
57
|
+
const systemRecord = toComparableAgentRecord(systemAgent);
|
|
58
|
+
const personalRecord = toComparableAgentRecord(personalAgent);
|
|
59
|
+
const diffResult = computeDiff(
|
|
60
|
+
systemRecord,
|
|
61
|
+
personalRecord,
|
|
62
|
+
AGENT_DIFF_OPTIONS
|
|
63
|
+
);
|
|
64
|
+
const changedFields = diffResult.root.filter(
|
|
65
|
+
(node) => node.changeType !== "unchanged"
|
|
66
|
+
);
|
|
67
|
+
const profileFields = changedFields.filter(
|
|
68
|
+
(node) => AGENT_PROFILE_FIELDS.has(node.key)
|
|
69
|
+
);
|
|
70
|
+
const localStateFields = changedFields.filter(
|
|
71
|
+
(node) => AGENT_LOCAL_STATE_FIELDS.has(node.key)
|
|
72
|
+
);
|
|
73
|
+
const behaviorFields = changedFields.filter(
|
|
74
|
+
(node) => !AGENT_PROFILE_FIELDS.has(node.key) && !AGENT_LOCAL_STATE_FIELDS.has(node.key)
|
|
75
|
+
);
|
|
76
|
+
return {
|
|
77
|
+
diffResult,
|
|
78
|
+
changedFields,
|
|
79
|
+
behaviorFields,
|
|
80
|
+
profileFields,
|
|
81
|
+
localStateFields,
|
|
82
|
+
behaviorMatches: behaviorFields.length === 0,
|
|
83
|
+
comparedConfigurationMatches: !diffResult.hasChanges
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// versions/promote-failure-message.ts
|
|
88
|
+
var GENERIC = "Failed to promote version";
|
|
89
|
+
function promoteFailureMessage(err) {
|
|
90
|
+
const raw = err instanceof Error ? err.message : err && typeof err === "object" && "message" in err ? String(err.message ?? "") : typeof err === "string" ? err : "";
|
|
91
|
+
const reason = raw.trim();
|
|
92
|
+
return reason ? `${GENERIC}: ${reason}` : GENERIC;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// versions/enrich.ts
|
|
96
|
+
import { computeDiff as computeDiff2 } from "@ai-matrx/diff/structural";
|
|
97
|
+
function computeVersionFetchTargets(versions) {
|
|
98
|
+
if (versions.length === 0) return [];
|
|
99
|
+
const sorted = [...versions].sort((a, b) => b.version_number - a.version_number);
|
|
100
|
+
const targets = /* @__PURE__ */ new Set();
|
|
101
|
+
for (const v of sorted.slice(0, 10)) targets.add(v.version_number);
|
|
102
|
+
if (sorted.length <= 10) return [...targets];
|
|
103
|
+
targets.add(sorted[sorted.length - 1].version_number);
|
|
104
|
+
const remaining = sorted.slice(10, -1);
|
|
105
|
+
if (remaining.length > 0) {
|
|
106
|
+
const step = Math.max(1, Math.floor(remaining.length / Math.ceil(remaining.length / 10)));
|
|
107
|
+
for (let i = 0; i < remaining.length; i += step) targets.add(remaining[i].version_number);
|
|
108
|
+
}
|
|
109
|
+
return [...targets];
|
|
110
|
+
}
|
|
111
|
+
function enrichVersions(versions, snapshotOf) {
|
|
112
|
+
const sorted = [...versions].sort((a, b) => b.version_number - a.version_number);
|
|
113
|
+
return sorted.map((v, i) => {
|
|
114
|
+
const snapshot = snapshotOf(v.version_number);
|
|
115
|
+
const prev = sorted[i + 1];
|
|
116
|
+
const prevSnapshot = prev ? snapshotOf(prev.version_number) : void 0;
|
|
117
|
+
let diffSummary;
|
|
118
|
+
if (snapshot && prevSnapshot) {
|
|
119
|
+
diffSummary = computeDiff2(
|
|
120
|
+
prevSnapshot,
|
|
121
|
+
snapshot,
|
|
122
|
+
AGENT_DIFF_OPTIONS
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
return { ...v, snapshotLoaded: !!snapshot, ...diffSummary ? { diffSummary } : {} };
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
var camel = (key) => key.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
129
|
+
function snapshotToComparable(row) {
|
|
130
|
+
const out = {};
|
|
131
|
+
for (const [key, value] of Object.entries(row)) {
|
|
132
|
+
const name = key === "version_number" ? "version" : camel(key);
|
|
133
|
+
out[name] = value;
|
|
134
|
+
}
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// versions/data.ts
|
|
139
|
+
var AgentVersionsError = class extends Error {
|
|
140
|
+
code;
|
|
141
|
+
detail;
|
|
142
|
+
hint;
|
|
143
|
+
constructor(error) {
|
|
144
|
+
super(error.message || "The agent's versions could not be read and the database gave no reason.");
|
|
145
|
+
this.name = "AgentVersionsError";
|
|
146
|
+
this.code = error.code ?? null;
|
|
147
|
+
this.detail = error.details ?? null;
|
|
148
|
+
this.hint = error.hint ?? null;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
async function listAgentVersions(client, query) {
|
|
152
|
+
const { data, error } = await client.rpc("agx_get_version_history", {
|
|
153
|
+
p_agent_id: query.agentId,
|
|
154
|
+
p_limit: query.limit ?? 50,
|
|
155
|
+
p_offset: query.offset ?? 0
|
|
156
|
+
});
|
|
157
|
+
if (error) throw new AgentVersionsError(error);
|
|
158
|
+
return Array.isArray(data) ? data : [];
|
|
159
|
+
}
|
|
160
|
+
async function readAgentVersionSnapshot(client, query) {
|
|
161
|
+
const { data, error } = await client.rpc("agx_get_version_snapshot", {
|
|
162
|
+
p_agent_id: query.agentId,
|
|
163
|
+
p_version_number: query.version
|
|
164
|
+
});
|
|
165
|
+
if (error) throw new AgentVersionsError(error);
|
|
166
|
+
const raw = Array.isArray(data) ? data[0] : data;
|
|
167
|
+
return raw && typeof raw === "object" ? raw : null;
|
|
168
|
+
}
|
|
169
|
+
async function resolveAgentVersionId(client, versionId) {
|
|
170
|
+
const { data, error } = await client.schema("agent").from("definition_version").select("id, agent_id, version_number, name").eq("id", versionId).maybeSingle();
|
|
171
|
+
if (error) throw new AgentVersionsError(error);
|
|
172
|
+
if (!data) return null;
|
|
173
|
+
const row = data;
|
|
174
|
+
return { versionId: row.id, agentId: row.agent_id, versionNumber: row.version_number, agentName: row.name };
|
|
175
|
+
}
|
|
176
|
+
async function promoteAgentVersion(client, query) {
|
|
177
|
+
const { data, error } = await client.rpc("agx_promote_version", {
|
|
178
|
+
p_agent_id: query.agentId,
|
|
179
|
+
p_version_number: query.version
|
|
180
|
+
});
|
|
181
|
+
if (error) throw new AgentVersionsError(error);
|
|
182
|
+
const result = data ?? {};
|
|
183
|
+
return { ...result, success: result.success === true };
|
|
184
|
+
}
|
|
185
|
+
async function declareContractBreak(client, query) {
|
|
186
|
+
const { error } = await client.rpc("agx_declare_contract_break", {
|
|
187
|
+
p_agent_id: query.agentId,
|
|
188
|
+
p_version_number: query.version,
|
|
189
|
+
p_kind: query.kind ?? void 0
|
|
190
|
+
});
|
|
191
|
+
if (error) throw new AgentVersionsError(error);
|
|
192
|
+
}
|
|
193
|
+
export {
|
|
194
|
+
AGENT_DIFF_OPTIONS,
|
|
195
|
+
AGENT_EXCLUDE_PATHS,
|
|
196
|
+
AGENT_IDENTITY_KEYS,
|
|
197
|
+
AgentVersionsError,
|
|
198
|
+
compareAgentDefinitions,
|
|
199
|
+
computeVersionFetchTargets,
|
|
200
|
+
declareContractBreak,
|
|
201
|
+
enrichVersions,
|
|
202
|
+
listAgentVersions,
|
|
203
|
+
promoteAgentVersion,
|
|
204
|
+
promoteFailureMessage,
|
|
205
|
+
readAgentVersionSnapshot,
|
|
206
|
+
resolveAgentVersionId,
|
|
207
|
+
snapshotToComparable
|
|
208
|
+
};
|