@peers-app/peers-sdk 0.16.5 → 0.17.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/dist/contracts/__tests__/builder.test.d.ts +1 -0
- package/dist/contracts/__tests__/builder.test.js +426 -0
- package/dist/contracts/__tests__/extract.test.d.ts +1 -0
- package/dist/contracts/__tests__/extract.test.js +145 -0
- package/dist/contracts/__tests__/integration.test.d.ts +1 -0
- package/dist/contracts/__tests__/integration.test.js +348 -0
- package/dist/contracts/__tests__/registry.test.d.ts +1 -0
- package/dist/contracts/__tests__/registry.test.js +324 -0
- package/dist/contracts/__tests__/validate.test.d.ts +1 -0
- package/dist/contracts/__tests__/validate.test.js +699 -0
- package/dist/contracts/builder.d.ts +102 -0
- package/dist/contracts/builder.js +216 -0
- package/dist/contracts/contract-providers.table.d.ts +40 -0
- package/dist/contracts/contract-providers.table.js +41 -0
- package/dist/contracts/contracts.table.d.ts +44 -0
- package/dist/contracts/contracts.table.js +44 -0
- package/dist/contracts/extract.d.ts +46 -0
- package/dist/contracts/extract.js +51 -0
- package/dist/contracts/index.d.ts +9 -0
- package/dist/contracts/index.js +31 -0
- package/dist/contracts/persistent-registry.d.ts +32 -0
- package/dist/contracts/persistent-registry.js +138 -0
- package/dist/contracts/registry.d.ts +58 -0
- package/dist/contracts/registry.js +155 -0
- package/dist/contracts/types.d.ts +108 -0
- package/dist/contracts/types.js +10 -0
- package/dist/contracts/validate.d.ts +24 -0
- package/dist/contracts/validate.js +274 -0
- package/dist/data/assistants.d.ts +5 -0
- package/dist/data/assistants.js +1 -0
- package/dist/data/package-versions.d.ts +2 -2
- package/dist/data/persistent-vars.d.ts +3 -0
- package/dist/data/persistent-vars.js +3 -0
- package/dist/data/tools.d.ts +5 -2
- package/dist/data/tools.js +1 -1
- package/dist/data/workflows.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/package-loader/contract-package-loader.d.ts +23 -0
- package/dist/package-loader/contract-package-loader.js +65 -0
- package/dist/package-loader/index.d.ts +1 -0
- package/dist/package-loader/index.js +1 -0
- package/dist/package-loader/package-loader.d.ts +11 -0
- package/dist/package-loader/package-loader.js +59 -8
- package/dist/rpc-types.d.ts +7 -0
- package/dist/rpc-types.js +4 -0
- package/dist/types/workflow.d.ts +3 -0
- package/dist/types/workflow.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { ITableDefinition } from "../data/orm/table-definitions.type";
|
|
2
|
+
import type { IToolInstance } from "../data/tools";
|
|
3
|
+
import type { IExtractableTableDef, IExtractableToolInstance } from "./extract";
|
|
4
|
+
import type { IAlsoImplementsDeclaration, IContractDefinition, IContractObservable, IPackageDefinitionResult } from "./types";
|
|
5
|
+
/**
|
|
6
|
+
* Scoped builder for a single contract within a package. Returned by
|
|
7
|
+
* `PackageBuilder.contract()`.
|
|
8
|
+
*/
|
|
9
|
+
export declare class ContractBuilder {
|
|
10
|
+
readonly contractId: string;
|
|
11
|
+
readonly version: number;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly description?: string | undefined;
|
|
14
|
+
readonly devTag?: "dev" | undefined;
|
|
15
|
+
private _tables;
|
|
16
|
+
private _tools;
|
|
17
|
+
private _observables;
|
|
18
|
+
private _alsoImplements;
|
|
19
|
+
/** Full runtime tool instances for this contract (used by the install flow). */
|
|
20
|
+
private _toolInstances;
|
|
21
|
+
/** Full table definitions for this contract (used by the install flow). */
|
|
22
|
+
private _tableDefinitions;
|
|
23
|
+
constructor(contractId: string, version: number, name: string, description?: string | undefined, devTag?: "dev" | undefined);
|
|
24
|
+
set tables(defs: IExtractableTableDef[]);
|
|
25
|
+
get tables(): IExtractableTableDef[];
|
|
26
|
+
set tools(instances: IExtractableToolInstance[]);
|
|
27
|
+
get tools(): IExtractableToolInstance[];
|
|
28
|
+
set observables(obs: IContractObservable[]);
|
|
29
|
+
get observables(): IContractObservable[];
|
|
30
|
+
/** Full tool instances with executable toolFn for runtime registration. */
|
|
31
|
+
set toolInstances(instances: IToolInstance[]);
|
|
32
|
+
get toolInstances(): IToolInstance[];
|
|
33
|
+
/** Full table definitions for runtime table registration. */
|
|
34
|
+
set tableDefinitions(defs: ITableDefinition[]);
|
|
35
|
+
get tableDefinitions(): ITableDefinition[];
|
|
36
|
+
/**
|
|
37
|
+
* Declare that this contract's implementation also satisfies another
|
|
38
|
+
* contract at the given version or inclusive version range.
|
|
39
|
+
*/
|
|
40
|
+
alsoImplements(targetContractId: string, versionOrRange: number | {
|
|
41
|
+
from: number;
|
|
42
|
+
to: number;
|
|
43
|
+
}): void;
|
|
44
|
+
/** @internal Build the contract definition by extracting shapes. */
|
|
45
|
+
_build(): {
|
|
46
|
+
definition: IContractDefinition;
|
|
47
|
+
alsoImplements: IAlsoImplementsDeclaration[];
|
|
48
|
+
toolInstances: IToolInstance[];
|
|
49
|
+
tableDefinitions: ITableDefinition[];
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Top-level builder passed to the `definePackage` callback.
|
|
54
|
+
*
|
|
55
|
+
* Packages may define **zero** contracts (e.g. UI-only apps with no persisted
|
|
56
|
+
* tables). Tables, tool shapes, and executable `toolInstances` /
|
|
57
|
+
* `tableDefinitions` are only attached via {@link PackageBuilder.contract} —
|
|
58
|
+
* there is no package-level API for them, so persistence always implies a
|
|
59
|
+
* contract.
|
|
60
|
+
*/
|
|
61
|
+
export declare class PackageBuilder {
|
|
62
|
+
private _packageId?;
|
|
63
|
+
private _version?;
|
|
64
|
+
private _versionTag?;
|
|
65
|
+
private _contracts;
|
|
66
|
+
private _consumes;
|
|
67
|
+
/** Package-level assistants (not part of any contract). */
|
|
68
|
+
assistants: unknown[];
|
|
69
|
+
/** Package-level app navigation items (not part of any contract). */
|
|
70
|
+
appNavs: unknown[];
|
|
71
|
+
/** The peer ID of this package (25-char alphanumeric from `newid()`). */
|
|
72
|
+
set packageId(id: string);
|
|
73
|
+
get packageId(): string | undefined;
|
|
74
|
+
/** Semantic version string (e.g. "1.0.0"). Typically imported from package.json at build time. */
|
|
75
|
+
set version(v: string);
|
|
76
|
+
get version(): string | undefined;
|
|
77
|
+
/** Version channel tag (e.g. "dev", "beta", "stable"). */
|
|
78
|
+
set versionTag(tag: string);
|
|
79
|
+
get versionTag(): string | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* Define (and implement) a contract at the given version.
|
|
82
|
+
* Returns a `ContractBuilder` for assigning tables, tools, and observables.
|
|
83
|
+
*
|
|
84
|
+
* @param contractId - Must be a valid peer ID (25-char alphanumeric from `newid()`).
|
|
85
|
+
* @param version - Positive integer version number.
|
|
86
|
+
* @param name - Human-readable contract name. This is the meaningful identifier that creators can change over time.
|
|
87
|
+
* @param devTag - Set to `"dev"` while the contract shape is still mutable.
|
|
88
|
+
*/
|
|
89
|
+
contract(contractId: string, version: number, name: string, devTag?: "dev"): ContractBuilder;
|
|
90
|
+
/**
|
|
91
|
+
* Declare a dependency on another package's contract.
|
|
92
|
+
*/
|
|
93
|
+
consumes(contractId: string, version: number): void;
|
|
94
|
+
/** @internal Finalize and return the package definition result. */
|
|
95
|
+
_build(): IPackageDefinitionResult;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Entry point for defining a package. The callback receives a
|
|
99
|
+
* `PackageBuilder` which is used to declare contracts, consumptions, and
|
|
100
|
+
* package-level items. Returns the fully-built package definition.
|
|
101
|
+
*/
|
|
102
|
+
export declare function definePackage(fn: (pkg: PackageBuilder) => void): IPackageDefinitionResult;
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PackageBuilder = exports.ContractBuilder = void 0;
|
|
4
|
+
exports.definePackage = definePackage;
|
|
5
|
+
const utils_1 = require("../utils");
|
|
6
|
+
const extract_1 = require("./extract");
|
|
7
|
+
const types_1 = require("./types");
|
|
8
|
+
/**
|
|
9
|
+
* Scoped builder for a single contract within a package. Returned by
|
|
10
|
+
* `PackageBuilder.contract()`.
|
|
11
|
+
*/
|
|
12
|
+
class ContractBuilder {
|
|
13
|
+
contractId;
|
|
14
|
+
version;
|
|
15
|
+
name;
|
|
16
|
+
description;
|
|
17
|
+
devTag;
|
|
18
|
+
_tables = [];
|
|
19
|
+
_tools = [];
|
|
20
|
+
_observables = [];
|
|
21
|
+
_alsoImplements = [];
|
|
22
|
+
/** Full runtime tool instances for this contract (used by the install flow). */
|
|
23
|
+
_toolInstances = [];
|
|
24
|
+
/** Full table definitions for this contract (used by the install flow). */
|
|
25
|
+
_tableDefinitions = [];
|
|
26
|
+
constructor(contractId, version, name, description, devTag) {
|
|
27
|
+
this.contractId = contractId;
|
|
28
|
+
this.version = version;
|
|
29
|
+
this.name = name;
|
|
30
|
+
this.description = description;
|
|
31
|
+
this.devTag = devTag;
|
|
32
|
+
}
|
|
33
|
+
set tables(defs) {
|
|
34
|
+
this._tables = defs;
|
|
35
|
+
}
|
|
36
|
+
get tables() {
|
|
37
|
+
return this._tables;
|
|
38
|
+
}
|
|
39
|
+
set tools(instances) {
|
|
40
|
+
this._tools = instances;
|
|
41
|
+
}
|
|
42
|
+
get tools() {
|
|
43
|
+
return this._tools;
|
|
44
|
+
}
|
|
45
|
+
set observables(obs) {
|
|
46
|
+
this._observables = obs;
|
|
47
|
+
}
|
|
48
|
+
get observables() {
|
|
49
|
+
return this._observables;
|
|
50
|
+
}
|
|
51
|
+
/** Full tool instances with executable toolFn for runtime registration. */
|
|
52
|
+
set toolInstances(instances) {
|
|
53
|
+
this._toolInstances = instances;
|
|
54
|
+
}
|
|
55
|
+
get toolInstances() {
|
|
56
|
+
return this._toolInstances;
|
|
57
|
+
}
|
|
58
|
+
/** Full table definitions for runtime table registration. */
|
|
59
|
+
set tableDefinitions(defs) {
|
|
60
|
+
this._tableDefinitions = defs;
|
|
61
|
+
}
|
|
62
|
+
get tableDefinitions() {
|
|
63
|
+
return this._tableDefinitions;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Declare that this contract's implementation also satisfies another
|
|
67
|
+
* contract at the given version or inclusive version range.
|
|
68
|
+
*/
|
|
69
|
+
alsoImplements(targetContractId, versionOrRange) {
|
|
70
|
+
if (!(0, utils_1.isid)(targetContractId)) {
|
|
71
|
+
throw new Error(`alsoImplements targetContractId must be a valid peer ID (25-char alphanumeric), got "${targetContractId}"`);
|
|
72
|
+
}
|
|
73
|
+
this._alsoImplements.push({ contractId: targetContractId, version: versionOrRange });
|
|
74
|
+
}
|
|
75
|
+
/** @internal Build the contract definition by extracting shapes. */
|
|
76
|
+
_build() {
|
|
77
|
+
return {
|
|
78
|
+
definition: {
|
|
79
|
+
contractId: this.contractId,
|
|
80
|
+
version: this.version,
|
|
81
|
+
devTag: this.devTag,
|
|
82
|
+
name: this.name,
|
|
83
|
+
description: this.description ?? "",
|
|
84
|
+
tables: this._tables.map(extract_1.extractTableShape),
|
|
85
|
+
tools: this._tools.map(extract_1.extractToolShape),
|
|
86
|
+
observables: [...this._observables],
|
|
87
|
+
},
|
|
88
|
+
alsoImplements: [...this._alsoImplements],
|
|
89
|
+
toolInstances: [...this._toolInstances],
|
|
90
|
+
tableDefinitions: [...this._tableDefinitions],
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.ContractBuilder = ContractBuilder;
|
|
95
|
+
/**
|
|
96
|
+
* Top-level builder passed to the `definePackage` callback.
|
|
97
|
+
*
|
|
98
|
+
* Packages may define **zero** contracts (e.g. UI-only apps with no persisted
|
|
99
|
+
* tables). Tables, tool shapes, and executable `toolInstances` /
|
|
100
|
+
* `tableDefinitions` are only attached via {@link PackageBuilder.contract} —
|
|
101
|
+
* there is no package-level API for them, so persistence always implies a
|
|
102
|
+
* contract.
|
|
103
|
+
*/
|
|
104
|
+
class PackageBuilder {
|
|
105
|
+
_packageId;
|
|
106
|
+
_version;
|
|
107
|
+
_versionTag;
|
|
108
|
+
_contracts = [];
|
|
109
|
+
_consumes = [];
|
|
110
|
+
/** Package-level assistants (not part of any contract). */
|
|
111
|
+
assistants = [];
|
|
112
|
+
/** Package-level app navigation items (not part of any contract). */
|
|
113
|
+
appNavs = [];
|
|
114
|
+
/** The peer ID of this package (25-char alphanumeric from `newid()`). */
|
|
115
|
+
set packageId(id) {
|
|
116
|
+
if (!(0, utils_1.isid)(id)) {
|
|
117
|
+
throw new Error(`packageId must be a valid peer ID (25-char alphanumeric), got "${id}"`);
|
|
118
|
+
}
|
|
119
|
+
this._packageId = id;
|
|
120
|
+
}
|
|
121
|
+
get packageId() {
|
|
122
|
+
return this._packageId;
|
|
123
|
+
}
|
|
124
|
+
/** Semantic version string (e.g. "1.0.0"). Typically imported from package.json at build time. */
|
|
125
|
+
set version(v) {
|
|
126
|
+
this._version = v;
|
|
127
|
+
}
|
|
128
|
+
get version() {
|
|
129
|
+
return this._version;
|
|
130
|
+
}
|
|
131
|
+
/** Version channel tag (e.g. "dev", "beta", "stable"). */
|
|
132
|
+
set versionTag(tag) {
|
|
133
|
+
this._versionTag = tag;
|
|
134
|
+
}
|
|
135
|
+
get versionTag() {
|
|
136
|
+
return this._versionTag;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Define (and implement) a contract at the given version.
|
|
140
|
+
* Returns a `ContractBuilder` for assigning tables, tools, and observables.
|
|
141
|
+
*
|
|
142
|
+
* @param contractId - Must be a valid peer ID (25-char alphanumeric from `newid()`).
|
|
143
|
+
* @param version - Positive integer version number.
|
|
144
|
+
* @param name - Human-readable contract name. This is the meaningful identifier that creators can change over time.
|
|
145
|
+
* @param devTag - Set to `"dev"` while the contract shape is still mutable.
|
|
146
|
+
*/
|
|
147
|
+
contract(contractId, version, name, devTag) {
|
|
148
|
+
if (!(0, utils_1.isid)(contractId)) {
|
|
149
|
+
throw new Error(`contractId must be a valid peer ID (25-char alphanumeric), got "${contractId}"`);
|
|
150
|
+
}
|
|
151
|
+
if (version < 1 || !Number.isInteger(version)) {
|
|
152
|
+
throw new Error(`Contract version must be a positive integer, got ${version}`);
|
|
153
|
+
}
|
|
154
|
+
const existing = this._contracts.find((c) => c.contractId === contractId && c.version === version);
|
|
155
|
+
if (existing) {
|
|
156
|
+
throw new Error(`Duplicate contract: ${contractId} v${version} is already defined in this package`);
|
|
157
|
+
}
|
|
158
|
+
const builder = new ContractBuilder(contractId, version, name, undefined, devTag);
|
|
159
|
+
this._contracts.push(builder);
|
|
160
|
+
return builder;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Declare a dependency on another package's contract.
|
|
164
|
+
*/
|
|
165
|
+
consumes(contractId, version) {
|
|
166
|
+
if (!(0, utils_1.isid)(contractId)) {
|
|
167
|
+
throw new Error(`consumes contractId must be a valid peer ID (25-char alphanumeric), got "${contractId}"`);
|
|
168
|
+
}
|
|
169
|
+
this._consumes.push({ contractId, version });
|
|
170
|
+
}
|
|
171
|
+
/** @internal Finalize and return the package definition result. */
|
|
172
|
+
_build() {
|
|
173
|
+
if (!this._packageId) {
|
|
174
|
+
throw new Error("A package must set packageId before building");
|
|
175
|
+
}
|
|
176
|
+
const contracts = [];
|
|
177
|
+
const alsoImplementsMap = new Map();
|
|
178
|
+
const allToolInstances = [];
|
|
179
|
+
const allTableDefinitions = [];
|
|
180
|
+
for (const cb of this._contracts) {
|
|
181
|
+
const { definition, alsoImplements, toolInstances, tableDefinitions } = cb._build();
|
|
182
|
+
contracts.push(definition);
|
|
183
|
+
if (alsoImplements.length > 0) {
|
|
184
|
+
alsoImplementsMap.set((0, types_1.contractKey)(cb.contractId, cb.version), alsoImplements);
|
|
185
|
+
}
|
|
186
|
+
allToolInstances.push(...toolInstances);
|
|
187
|
+
allTableDefinitions.push(...tableDefinitions);
|
|
188
|
+
}
|
|
189
|
+
if (contracts.length === 0 && (allToolInstances.length > 0 || allTableDefinitions.length > 0)) {
|
|
190
|
+
throw new Error("Tables and executable tools must be declared on a contract (use pkg.contract(...), then assign .tables, .tools, .toolInstances, or .tableDefinitions on that builder). UI-only packages omit contracts and do not register tables or tools.");
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
packageId: this._packageId,
|
|
194
|
+
version: this._version,
|
|
195
|
+
versionTag: this._versionTag,
|
|
196
|
+
contracts,
|
|
197
|
+
consumes: [...this._consumes],
|
|
198
|
+
alsoImplements: alsoImplementsMap,
|
|
199
|
+
assistants: [...this.assistants],
|
|
200
|
+
appNavs: [...this.appNavs],
|
|
201
|
+
toolInstances: allToolInstances,
|
|
202
|
+
tableDefinitions: allTableDefinitions,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
exports.PackageBuilder = PackageBuilder;
|
|
207
|
+
/**
|
|
208
|
+
* Entry point for defining a package. The callback receives a
|
|
209
|
+
* `PackageBuilder` which is used to declare contracts, consumptions, and
|
|
210
|
+
* package-level items. Returns the fully-built package definition.
|
|
211
|
+
*/
|
|
212
|
+
function definePackage(fn) {
|
|
213
|
+
const pkg = new PackageBuilder();
|
|
214
|
+
fn(pkg);
|
|
215
|
+
return pkg._build();
|
|
216
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { DataContext } from "../context/data-context";
|
|
3
|
+
import { Table } from "../data/orm/table";
|
|
4
|
+
import { type ITableMetaData } from "../data/orm/types";
|
|
5
|
+
/**
|
|
6
|
+
* Zod schema for persisted contract provider mappings.
|
|
7
|
+
* Tracks which package provides each contract and whether it is the active
|
|
8
|
+
* provider for resolution purposes.
|
|
9
|
+
*/
|
|
10
|
+
declare const schema: z.ZodObject<{
|
|
11
|
+
contractProviderId: z.ZodEffects<z.ZodString, string, string>;
|
|
12
|
+
contractId: z.ZodEffects<z.ZodString, string, string>;
|
|
13
|
+
version: z.ZodNumber;
|
|
14
|
+
providerPackageId: z.ZodEffects<z.ZodString, string, string>;
|
|
15
|
+
isActive: z.ZodBoolean;
|
|
16
|
+
registeredAt: z.ZodString;
|
|
17
|
+
}, "strip", z.ZodTypeAny, {
|
|
18
|
+
version: number;
|
|
19
|
+
contractProviderId: string;
|
|
20
|
+
contractId: string;
|
|
21
|
+
providerPackageId: string;
|
|
22
|
+
isActive: boolean;
|
|
23
|
+
registeredAt: string;
|
|
24
|
+
}, {
|
|
25
|
+
version: number;
|
|
26
|
+
contractProviderId: string;
|
|
27
|
+
contractId: string;
|
|
28
|
+
providerPackageId: string;
|
|
29
|
+
isActive: boolean;
|
|
30
|
+
registeredAt: string;
|
|
31
|
+
}>;
|
|
32
|
+
declare const metaData: ITableMetaData;
|
|
33
|
+
/** Row type for the ContractProviders table. */
|
|
34
|
+
export type IContractProviderRecord = z.infer<typeof schema>;
|
|
35
|
+
/** Table class — no custom logic needed beyond base Table. */
|
|
36
|
+
export declare class ContractProvidersTable extends Table<IContractProviderRecord> {
|
|
37
|
+
}
|
|
38
|
+
/** Accessor for the ContractProviders table. */
|
|
39
|
+
export declare function ContractProviders(dataContext?: DataContext): ContractProvidersTable;
|
|
40
|
+
export { schema as contractProvidersSchema, metaData as contractProvidersMetaData };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.contractProvidersMetaData = exports.contractProvidersSchema = exports.ContractProvidersTable = void 0;
|
|
4
|
+
exports.ContractProviders = ContractProviders;
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const context_1 = require("../context");
|
|
7
|
+
const table_1 = require("../data/orm/table");
|
|
8
|
+
const table_definitions_system_1 = require("../data/orm/table-definitions.system");
|
|
9
|
+
const types_1 = require("../data/orm/types");
|
|
10
|
+
const zod_types_1 = require("../types/zod-types");
|
|
11
|
+
/**
|
|
12
|
+
* Zod schema for persisted contract provider mappings.
|
|
13
|
+
* Tracks which package provides each contract and whether it is the active
|
|
14
|
+
* provider for resolution purposes.
|
|
15
|
+
*/
|
|
16
|
+
const schema = zod_1.z.object({
|
|
17
|
+
contractProviderId: zod_types_1.zodPeerId,
|
|
18
|
+
contractId: zod_types_1.zodPeerId.describe("Contract this provider implements"),
|
|
19
|
+
version: zod_1.z.number().describe("Contract version (positive integer)"),
|
|
20
|
+
providerPackageId: zod_types_1.zodPeerId.describe("Package that provides this contract"),
|
|
21
|
+
isActive: zod_1.z.boolean().describe("Whether this provider is the current active resolver"),
|
|
22
|
+
registeredAt: zod_1.z.string().describe("ISO 8601 timestamp of registration"),
|
|
23
|
+
});
|
|
24
|
+
exports.contractProvidersSchema = schema;
|
|
25
|
+
const metaData = {
|
|
26
|
+
name: "ContractProviders",
|
|
27
|
+
description: "Maps contracts to their provider packages and active selection",
|
|
28
|
+
primaryKeyName: "contractProviderId",
|
|
29
|
+
fields: (0, types_1.schemaToFields)(schema),
|
|
30
|
+
indexes: [{ fields: ["contractId", "version"] }, { fields: ["providerPackageId"] }],
|
|
31
|
+
};
|
|
32
|
+
exports.contractProvidersMetaData = metaData;
|
|
33
|
+
/** Table class — no custom logic needed beyond base Table. */
|
|
34
|
+
class ContractProvidersTable extends table_1.Table {
|
|
35
|
+
}
|
|
36
|
+
exports.ContractProvidersTable = ContractProvidersTable;
|
|
37
|
+
(0, table_definitions_system_1.registerSystemTableDefinition)(metaData, schema, ContractProvidersTable);
|
|
38
|
+
/** Accessor for the ContractProviders table. */
|
|
39
|
+
function ContractProviders(dataContext) {
|
|
40
|
+
return (0, context_1.getTableContainer)(dataContext).getTable(metaData, schema, ContractProvidersTable);
|
|
41
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { DataContext } from "../context/data-context";
|
|
3
|
+
import { Table } from "../data/orm/table";
|
|
4
|
+
import { type ITableMetaData } from "../data/orm/types";
|
|
5
|
+
/**
|
|
6
|
+
* Zod schema for persisted contracts.
|
|
7
|
+
* The `shape` field stores the serialized `{ tables, tools, observables }` JSON
|
|
8
|
+
* so the full `IContractDefinition` can be rehydrated without re-extracting from
|
|
9
|
+
* live package code.
|
|
10
|
+
*/
|
|
11
|
+
declare const schema: z.ZodObject<{
|
|
12
|
+
contractId: z.ZodEffects<z.ZodString, string, string>;
|
|
13
|
+
version: z.ZodNumber;
|
|
14
|
+
devTag: z.ZodOptional<z.ZodString>;
|
|
15
|
+
name: z.ZodString;
|
|
16
|
+
description: z.ZodString;
|
|
17
|
+
shape: z.ZodString;
|
|
18
|
+
registeredAt: z.ZodString;
|
|
19
|
+
}, "strip", z.ZodTypeAny, {
|
|
20
|
+
name: string;
|
|
21
|
+
description: string;
|
|
22
|
+
shape: string;
|
|
23
|
+
version: number;
|
|
24
|
+
contractId: string;
|
|
25
|
+
registeredAt: string;
|
|
26
|
+
devTag?: string | undefined;
|
|
27
|
+
}, {
|
|
28
|
+
name: string;
|
|
29
|
+
description: string;
|
|
30
|
+
shape: string;
|
|
31
|
+
version: number;
|
|
32
|
+
contractId: string;
|
|
33
|
+
registeredAt: string;
|
|
34
|
+
devTag?: string | undefined;
|
|
35
|
+
}>;
|
|
36
|
+
declare const metaData: ITableMetaData;
|
|
37
|
+
/** Row type for the Contracts table. */
|
|
38
|
+
export type IContractRecord = z.infer<typeof schema>;
|
|
39
|
+
/** Table class — no custom logic needed beyond base Table. */
|
|
40
|
+
export declare class ContractsTable extends Table<IContractRecord> {
|
|
41
|
+
}
|
|
42
|
+
/** Accessor for the Contracts table. */
|
|
43
|
+
export declare function Contracts(dataContext?: DataContext): ContractsTable;
|
|
44
|
+
export { schema as contractsSchema, metaData as contractsMetaData };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.contractsMetaData = exports.contractsSchema = exports.ContractsTable = void 0;
|
|
4
|
+
exports.Contracts = Contracts;
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const context_1 = require("../context");
|
|
7
|
+
const table_1 = require("../data/orm/table");
|
|
8
|
+
const table_definitions_system_1 = require("../data/orm/table-definitions.system");
|
|
9
|
+
const types_1 = require("../data/orm/types");
|
|
10
|
+
const zod_types_1 = require("../types/zod-types");
|
|
11
|
+
/**
|
|
12
|
+
* Zod schema for persisted contracts.
|
|
13
|
+
* The `shape` field stores the serialized `{ tables, tools, observables }` JSON
|
|
14
|
+
* so the full `IContractDefinition` can be rehydrated without re-extracting from
|
|
15
|
+
* live package code.
|
|
16
|
+
*/
|
|
17
|
+
const schema = zod_1.z.object({
|
|
18
|
+
contractId: zod_types_1.zodPeerId,
|
|
19
|
+
version: zod_1.z.number().describe("Contract version (positive integer)"),
|
|
20
|
+
devTag: zod_1.z.string().optional().describe("'dev' if the contract shape is still mutable"),
|
|
21
|
+
name: zod_1.z.string().describe("Human-readable contract name"),
|
|
22
|
+
description: zod_1.z.string().describe("Contract description"),
|
|
23
|
+
shape: zod_1.z.string().describe("JSON-serialized { tables, tools, observables }"),
|
|
24
|
+
registeredAt: zod_1.z.string().describe("ISO 8601 timestamp of first registration"),
|
|
25
|
+
});
|
|
26
|
+
exports.contractsSchema = schema;
|
|
27
|
+
const metaData = {
|
|
28
|
+
name: "Contracts",
|
|
29
|
+
description: "Persisted contract definition shapes for the contract registry",
|
|
30
|
+
primaryKeyName: "contractId",
|
|
31
|
+
fields: (0, types_1.schemaToFields)(schema),
|
|
32
|
+
localOnly: true,
|
|
33
|
+
indexes: [{ fields: ["contractId", "version"], unique: true }],
|
|
34
|
+
};
|
|
35
|
+
exports.contractsMetaData = metaData;
|
|
36
|
+
/** Table class — no custom logic needed beyond base Table. */
|
|
37
|
+
class ContractsTable extends table_1.Table {
|
|
38
|
+
}
|
|
39
|
+
exports.ContractsTable = ContractsTable;
|
|
40
|
+
(0, table_definitions_system_1.registerSystemTableDefinition)(metaData, schema, ContractsTable);
|
|
41
|
+
/** Accessor for the Contracts table. */
|
|
42
|
+
function Contracts(dataContext) {
|
|
43
|
+
return (0, context_1.getTableContainer)(dataContext).getTable(metaData, schema, ContractsTable);
|
|
44
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { IField } from "../types/field-type";
|
|
3
|
+
import type { IContractTable, IContractTool } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* Minimal table-definition shape accepted by extractTableShape.
|
|
6
|
+
* Mirrors the subset of ITableDefinition we need without importing the
|
|
7
|
+
* runtime module (which pulls in the full DI/table chain).
|
|
8
|
+
*/
|
|
9
|
+
export interface IExtractableTableDef {
|
|
10
|
+
metaData: {
|
|
11
|
+
name: string;
|
|
12
|
+
description: string;
|
|
13
|
+
primaryKeyName: string;
|
|
14
|
+
fields: IField[];
|
|
15
|
+
};
|
|
16
|
+
schema?: z.AnyZodObject;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Minimal tool-instance shape accepted by extractToolShape.
|
|
20
|
+
* Mirrors the subset of IToolInstance we need.
|
|
21
|
+
*/
|
|
22
|
+
export interface IExtractableToolInstance {
|
|
23
|
+
tool: {
|
|
24
|
+
name: string;
|
|
25
|
+
usageDescription: string;
|
|
26
|
+
inputSchema?: {
|
|
27
|
+
fields: IField[];
|
|
28
|
+
};
|
|
29
|
+
outputSchema?: {
|
|
30
|
+
fields: IField[];
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** Override the schemaToFields implementation (useful for tests). */
|
|
35
|
+
export declare function setSchemaToFieldsFn(fn: (schema: z.AnyZodObject) => IField[]): void;
|
|
36
|
+
/**
|
|
37
|
+
* Extract the contract-level table shape from a table definition.
|
|
38
|
+
* Prefers the Zod schema (via `schemaToFields`) when available, falling back
|
|
39
|
+
* to `metaData.fields`.
|
|
40
|
+
*/
|
|
41
|
+
export declare function extractTableShape(def: IExtractableTableDef): IContractTable;
|
|
42
|
+
/**
|
|
43
|
+
* Extract the contract-level tool shape from a tool instance.
|
|
44
|
+
* Uses the tool record's `inputSchema.fields` and `outputSchema.fields`.
|
|
45
|
+
*/
|
|
46
|
+
export declare function extractToolShape(inst: IExtractableToolInstance): IContractTool;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setSchemaToFieldsFn = setSchemaToFieldsFn;
|
|
4
|
+
exports.extractTableShape = extractTableShape;
|
|
5
|
+
exports.extractToolShape = extractToolShape;
|
|
6
|
+
/**
|
|
7
|
+
* Convert a Zod schema to IField[]. Lazy-loaded to avoid pulling in the
|
|
8
|
+
* heavy ORM module graph at import time. In production this resolves to the
|
|
9
|
+
* real `schemaToFields`; tests can call `setSchemaToFieldsFn` to inject a
|
|
10
|
+
* lightweight alternative.
|
|
11
|
+
*/
|
|
12
|
+
let _schemaToFields;
|
|
13
|
+
/** Override the schemaToFields implementation (useful for tests). */
|
|
14
|
+
function setSchemaToFieldsFn(fn) {
|
|
15
|
+
_schemaToFields = fn;
|
|
16
|
+
}
|
|
17
|
+
function getSchemaToFields() {
|
|
18
|
+
if (!_schemaToFields) {
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
20
|
+
_schemaToFields = require("../data/orm/types").schemaToFields;
|
|
21
|
+
}
|
|
22
|
+
return _schemaToFields;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Extract the contract-level table shape from a table definition.
|
|
26
|
+
* Prefers the Zod schema (via `schemaToFields`) when available, falling back
|
|
27
|
+
* to `metaData.fields`.
|
|
28
|
+
*/
|
|
29
|
+
function extractTableShape(def) {
|
|
30
|
+
const { metaData, schema } = def;
|
|
31
|
+
const fields = schema ? getSchemaToFields()(schema) : [...metaData.fields];
|
|
32
|
+
return {
|
|
33
|
+
name: metaData.name,
|
|
34
|
+
description: metaData.description,
|
|
35
|
+
primaryKeyName: metaData.primaryKeyName,
|
|
36
|
+
fields,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Extract the contract-level tool shape from a tool instance.
|
|
41
|
+
* Uses the tool record's `inputSchema.fields` and `outputSchema.fields`.
|
|
42
|
+
*/
|
|
43
|
+
function extractToolShape(inst) {
|
|
44
|
+
const { tool } = inst;
|
|
45
|
+
return {
|
|
46
|
+
name: tool.name,
|
|
47
|
+
usageDescription: tool.usageDescription,
|
|
48
|
+
inputFields: tool.inputSchema?.fields ?? [],
|
|
49
|
+
outputFields: tool.outputSchema?.fields ?? [],
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { ContractBuilder, definePackage, PackageBuilder } from "./builder";
|
|
2
|
+
export { ContractProviders, ContractProvidersTable, contractProvidersMetaData, contractProvidersSchema, type IContractProviderRecord, } from "./contract-providers.table";
|
|
3
|
+
export { Contracts, ContractsTable, contractsMetaData, contractsSchema, type IContractRecord, } from "./contracts.table";
|
|
4
|
+
export { extractTableShape, extractToolShape, type IExtractableTableDef, type IExtractableToolInstance, setSchemaToFieldsFn, } from "./extract";
|
|
5
|
+
export { PersistentContractRegistry } from "./persistent-registry";
|
|
6
|
+
export { ContractRegistry } from "./registry";
|
|
7
|
+
export type { IAlsoImplementsDeclaration, IConsumedContract, IContractDefinition, IContractObservable, IContractTable, IContractTool, IPackageDefinitionResult, IResolvedContract, IValidationError, IValidationResult, } from "./types";
|
|
8
|
+
export { contractKey } from "./types";
|
|
9
|
+
export { validateAlsoImplements, validateImmutability, validateProviderSatisfiesContract, } from "./validate";
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateProviderSatisfiesContract = exports.validateImmutability = exports.validateAlsoImplements = exports.contractKey = exports.ContractRegistry = exports.PersistentContractRegistry = exports.setSchemaToFieldsFn = exports.extractToolShape = exports.extractTableShape = exports.contractsSchema = exports.contractsMetaData = exports.ContractsTable = exports.Contracts = exports.contractProvidersSchema = exports.contractProvidersMetaData = exports.ContractProvidersTable = exports.ContractProviders = exports.PackageBuilder = exports.definePackage = exports.ContractBuilder = void 0;
|
|
4
|
+
var builder_1 = require("./builder");
|
|
5
|
+
Object.defineProperty(exports, "ContractBuilder", { enumerable: true, get: function () { return builder_1.ContractBuilder; } });
|
|
6
|
+
Object.defineProperty(exports, "definePackage", { enumerable: true, get: function () { return builder_1.definePackage; } });
|
|
7
|
+
Object.defineProperty(exports, "PackageBuilder", { enumerable: true, get: function () { return builder_1.PackageBuilder; } });
|
|
8
|
+
var contract_providers_table_1 = require("./contract-providers.table");
|
|
9
|
+
Object.defineProperty(exports, "ContractProviders", { enumerable: true, get: function () { return contract_providers_table_1.ContractProviders; } });
|
|
10
|
+
Object.defineProperty(exports, "ContractProvidersTable", { enumerable: true, get: function () { return contract_providers_table_1.ContractProvidersTable; } });
|
|
11
|
+
Object.defineProperty(exports, "contractProvidersMetaData", { enumerable: true, get: function () { return contract_providers_table_1.contractProvidersMetaData; } });
|
|
12
|
+
Object.defineProperty(exports, "contractProvidersSchema", { enumerable: true, get: function () { return contract_providers_table_1.contractProvidersSchema; } });
|
|
13
|
+
var contracts_table_1 = require("./contracts.table");
|
|
14
|
+
Object.defineProperty(exports, "Contracts", { enumerable: true, get: function () { return contracts_table_1.Contracts; } });
|
|
15
|
+
Object.defineProperty(exports, "ContractsTable", { enumerable: true, get: function () { return contracts_table_1.ContractsTable; } });
|
|
16
|
+
Object.defineProperty(exports, "contractsMetaData", { enumerable: true, get: function () { return contracts_table_1.contractsMetaData; } });
|
|
17
|
+
Object.defineProperty(exports, "contractsSchema", { enumerable: true, get: function () { return contracts_table_1.contractsSchema; } });
|
|
18
|
+
var extract_1 = require("./extract");
|
|
19
|
+
Object.defineProperty(exports, "extractTableShape", { enumerable: true, get: function () { return extract_1.extractTableShape; } });
|
|
20
|
+
Object.defineProperty(exports, "extractToolShape", { enumerable: true, get: function () { return extract_1.extractToolShape; } });
|
|
21
|
+
Object.defineProperty(exports, "setSchemaToFieldsFn", { enumerable: true, get: function () { return extract_1.setSchemaToFieldsFn; } });
|
|
22
|
+
var persistent_registry_1 = require("./persistent-registry");
|
|
23
|
+
Object.defineProperty(exports, "PersistentContractRegistry", { enumerable: true, get: function () { return persistent_registry_1.PersistentContractRegistry; } });
|
|
24
|
+
var registry_1 = require("./registry");
|
|
25
|
+
Object.defineProperty(exports, "ContractRegistry", { enumerable: true, get: function () { return registry_1.ContractRegistry; } });
|
|
26
|
+
var types_1 = require("./types");
|
|
27
|
+
Object.defineProperty(exports, "contractKey", { enumerable: true, get: function () { return types_1.contractKey; } });
|
|
28
|
+
var validate_1 = require("./validate");
|
|
29
|
+
Object.defineProperty(exports, "validateAlsoImplements", { enumerable: true, get: function () { return validate_1.validateAlsoImplements; } });
|
|
30
|
+
Object.defineProperty(exports, "validateImmutability", { enumerable: true, get: function () { return validate_1.validateImmutability; } });
|
|
31
|
+
Object.defineProperty(exports, "validateProviderSatisfiesContract", { enumerable: true, get: function () { return validate_1.validateProviderSatisfiesContract; } });
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { DataContext } from "../context/data-context";
|
|
2
|
+
import type { ContractRegistry } from "./registry";
|
|
3
|
+
import type { IContractDefinition } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* Bridges the in-memory `ContractRegistry` to SQLite-backed persistence tables.
|
|
6
|
+
* Handles persisting contract definitions and provider selections, as well as
|
|
7
|
+
* hydrating a fresh registry from stored data on startup.
|
|
8
|
+
*/
|
|
9
|
+
export declare class PersistentContractRegistry {
|
|
10
|
+
private registry;
|
|
11
|
+
private dataContext;
|
|
12
|
+
constructor(registry: ContractRegistry, dataContext: DataContext);
|
|
13
|
+
/**
|
|
14
|
+
* Persist a contract definition and its provider to the database.
|
|
15
|
+
* If the definition already exists (same contractId), updates it.
|
|
16
|
+
* Creates or updates the provider record with the given active status.
|
|
17
|
+
*/
|
|
18
|
+
persist(definition: IContractDefinition, providerPackageId: string, isActive: boolean): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Hydrate the in-memory registry from persisted definitions and providers.
|
|
21
|
+
* Registers all stored definitions with their respective providers, then
|
|
22
|
+
* sets the active provider for each contract.
|
|
23
|
+
*/
|
|
24
|
+
hydrate(): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Switch the active provider for a contract and persist the change.
|
|
27
|
+
* Returns false if the switch failed in the in-memory registry.
|
|
28
|
+
*/
|
|
29
|
+
setActiveProvider(contractId: string, version: number, providerPackageId: string): Promise<boolean>;
|
|
30
|
+
/** Convert a persisted contract record back to an `IContractDefinition`. */
|
|
31
|
+
private recordToDefinition;
|
|
32
|
+
}
|