pi-model-profiles 0.3.1 → 0.3.3

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.
@@ -1,134 +1,134 @@
1
- import { multiProfilesDebugLogger } from "./debug-logger.js";
2
- import { ModelProfilesError } from "./errors.js";
3
- import { captureAgentSnapshots, type AgentSelectionOptions } from "./agent-writer.js";
4
- import type { ProfilesFile, ProfileUpdateResult, SavedProfile } from "./types.js";
5
-
6
- /**
7
- * Error code for profile not found during update.
8
- */
9
- export const PROFILE_NOT_FOUND_CODE = "PROFILE_NOT_FOUND";
10
-
11
- export interface ProfileUpdateDataResult {
12
- data: ProfilesFile;
13
- result: ProfileUpdateResult;
14
- }
15
-
16
- interface ProfileUpdatePlan {
17
- updatedData: ProfilesFile;
18
- updatedProfile: SavedProfile;
19
- previousProfile: SavedProfile;
20
- result: ProfileUpdateResult;
21
- }
22
-
23
- function createProfileNotFoundError(profileId: string): ModelProfilesError {
24
- return new ModelProfilesError(
25
- `Profile '${profileId}' not found. It may have been removed already.`,
26
- PROFILE_NOT_FOUND_CODE,
27
- );
28
- }
29
-
30
- function buildUpdatePlan(
31
- data: ProfilesFile,
32
- profileId: string,
33
- agentOptions: AgentSelectionOptions,
34
- ): ProfileUpdatePlan {
35
- const profileIndex = data.profiles.findIndex((profile) => profile.id === profileId);
36
- const profile = data.profiles[profileIndex];
37
-
38
- if (profileIndex === -1 || !profile) {
39
- multiProfilesDebugLogger.log("profile-update", {
40
- event: "update_failed",
41
- profileId,
42
- reason: "profile_not_found",
43
- profileCount: data.profiles.length,
44
- });
45
- throw createProfileNotFoundError(profileId);
46
- }
47
-
48
- const snapshot = captureAgentSnapshots(agentOptions);
49
- const updatedProfile: SavedProfile = {
50
- id: profile.id,
51
- name: profile.name,
52
- agents: snapshot.agents,
53
- createdAt: profile.createdAt,
54
- updatedAt: new Date().toISOString(),
55
- };
56
- const updatedProfiles = [
57
- ...data.profiles.slice(0, profileIndex),
58
- updatedProfile,
59
- ...data.profiles.slice(profileIndex + 1),
60
- ];
61
-
62
- return {
63
- updatedData: {
64
- version: data.version,
65
- importedAt: data.importedAt,
66
- profiles: updatedProfiles,
67
- },
68
- updatedProfile,
69
- previousProfile: profile,
70
- result: {
71
- updatedProfileId: profile.id,
72
- updatedProfileName: profile.name,
73
- updatedAgents: snapshot.agents.length,
74
- warnings: snapshot.warnings,
75
- },
76
- };
77
- }
78
-
79
- function logProfileUpdated(plan: ProfileUpdatePlan): void {
80
- multiProfilesDebugLogger.log("profile-update", {
81
- event: "profile_updated",
82
- profileId: plan.updatedProfile.id,
83
- profileName: plan.updatedProfile.name,
84
- beforeAgentCount: plan.previousProfile.agents.length,
85
- afterAgentCount: plan.updatedProfile.agents.length,
86
- warnings: plan.result.warnings,
87
- });
88
- }
89
-
90
- /**
91
- * Update a profile with the current agent state.
92
- *
93
- * Captures the current agent snapshots and replaces the profile's agents array.
94
- * Preserves profile.id, profile.name, and profile.createdAt.
95
- * Updates profile.updatedAt timestamp to current time.
96
- * Logs update event with before/after agent counts via debug logger.
97
- *
98
- * @param data - The profiles file data
99
- * @param profileId - The ID of the profile to update
100
- * @param agentOptions - Options for capturing agent snapshots
101
- * @returns ProfileUpdateResult with updated profile info and agent counts
102
- * @throws ModelProfilesError with code PROFILE_NOT_FOUND if profile doesn't exist
103
- */
104
- export function updateProfile(
105
- data: ProfilesFile,
106
- profileId: string,
107
- agentOptions: AgentSelectionOptions,
108
- ): ProfileUpdateResult {
109
- const plan = buildUpdatePlan(data, profileId, agentOptions);
110
- logProfileUpdated(plan);
111
- return plan.result;
112
- }
113
-
114
- /**
115
- * Update a profile and return the updated profiles file with update metadata.
116
- *
117
- * @param data - The profiles file data
118
- * @param profileId - The ID of the profile to update
119
- * @param agentOptions - Options for capturing agent snapshots
120
- * @returns New ProfilesFile with the profile updated and update metadata
121
- * @throws ModelProfilesError with code PROFILE_NOT_FOUND if profile doesn't exist
122
- */
123
- export function updateProfileAndReturn(
124
- data: ProfilesFile,
125
- profileId: string,
126
- agentOptions: AgentSelectionOptions,
127
- ): ProfileUpdateDataResult {
128
- const plan = buildUpdatePlan(data, profileId, agentOptions);
129
- logProfileUpdated(plan);
130
- return {
131
- data: plan.updatedData,
132
- result: plan.result,
133
- };
134
- }
1
+ import { multiProfilesDebugLogger } from "./debug-logger.js";
2
+ import { ModelProfilesError } from "./errors.js";
3
+ import { captureAgentSnapshots, type AgentSelectionOptions } from "./agent-writer.js";
4
+ import type { ProfilesFile, ProfileUpdateResult, SavedProfile } from "./types.js";
5
+
6
+ /**
7
+ * Error code for profile not found during update.
8
+ */
9
+ export const PROFILE_NOT_FOUND_CODE = "PROFILE_NOT_FOUND";
10
+
11
+ export interface ProfileUpdateDataResult {
12
+ data: ProfilesFile;
13
+ result: ProfileUpdateResult;
14
+ }
15
+
16
+ interface ProfileUpdatePlan {
17
+ updatedData: ProfilesFile;
18
+ updatedProfile: SavedProfile;
19
+ previousProfile: SavedProfile;
20
+ result: ProfileUpdateResult;
21
+ }
22
+
23
+ function createProfileNotFoundError(profileId: string): ModelProfilesError {
24
+ return new ModelProfilesError(
25
+ `Profile '${profileId}' not found. It may have been removed already.`,
26
+ PROFILE_NOT_FOUND_CODE,
27
+ );
28
+ }
29
+
30
+ function buildUpdatePlan(
31
+ data: ProfilesFile,
32
+ profileId: string,
33
+ agentOptions: AgentSelectionOptions,
34
+ ): ProfileUpdatePlan {
35
+ const profileIndex = data.profiles.findIndex((profile) => profile.id === profileId);
36
+ const profile = data.profiles[profileIndex];
37
+
38
+ if (profileIndex === -1 || !profile) {
39
+ multiProfilesDebugLogger.log("profile-update", {
40
+ event: "update_failed",
41
+ profileId,
42
+ reason: "profile_not_found",
43
+ profileCount: data.profiles.length,
44
+ });
45
+ throw createProfileNotFoundError(profileId);
46
+ }
47
+
48
+ const snapshot = captureAgentSnapshots(agentOptions);
49
+ const updatedProfile: SavedProfile = {
50
+ id: profile.id,
51
+ name: profile.name,
52
+ agents: snapshot.agents,
53
+ createdAt: profile.createdAt,
54
+ updatedAt: new Date().toISOString(),
55
+ };
56
+ const updatedProfiles = [
57
+ ...data.profiles.slice(0, profileIndex),
58
+ updatedProfile,
59
+ ...data.profiles.slice(profileIndex + 1),
60
+ ];
61
+
62
+ return {
63
+ updatedData: {
64
+ version: data.version,
65
+ importedAt: data.importedAt,
66
+ profiles: updatedProfiles,
67
+ },
68
+ updatedProfile,
69
+ previousProfile: profile,
70
+ result: {
71
+ updatedProfileId: profile.id,
72
+ updatedProfileName: profile.name,
73
+ updatedAgents: snapshot.agents.length,
74
+ warnings: snapshot.warnings,
75
+ },
76
+ };
77
+ }
78
+
79
+ function logProfileUpdated(plan: ProfileUpdatePlan): void {
80
+ multiProfilesDebugLogger.log("profile-update", {
81
+ event: "profile_updated",
82
+ profileId: plan.updatedProfile.id,
83
+ profileName: plan.updatedProfile.name,
84
+ beforeAgentCount: plan.previousProfile.agents.length,
85
+ afterAgentCount: plan.updatedProfile.agents.length,
86
+ warnings: plan.result.warnings,
87
+ });
88
+ }
89
+
90
+ /**
91
+ * Update a profile with the current agent state.
92
+ *
93
+ * Captures the current agent snapshots and replaces the profile's agents array.
94
+ * Preserves profile.id, profile.name, and profile.createdAt.
95
+ * Updates profile.updatedAt timestamp to current time.
96
+ * Logs update event with before/after agent counts via debug logger.
97
+ *
98
+ * @param data - The profiles file data
99
+ * @param profileId - The ID of the profile to update
100
+ * @param agentOptions - Options for capturing agent snapshots
101
+ * @returns ProfileUpdateResult with updated profile info and agent counts
102
+ * @throws ModelProfilesError with code PROFILE_NOT_FOUND if profile doesn't exist
103
+ */
104
+ export function updateProfile(
105
+ data: ProfilesFile,
106
+ profileId: string,
107
+ agentOptions: AgentSelectionOptions,
108
+ ): ProfileUpdateResult {
109
+ const plan = buildUpdatePlan(data, profileId, agentOptions);
110
+ logProfileUpdated(plan);
111
+ return plan.result;
112
+ }
113
+
114
+ /**
115
+ * Update a profile and return the updated profiles file with update metadata.
116
+ *
117
+ * @param data - The profiles file data
118
+ * @param profileId - The ID of the profile to update
119
+ * @param agentOptions - Options for capturing agent snapshots
120
+ * @returns New ProfilesFile with the profile updated and update metadata
121
+ * @throws ModelProfilesError with code PROFILE_NOT_FOUND if profile doesn't exist
122
+ */
123
+ export function updateProfileAndReturn(
124
+ data: ProfilesFile,
125
+ profileId: string,
126
+ agentOptions: AgentSelectionOptions,
127
+ ): ProfileUpdateDataResult {
128
+ const plan = buildUpdatePlan(data, profileId, agentOptions);
129
+ logProfileUpdated(plan);
130
+ return {
131
+ data: plan.updatedData,
132
+ result: plan.result,
133
+ };
134
+ }