agentnet 0.0.2 → 0.0.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.
@@ -0,0 +1,212 @@
1
+ import { logger } from './logger.js';
2
+ import { ConfigurationError } from '../errors/index.js';
3
+
4
+ /**
5
+ * Map of API versions with their compatibility information
6
+ */
7
+ export const API_VERSIONS = {
8
+ // Smartagent API versions
9
+ 'smartagent.io/v1alpha1': {
10
+ isSupported: true,
11
+ isStable: false,
12
+ minEngineVersion: '0.5.0',
13
+ features: ['basic', 'discovery', 'handoffs']
14
+ },
15
+ // Agentnet API versions
16
+ 'agentnet.io/v1alpha1': {
17
+ isSupported: true,
18
+ isStable: false,
19
+ minEngineVersion: '0.8.0',
20
+ features: ['basic', 'discovery', 'handoffs', 'advanced-routing']
21
+ }
22
+ };
23
+
24
+ /**
25
+ * Default API version to use when none is specified
26
+ */
27
+ export const DEFAULT_API_VERSION = 'smartagent.io/v1alpha1';
28
+
29
+ /**
30
+ * Latest stable API version
31
+ */
32
+ export const LATEST_STABLE_VERSION = 'agentnet.io/v1alpha1';
33
+
34
+ /**
35
+ * Version upgrade paths map
36
+ * Defines valid upgrade paths between versions
37
+ */
38
+ export const VERSION_UPGRADE_PATHS = {
39
+ 'smartagent.io/v1alpha1': ['agentnet.io/v1alpha1']
40
+ };
41
+
42
+ /**
43
+ * Check if an API version is supported
44
+ * @param {string} version - The API version to check
45
+ * @returns {boolean} True if the version is supported
46
+ */
47
+ export function isVersionSupported(version) {
48
+ return API_VERSIONS[version]?.isSupported === true;
49
+ }
50
+
51
+ /**
52
+ * Check if an API version is stable (not subject to breaking changes)
53
+ * @param {string} version - The API version to check
54
+ * @returns {boolean} True if the version is stable
55
+ */
56
+ export function isVersionStable(version) {
57
+ return API_VERSIONS[version]?.isStable === true;
58
+ }
59
+
60
+ /**
61
+ * Checks if a feature is supported in given API version
62
+ * @param {string} version - The API version to check
63
+ * @param {string} feature - The feature to check
64
+ * @returns {boolean} True if the feature is supported in this version
65
+ */
66
+ export function isFeatureSupported(version, feature) {
67
+ return API_VERSIONS[version]?.features?.includes(feature) === true;
68
+ }
69
+
70
+ /**
71
+ * Validates an agent definition's API version
72
+ * @param {object} definition - Agent definition with apiVersion
73
+ * @param {string} currentEngineVersion - Current version of the agent engine
74
+ * @returns {object} Validated version information
75
+ * @throws {ConfigurationError} If version is unsupported
76
+ */
77
+ export function validateApiVersion(definition, currentEngineVersion = '1.0.0') {
78
+ const apiVersion = definition.apiVersion || DEFAULT_API_VERSION;
79
+ const agentName = definition.metadata?.name || 'Unnamed Agent';
80
+
81
+ // Check if version exists
82
+ const versionInfo = API_VERSIONS[apiVersion];
83
+ if (!versionInfo) {
84
+ const supportedVersions = Object.keys(API_VERSIONS).join(', ');
85
+ throw new ConfigurationError(
86
+ `Unsupported apiVersion '${apiVersion}' for agent '${agentName}'. Supported versions: ${supportedVersions}`,
87
+ { apiVersion, agentName }
88
+ );
89
+ }
90
+
91
+ // Check if version is supported
92
+ if (!versionInfo.isSupported) {
93
+ throw new ConfigurationError(
94
+ `API version '${apiVersion}' is deprecated for agent '${agentName}'. Please update to a supported version.`,
95
+ { apiVersion, agentName }
96
+ );
97
+ }
98
+
99
+ // Check engine compatibility (simple version check for now)
100
+ if (versionInfo.minEngineVersion && versionInfo.minEngineVersion > currentEngineVersion) {
101
+ logger.warn(
102
+ `Agent '${agentName}' uses apiVersion '${apiVersion}' which requires engine version ${versionInfo.minEngineVersion} or higher. Current: ${currentEngineVersion}`,
103
+ { apiVersion, agentName, minEngineVersion: versionInfo.minEngineVersion, currentEngineVersion }
104
+ );
105
+ }
106
+
107
+ // Log stability warning for non-stable versions
108
+ if (!versionInfo.isStable) {
109
+ logger.warn(
110
+ `Agent '${agentName}' uses apiVersion '${apiVersion}' which is not marked as stable and may have breaking changes in future releases.`,
111
+ { apiVersion, agentName }
112
+ );
113
+ }
114
+
115
+ return {
116
+ version: apiVersion,
117
+ info: versionInfo
118
+ };
119
+ }
120
+
121
+ /**
122
+ * Get normalized version of API version
123
+ * This extracts just the version part, removing the domain prefix
124
+ * @param {string} apiVersion - Full API version string (e.g., 'agentnet.io/v1alpha1')
125
+ * @returns {string} Normalized version (e.g., 'v1alpha1')
126
+ */
127
+ export function getNormalizedVersion(apiVersion) {
128
+ const parts = apiVersion.split('/');
129
+ return parts.length > 1 ? parts[1] : apiVersion;
130
+ }
131
+
132
+ /**
133
+ * Checks if an upgrade path exists between versions
134
+ * @param {string} fromVersion - Source version
135
+ * @param {string} toVersion - Target version
136
+ * @returns {boolean} Whether a direct upgrade path exists
137
+ */
138
+ export function canUpgradeVersion(fromVersion, toVersion) {
139
+ if (fromVersion === toVersion) return true;
140
+ return VERSION_UPGRADE_PATHS[fromVersion]?.includes(toVersion) === true;
141
+ }
142
+
143
+ /**
144
+ * Get possible upgrade paths for a version
145
+ * @param {string} fromVersion - Current version
146
+ * @returns {string[]} Available upgrade targets
147
+ */
148
+ export function getUpgradeOptions(fromVersion) {
149
+ return VERSION_UPGRADE_PATHS[fromVersion] || [];
150
+ }
151
+
152
+ /**
153
+ * Migration helpers for each version transformation
154
+ */
155
+ const VERSION_MIGRATIONS = {
156
+ // Migration from smartagent.io/v1alpha1 to agentnet.io/v1alpha1
157
+ 'smartagent.io/v1alpha1->agentnet.io/v1alpha1': (definition) => {
158
+ // Deep clone the definition to avoid modifying the original
159
+ const newDef = JSON.parse(JSON.stringify(definition));
160
+
161
+ // Update the apiVersion
162
+ newDef.apiVersion = 'agentnet.io/v1alpha1';
163
+
164
+ // Handle specific field migrations
165
+ // Example: rename or restructure fields as needed
166
+
167
+ // Log the migration
168
+ logger.info(`Migrated agent definition from smartagent.io/v1alpha1 to agentnet.io/v1alpha1`, {
169
+ agentName: newDef.metadata?.name
170
+ });
171
+
172
+ return newDef;
173
+ }
174
+ };
175
+
176
+ /**
177
+ * Migrate an agent definition from one version to another
178
+ * @param {object} definition - Agent definition to migrate
179
+ * @param {string} targetVersion - Target API version
180
+ * @returns {object} Migrated definition
181
+ * @throws {ConfigurationError} If migration path doesn't exist
182
+ */
183
+ export function migrateDefinition(definition, targetVersion) {
184
+ const sourceVersion = definition.apiVersion || DEFAULT_API_VERSION;
185
+
186
+ // If already at target version, return as is
187
+ if (sourceVersion === targetVersion) {
188
+ return definition;
189
+ }
190
+
191
+ // Check if direct upgrade path exists
192
+ if (!canUpgradeVersion(sourceVersion, targetVersion)) {
193
+ throw new ConfigurationError(
194
+ `No direct migration path from ${sourceVersion} to ${targetVersion}`,
195
+ { sourceVersion, targetVersion }
196
+ );
197
+ }
198
+
199
+ // Get the migration function
200
+ const migrationKey = `${sourceVersion}->${targetVersion}`;
201
+ const migrationFn = VERSION_MIGRATIONS[migrationKey];
202
+
203
+ if (!migrationFn) {
204
+ throw new ConfigurationError(
205
+ `Migration path exists but no implementation found for ${migrationKey}`,
206
+ { sourceVersion, targetVersion }
207
+ );
208
+ }
209
+
210
+ // Apply the migration
211
+ return migrationFn(definition);
212
+ }