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.
- package/CHANGELOG.md +59 -48
- package/package.json +66 -66
- package/src/command-handler.ts +137 -0
- package/src/errors.ts +2 -0
- package/src/import-service.ts +60 -60
- package/src/index.ts +36 -158
- package/src/profile-removal-service.ts +106 -106
- package/src/profile-sort-service.ts +105 -105
- package/src/profile-update-service.ts +134 -134
package/src/index.ts
CHANGED
|
@@ -1,158 +1,36 @@
|
|
|
1
|
-
import type { ExtensionAPI, ExtensionCommandContext
|
|
2
|
-
|
|
3
|
-
import { COMMAND_NAME,
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const summary = summarizeApplyOutcome(outcome);
|
|
38
|
-
ctx.ui.notify(`Profile '${outcome.profileName}' applied across ${summary}. Reloading…`, "info");
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
await ctx.reload();
|
|
42
|
-
} catch (error) {
|
|
43
|
-
ctx.ui.notify(
|
|
44
|
-
`Profile '${outcome.profileName}' applied across ${summary}, but automatic reload failed: ${toErrorMessage(error)}. Run /reload.`,
|
|
45
|
-
"error",
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async function handleModelProfilesCommand(ctx: ExtensionCommandContext): Promise<void> {
|
|
51
|
-
const agentOptions = { cwd: ctx.cwd, scope: "both" as const };
|
|
52
|
-
const prepared = loadAndPrepareProfiles(PROFILE_STORE_PATH, agentOptions);
|
|
53
|
-
notifyWarnings(ctx, prepared.warnings);
|
|
54
|
-
|
|
55
|
-
let data = prepared.data;
|
|
56
|
-
const activeAgentName = detectActiveAgentName(ctx.sessionManager, ctx.getSystemPrompt());
|
|
57
|
-
|
|
58
|
-
const result: ProfileModalResult = await openProfilesModal(ctx, data, activeAgentName, {
|
|
59
|
-
renameProfile: async (profileId, nextName) => {
|
|
60
|
-
data = renameStoredProfile(data, profileId, nextName);
|
|
61
|
-
saveProfilesFile(data, PROFILE_STORE_PATH);
|
|
62
|
-
return {
|
|
63
|
-
data,
|
|
64
|
-
message: `Renamed saved snapshot to '${nextName.trim()}'.`,
|
|
65
|
-
selectedProfileId: profileId,
|
|
66
|
-
};
|
|
67
|
-
},
|
|
68
|
-
addCurrentProfile: async () => {
|
|
69
|
-
const snapshot = captureAgentSnapshots(agentOptions);
|
|
70
|
-
notifyWarnings(ctx, snapshot.warnings);
|
|
71
|
-
const profile = createProfile(buildCurrentProfileName(activeAgentName, data), snapshot.agents);
|
|
72
|
-
data = appendProfile(data, profile);
|
|
73
|
-
saveProfilesFile(data, PROFILE_STORE_PATH);
|
|
74
|
-
return {
|
|
75
|
-
data,
|
|
76
|
-
message: `Saved current agents snapshot (${snapshot.agents.length} agents).`,
|
|
77
|
-
selectedProfileId: profile.id,
|
|
78
|
-
};
|
|
79
|
-
},
|
|
80
|
-
applyProfile: async (profileId) => {
|
|
81
|
-
const profile = findProfileById(data, profileId);
|
|
82
|
-
if (!profile) {
|
|
83
|
-
throw new Error(`Saved profile '${profileId}' was not found.`);
|
|
84
|
-
}
|
|
85
|
-
const applied = applySavedProfile(profile, agentOptions);
|
|
86
|
-
notifyWarnings(ctx, applied.warnings);
|
|
87
|
-
return {
|
|
88
|
-
...applied,
|
|
89
|
-
profileName: profile.name,
|
|
90
|
-
};
|
|
91
|
-
},
|
|
92
|
-
removeProfile: async (profileId) => {
|
|
93
|
-
const profile = findProfileById(data, profileId);
|
|
94
|
-
if (!profile) {
|
|
95
|
-
throw new Error(`Saved profile '${profileId}' was not found.`);
|
|
96
|
-
}
|
|
97
|
-
const removal = removeProfileAndUpdate(data, profileId);
|
|
98
|
-
data = removal.data;
|
|
99
|
-
saveProfilesFile(data, PROFILE_STORE_PATH);
|
|
100
|
-
return {
|
|
101
|
-
data,
|
|
102
|
-
message: `Removed saved snapshot '${removal.result.removedProfileName}' (${removal.result.remainingCount} snapshots remaining).`,
|
|
103
|
-
selectedProfileId: data.profiles[0]?.id,
|
|
104
|
-
};
|
|
105
|
-
},
|
|
106
|
-
updateProfile: async (profileId) => {
|
|
107
|
-
const profile = findProfileById(data, profileId);
|
|
108
|
-
if (!profile) {
|
|
109
|
-
throw new Error(`Saved profile '${profileId}' was not found.`);
|
|
110
|
-
}
|
|
111
|
-
const update = updateProfileAndReturn(data, profileId, agentOptions);
|
|
112
|
-
notifyWarnings(ctx, update.result.warnings);
|
|
113
|
-
data = update.data;
|
|
114
|
-
saveProfilesFile(data, PROFILE_STORE_PATH);
|
|
115
|
-
return {
|
|
116
|
-
data,
|
|
117
|
-
message: `Updated '${profile.name}' with current agent state (${update.result.updatedAgents} agents).`,
|
|
118
|
-
selectedProfileId: profileId,
|
|
119
|
-
};
|
|
120
|
-
},
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
if (result.type === "applied") {
|
|
124
|
-
await reloadAfterApply(ctx, result.outcome);
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export default function modelProfilesExtension(pi: ExtensionAPI): void {
|
|
130
|
-
// Register handler to refresh model registry on /reload
|
|
131
|
-
// This works around the issue where AgentSession.reload() doesn't call ModelRegistry.refresh()
|
|
132
|
-
onResourcesDiscover(pi, (event, ctx): void => {
|
|
133
|
-
if (event.reason === "reload") {
|
|
134
|
-
refreshModelRegistry(ctx);
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
pi.registerCommand(COMMAND_NAME, {
|
|
139
|
-
description: "Open saved whole-agent model profile snapshots",
|
|
140
|
-
handler: async (args: string, ctx: ExtensionCommandContext): Promise<void> => {
|
|
141
|
-
if (args.trim()) {
|
|
142
|
-
ctx.ui.notify(`Usage: /${COMMAND_NAME}`, "warning");
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (!ctx.hasUI) {
|
|
147
|
-
ctx.ui.notify(`/${COMMAND_NAME} requires interactive TUI mode.`, "warning");
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
try {
|
|
152
|
-
await handleModelProfilesCommand(ctx);
|
|
153
|
-
} catch (error) {
|
|
154
|
-
ctx.ui.notify(`/${COMMAND_NAME} failed: ${toErrorMessage(error)}`, "error");
|
|
155
|
-
}
|
|
156
|
-
},
|
|
157
|
-
});
|
|
158
|
-
}
|
|
1
|
+
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
import { COMMAND_NAME, toErrorMessage } from "./errors.js";
|
|
4
|
+
import { onResourcesDiscover, refreshModelRegistry } from "./pi-api-utils.js";
|
|
5
|
+
|
|
6
|
+
export default function modelProfilesExtension(pi: ExtensionAPI): void {
|
|
7
|
+
// Register handler to refresh model registry on /reload
|
|
8
|
+
// This works around the issue where AgentSession.reload() doesn't call ModelRegistry.refresh()
|
|
9
|
+
onResourcesDiscover(pi, (event, ctx): void => {
|
|
10
|
+
if (event.reason === "reload") {
|
|
11
|
+
refreshModelRegistry(ctx);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
pi.registerCommand(COMMAND_NAME, {
|
|
16
|
+
description: "Open saved whole-agent model profile snapshots",
|
|
17
|
+
handler: async (args: string, ctx: ExtensionCommandContext): Promise<void> => {
|
|
18
|
+
if (args.trim()) {
|
|
19
|
+
ctx.ui.notify(`Usage: /${COMMAND_NAME}`, "warning");
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (!ctx.hasUI) {
|
|
24
|
+
ctx.ui.notify(`/${COMMAND_NAME} requires interactive TUI mode.`, "warning");
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const { handleModelProfilesCommand } = await import("./command-handler.js");
|
|
30
|
+
await handleModelProfilesCommand(ctx);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
ctx.ui.notify(`/${COMMAND_NAME} failed: ${toErrorMessage(error)}`, "error");
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
import { multiProfilesDebugLogger } from "./debug-logger.js";
|
|
2
|
-
import { ModelProfilesError } from "./errors.js";
|
|
3
|
-
import type { ProfilesFile, ProfileRemovalResult, SavedProfile } from "./types.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Error code for profile not found during removal.
|
|
7
|
-
*/
|
|
8
|
-
export const PROFILE_NOT_FOUND_CODE = "PROFILE_NOT_FOUND";
|
|
9
|
-
|
|
10
|
-
export interface ProfileRemovalUpdateResult {
|
|
11
|
-
data: ProfilesFile;
|
|
12
|
-
result: ProfileRemovalResult;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface ProfileRemovalPlan {
|
|
16
|
-
profile: SavedProfile;
|
|
17
|
-
remainingProfiles: SavedProfile[];
|
|
18
|
-
result: ProfileRemovalResult;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function createProfileNotFoundError(profileId: string): ModelProfilesError {
|
|
22
|
-
return new ModelProfilesError(
|
|
23
|
-
`Profile '${profileId}' not found. It may have been removed already.`,
|
|
24
|
-
PROFILE_NOT_FOUND_CODE,
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function buildRemovalPlan(data: ProfilesFile, profileId: string): ProfileRemovalPlan {
|
|
29
|
-
const profileIndex = data.profiles.findIndex((profile) => profile.id === profileId);
|
|
30
|
-
const profile = data.profiles[profileIndex];
|
|
31
|
-
|
|
32
|
-
if (profileIndex === -1 || !profile) {
|
|
33
|
-
multiProfilesDebugLogger.log("profile-removal", {
|
|
34
|
-
event: "removal_failed",
|
|
35
|
-
profileId,
|
|
36
|
-
reason: "profile_not_found",
|
|
37
|
-
profileCount: data.profiles.length,
|
|
38
|
-
});
|
|
39
|
-
throw createProfileNotFoundError(profileId);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const remainingProfiles = [
|
|
43
|
-
...data.profiles.slice(0, profileIndex),
|
|
44
|
-
...data.profiles.slice(profileIndex + 1),
|
|
45
|
-
];
|
|
46
|
-
|
|
47
|
-
return {
|
|
48
|
-
profile,
|
|
49
|
-
remainingProfiles,
|
|
50
|
-
result: {
|
|
51
|
-
removedProfileId: profile.id,
|
|
52
|
-
removedProfileName: profile.name,
|
|
53
|
-
remainingCount: remainingProfiles.length,
|
|
54
|
-
},
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function logProfileRemoved(plan: ProfileRemovalPlan): void {
|
|
59
|
-
multiProfilesDebugLogger.log("profile-removal", {
|
|
60
|
-
event: "profile_removed",
|
|
61
|
-
profileId: plan.profile.id,
|
|
62
|
-
profileName: plan.profile.name,
|
|
63
|
-
agentCount: plan.profile.agents.length,
|
|
64
|
-
remainingCount: plan.result.remainingCount,
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Remove a profile from the profiles file.
|
|
70
|
-
*
|
|
71
|
-
* Validates that the profile exists before removal.
|
|
72
|
-
* Preserves other profiles unchanged (immutable pattern).
|
|
73
|
-
* Logs removal event via debug logger.
|
|
74
|
-
*
|
|
75
|
-
* @param data - The profiles file data
|
|
76
|
-
* @param profileId - The ID of the profile to remove
|
|
77
|
-
* @returns ProfileRemovalResult with removed profile info and remaining count
|
|
78
|
-
* @throws ModelProfilesError with code PROFILE_NOT_FOUND if profile doesn't exist
|
|
79
|
-
*/
|
|
80
|
-
export function removeProfile(data: ProfilesFile, profileId: string): ProfileRemovalResult {
|
|
81
|
-
const plan = buildRemovalPlan(data, profileId);
|
|
82
|
-
logProfileRemoved(plan);
|
|
83
|
-
return plan.result;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Remove a profile and return the updated profiles file with removal metadata.
|
|
88
|
-
*
|
|
89
|
-
* @param data - The profiles file data
|
|
90
|
-
* @param profileId - The ID of the profile to remove
|
|
91
|
-
* @returns New ProfilesFile with the profile removed and removal metadata
|
|
92
|
-
* @throws ModelProfilesError with code PROFILE_NOT_FOUND if profile doesn't exist
|
|
93
|
-
*/
|
|
94
|
-
export function removeProfileAndUpdate(data: ProfilesFile, profileId: string): ProfileRemovalUpdateResult {
|
|
95
|
-
const plan = buildRemovalPlan(data, profileId);
|
|
96
|
-
logProfileRemoved(plan);
|
|
97
|
-
|
|
98
|
-
return {
|
|
99
|
-
data: {
|
|
100
|
-
version: data.version,
|
|
101
|
-
importedAt: data.importedAt,
|
|
102
|
-
profiles: plan.remainingProfiles,
|
|
103
|
-
},
|
|
104
|
-
result: plan.result,
|
|
105
|
-
};
|
|
106
|
-
}
|
|
1
|
+
import { multiProfilesDebugLogger } from "./debug-logger.js";
|
|
2
|
+
import { ModelProfilesError } from "./errors.js";
|
|
3
|
+
import type { ProfilesFile, ProfileRemovalResult, SavedProfile } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Error code for profile not found during removal.
|
|
7
|
+
*/
|
|
8
|
+
export const PROFILE_NOT_FOUND_CODE = "PROFILE_NOT_FOUND";
|
|
9
|
+
|
|
10
|
+
export interface ProfileRemovalUpdateResult {
|
|
11
|
+
data: ProfilesFile;
|
|
12
|
+
result: ProfileRemovalResult;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface ProfileRemovalPlan {
|
|
16
|
+
profile: SavedProfile;
|
|
17
|
+
remainingProfiles: SavedProfile[];
|
|
18
|
+
result: ProfileRemovalResult;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function createProfileNotFoundError(profileId: string): ModelProfilesError {
|
|
22
|
+
return new ModelProfilesError(
|
|
23
|
+
`Profile '${profileId}' not found. It may have been removed already.`,
|
|
24
|
+
PROFILE_NOT_FOUND_CODE,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function buildRemovalPlan(data: ProfilesFile, profileId: string): ProfileRemovalPlan {
|
|
29
|
+
const profileIndex = data.profiles.findIndex((profile) => profile.id === profileId);
|
|
30
|
+
const profile = data.profiles[profileIndex];
|
|
31
|
+
|
|
32
|
+
if (profileIndex === -1 || !profile) {
|
|
33
|
+
multiProfilesDebugLogger.log("profile-removal", {
|
|
34
|
+
event: "removal_failed",
|
|
35
|
+
profileId,
|
|
36
|
+
reason: "profile_not_found",
|
|
37
|
+
profileCount: data.profiles.length,
|
|
38
|
+
});
|
|
39
|
+
throw createProfileNotFoundError(profileId);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const remainingProfiles = [
|
|
43
|
+
...data.profiles.slice(0, profileIndex),
|
|
44
|
+
...data.profiles.slice(profileIndex + 1),
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
profile,
|
|
49
|
+
remainingProfiles,
|
|
50
|
+
result: {
|
|
51
|
+
removedProfileId: profile.id,
|
|
52
|
+
removedProfileName: profile.name,
|
|
53
|
+
remainingCount: remainingProfiles.length,
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function logProfileRemoved(plan: ProfileRemovalPlan): void {
|
|
59
|
+
multiProfilesDebugLogger.log("profile-removal", {
|
|
60
|
+
event: "profile_removed",
|
|
61
|
+
profileId: plan.profile.id,
|
|
62
|
+
profileName: plan.profile.name,
|
|
63
|
+
agentCount: plan.profile.agents.length,
|
|
64
|
+
remainingCount: plan.result.remainingCount,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Remove a profile from the profiles file.
|
|
70
|
+
*
|
|
71
|
+
* Validates that the profile exists before removal.
|
|
72
|
+
* Preserves other profiles unchanged (immutable pattern).
|
|
73
|
+
* Logs removal event via debug logger.
|
|
74
|
+
*
|
|
75
|
+
* @param data - The profiles file data
|
|
76
|
+
* @param profileId - The ID of the profile to remove
|
|
77
|
+
* @returns ProfileRemovalResult with removed profile info and remaining count
|
|
78
|
+
* @throws ModelProfilesError with code PROFILE_NOT_FOUND if profile doesn't exist
|
|
79
|
+
*/
|
|
80
|
+
export function removeProfile(data: ProfilesFile, profileId: string): ProfileRemovalResult {
|
|
81
|
+
const plan = buildRemovalPlan(data, profileId);
|
|
82
|
+
logProfileRemoved(plan);
|
|
83
|
+
return plan.result;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Remove a profile and return the updated profiles file with removal metadata.
|
|
88
|
+
*
|
|
89
|
+
* @param data - The profiles file data
|
|
90
|
+
* @param profileId - The ID of the profile to remove
|
|
91
|
+
* @returns New ProfilesFile with the profile removed and removal metadata
|
|
92
|
+
* @throws ModelProfilesError with code PROFILE_NOT_FOUND if profile doesn't exist
|
|
93
|
+
*/
|
|
94
|
+
export function removeProfileAndUpdate(data: ProfilesFile, profileId: string): ProfileRemovalUpdateResult {
|
|
95
|
+
const plan = buildRemovalPlan(data, profileId);
|
|
96
|
+
logProfileRemoved(plan);
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
data: {
|
|
100
|
+
version: data.version,
|
|
101
|
+
importedAt: data.importedAt,
|
|
102
|
+
profiles: plan.remainingProfiles,
|
|
103
|
+
},
|
|
104
|
+
result: plan.result,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
@@ -1,105 +1,105 @@
|
|
|
1
|
-
import { multiProfilesDebugLogger } from "./debug-logger.js";
|
|
2
|
-
import { loadMultiProfilesConfig, saveMultiProfilesConfig } from "./config.js";
|
|
3
|
-
import type { ProfileSortResult, ProfilesFile, SavedProfile } from "./types.js";
|
|
4
|
-
import type { ProfileSortOrder } from "./types.js";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Compare two profile names using locale-aware string comparison.
|
|
8
|
-
*/
|
|
9
|
-
function compareByName(left: SavedProfile, right: SavedProfile): number {
|
|
10
|
-
return left.name.localeCompare(right.name);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Compare two profiles by their createdAt timestamp.
|
|
15
|
-
*/
|
|
16
|
-
function compareByDate(left: SavedProfile, right: SavedProfile): number {
|
|
17
|
-
const leftDate = new Date(left.createdAt).getTime();
|
|
18
|
-
const rightDate = new Date(right.createdAt).getTime();
|
|
19
|
-
return leftDate - rightDate;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Sort profiles based on the specified order.
|
|
24
|
-
* Returns a new array (immutable pattern), preserving the original data.profiles.
|
|
25
|
-
*
|
|
26
|
-
* @param data - The profiles file containing the profiles array
|
|
27
|
-
* @param order - The sort order to apply
|
|
28
|
-
* @returns ProfileSortResult with sorted profiles and applied sort order
|
|
29
|
-
*/
|
|
30
|
-
export function sortProfiles(data: ProfilesFile, order: ProfileSortOrder): ProfileSortResult {
|
|
31
|
-
const comparator = getSortComparator(order);
|
|
32
|
-
const sortedProfiles = [...data.profiles].sort(comparator);
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
sortedProfiles,
|
|
36
|
-
sortOrder: order,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Get the comparator function for a given sort order.
|
|
42
|
-
*/
|
|
43
|
-
function getSortComparator(order: ProfileSortOrder): (left: SavedProfile, right: SavedProfile) => number {
|
|
44
|
-
switch (order) {
|
|
45
|
-
case "name-asc":
|
|
46
|
-
return compareByName;
|
|
47
|
-
case "name-desc":
|
|
48
|
-
return (left, right) => compareByName(right, left);
|
|
49
|
-
case "date-asc":
|
|
50
|
-
return compareByDate;
|
|
51
|
-
case "date-desc":
|
|
52
|
-
return (left, right) => compareByDate(right, left);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Get the current sort order from config.
|
|
58
|
-
*/
|
|
59
|
-
export function getCurrentSortOrder(): ProfileSortOrder {
|
|
60
|
-
const result = loadMultiProfilesConfig();
|
|
61
|
-
return result.config.sorting.defaultSort;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Persist the sort order to config.
|
|
66
|
-
*/
|
|
67
|
-
export function persistSortOrder(order: ProfileSortOrder): void {
|
|
68
|
-
const result = loadMultiProfilesConfig();
|
|
69
|
-
const config = result.config;
|
|
70
|
-
config.sorting.defaultSort = order;
|
|
71
|
-
saveMultiProfilesConfig(config);
|
|
72
|
-
|
|
73
|
-
multiProfilesDebugLogger.log("config", {
|
|
74
|
-
event: "sort_order_persisted",
|
|
75
|
-
order,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Get display label for a sort order.
|
|
81
|
-
*/
|
|
82
|
-
export function getSortOrderLabel(order: ProfileSortOrder): string {
|
|
83
|
-
switch (order) {
|
|
84
|
-
case "name-asc":
|
|
85
|
-
return "Name (A-Z)";
|
|
86
|
-
case "name-desc":
|
|
87
|
-
return "Name (Z-A)";
|
|
88
|
-
case "date-asc":
|
|
89
|
-
return "Date (Oldest)";
|
|
90
|
-
case "date-desc":
|
|
91
|
-
return "Date (Newest)";
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Get all available sort orders with their labels.
|
|
97
|
-
*/
|
|
98
|
-
export function getAvailableSortOrders(): Array<{ order: ProfileSortOrder; label: string }> {
|
|
99
|
-
return [
|
|
100
|
-
{ order: "name-asc", label: "Name (A-Z)" },
|
|
101
|
-
{ order: "name-desc", label: "Name (Z-A)" },
|
|
102
|
-
{ order: "date-asc", label: "Date (Oldest)" },
|
|
103
|
-
{ order: "date-desc", label: "Date (Newest)" },
|
|
104
|
-
];
|
|
105
|
-
}
|
|
1
|
+
import { multiProfilesDebugLogger } from "./debug-logger.js";
|
|
2
|
+
import { loadMultiProfilesConfig, saveMultiProfilesConfig } from "./config.js";
|
|
3
|
+
import type { ProfileSortResult, ProfilesFile, SavedProfile } from "./types.js";
|
|
4
|
+
import type { ProfileSortOrder } from "./types.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Compare two profile names using locale-aware string comparison.
|
|
8
|
+
*/
|
|
9
|
+
function compareByName(left: SavedProfile, right: SavedProfile): number {
|
|
10
|
+
return left.name.localeCompare(right.name);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Compare two profiles by their createdAt timestamp.
|
|
15
|
+
*/
|
|
16
|
+
function compareByDate(left: SavedProfile, right: SavedProfile): number {
|
|
17
|
+
const leftDate = new Date(left.createdAt).getTime();
|
|
18
|
+
const rightDate = new Date(right.createdAt).getTime();
|
|
19
|
+
return leftDate - rightDate;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Sort profiles based on the specified order.
|
|
24
|
+
* Returns a new array (immutable pattern), preserving the original data.profiles.
|
|
25
|
+
*
|
|
26
|
+
* @param data - The profiles file containing the profiles array
|
|
27
|
+
* @param order - The sort order to apply
|
|
28
|
+
* @returns ProfileSortResult with sorted profiles and applied sort order
|
|
29
|
+
*/
|
|
30
|
+
export function sortProfiles(data: ProfilesFile, order: ProfileSortOrder): ProfileSortResult {
|
|
31
|
+
const comparator = getSortComparator(order);
|
|
32
|
+
const sortedProfiles = [...data.profiles].sort(comparator);
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
sortedProfiles,
|
|
36
|
+
sortOrder: order,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Get the comparator function for a given sort order.
|
|
42
|
+
*/
|
|
43
|
+
function getSortComparator(order: ProfileSortOrder): (left: SavedProfile, right: SavedProfile) => number {
|
|
44
|
+
switch (order) {
|
|
45
|
+
case "name-asc":
|
|
46
|
+
return compareByName;
|
|
47
|
+
case "name-desc":
|
|
48
|
+
return (left, right) => compareByName(right, left);
|
|
49
|
+
case "date-asc":
|
|
50
|
+
return compareByDate;
|
|
51
|
+
case "date-desc":
|
|
52
|
+
return (left, right) => compareByDate(right, left);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Get the current sort order from config.
|
|
58
|
+
*/
|
|
59
|
+
export function getCurrentSortOrder(): ProfileSortOrder {
|
|
60
|
+
const result = loadMultiProfilesConfig();
|
|
61
|
+
return result.config.sorting.defaultSort;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Persist the sort order to config.
|
|
66
|
+
*/
|
|
67
|
+
export function persistSortOrder(order: ProfileSortOrder): void {
|
|
68
|
+
const result = loadMultiProfilesConfig();
|
|
69
|
+
const config = result.config;
|
|
70
|
+
config.sorting.defaultSort = order;
|
|
71
|
+
saveMultiProfilesConfig(config);
|
|
72
|
+
|
|
73
|
+
multiProfilesDebugLogger.log("config", {
|
|
74
|
+
event: "sort_order_persisted",
|
|
75
|
+
order,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Get display label for a sort order.
|
|
81
|
+
*/
|
|
82
|
+
export function getSortOrderLabel(order: ProfileSortOrder): string {
|
|
83
|
+
switch (order) {
|
|
84
|
+
case "name-asc":
|
|
85
|
+
return "Name (A-Z)";
|
|
86
|
+
case "name-desc":
|
|
87
|
+
return "Name (Z-A)";
|
|
88
|
+
case "date-asc":
|
|
89
|
+
return "Date (Oldest)";
|
|
90
|
+
case "date-desc":
|
|
91
|
+
return "Date (Newest)";
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Get all available sort orders with their labels.
|
|
97
|
+
*/
|
|
98
|
+
export function getAvailableSortOrders(): Array<{ order: ProfileSortOrder; label: string }> {
|
|
99
|
+
return [
|
|
100
|
+
{ order: "name-asc", label: "Name (A-Z)" },
|
|
101
|
+
{ order: "name-desc", label: "Name (Z-A)" },
|
|
102
|
+
{ order: "date-asc", label: "Date (Oldest)" },
|
|
103
|
+
{ order: "date-desc", label: "Date (Newest)" },
|
|
104
|
+
];
|
|
105
|
+
}
|