pi-session-cleanup 1.1.0 → 1.1.2

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/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
2
 
3
3
  import { SESSION_CLEANUP_COMMAND, SESSION_NIX_COMMAND } from "./constants.js";
4
4
  import {
@@ -1,103 +1,103 @@
1
- import { readFile } from "node:fs/promises";
2
-
3
- import type { SessionInfo } from "@mariozechner/pi-coding-agent";
4
-
5
- import type { SessionCleanupSession } from "./types.js";
6
-
7
- function toRecord(value: unknown): Record<string, unknown> | null {
8
- return typeof value === "object" && value !== null ? (value as Record<string, unknown>) : null;
9
- }
10
-
11
- function normalizeAgentName(value: unknown): string | null {
12
- if (typeof value !== "string") {
13
- return null;
14
- }
15
-
16
- const trimmed = value.trim();
17
- return trimmed.length > 0 ? trimmed : null;
18
- }
19
-
20
- function parseJsonLine(value: string): Record<string, unknown> | null {
21
- try {
22
- return toRecord(JSON.parse(value));
23
- } catch {
24
- return null;
25
- }
26
- }
27
-
28
- export function extractResponsibleAgentNameFromContent(content: string): string | null {
29
- const lines = content.split(/\r?\n/);
30
-
31
- for (let index = lines.length - 1; index >= 0; index -= 1) {
32
- const trimmed = lines[index]?.trim();
33
- if (!trimmed) {
34
- continue;
35
- }
36
-
37
- const entry = parseJsonLine(trimmed);
38
- if (!entry || entry.type !== "custom" || entry.customType !== "active_agent") {
39
- continue;
40
- }
41
-
42
- const data = toRecord(entry.data);
43
- const normalizedAgentName = normalizeAgentName(data?.name);
44
- if (normalizedAgentName) {
45
- return normalizedAgentName;
46
- }
47
-
48
- if (data?.name === null) {
49
- return null;
50
- }
51
- }
52
-
53
- return null;
54
- }
55
-
56
- export async function resolveResponsibleAgentName(sessionPath: string): Promise<string | null> {
57
- if (typeof sessionPath !== "string" || sessionPath.trim().length === 0) {
58
- return null;
59
- }
60
-
61
- try {
62
- const content = await readFile(sessionPath, "utf8");
63
- return extractResponsibleAgentNameFromContent(content);
64
- } catch {
65
- return null;
66
- }
67
- }
68
-
69
- export function extractPersistedActiveAgentNameFromEntries(
70
- entries: readonly unknown[],
71
- ): string | null | undefined {
72
- for (let index = entries.length - 1; index >= 0; index -= 1) {
73
- const entry = toRecord(entries[index]);
74
- if (!entry || entry.type !== "custom" || entry.customType !== "active_agent") {
75
- continue;
76
- }
77
-
78
- const data = toRecord(entry.data);
79
- const normalizedAgentName = normalizeAgentName(data?.name);
80
- if (normalizedAgentName) {
81
- return normalizedAgentName;
82
- }
83
-
84
- if (data?.name === null) {
85
- return null;
86
- }
87
-
88
- return null;
89
- }
90
-
91
- return undefined;
92
- }
93
-
94
- export async function enrichSessionWithResponsibleAgent(
95
- session: SessionInfo,
96
- ): Promise<SessionCleanupSession> {
97
- const responsibleAgentName = await resolveResponsibleAgentName(session.path);
98
-
99
- return {
100
- ...session,
101
- responsibleAgentName,
102
- };
103
- }
1
+ import { readFile } from "node:fs/promises";
2
+
3
+ import type { SessionInfo } from "@earendil-works/pi-coding-agent";
4
+
5
+ import type { SessionCleanupSession } from "./types.js";
6
+
7
+ function toRecord(value: unknown): Record<string, unknown> | null {
8
+ return typeof value === "object" && value !== null ? (value as Record<string, unknown>) : null;
9
+ }
10
+
11
+ function normalizeAgentName(value: unknown): string | null {
12
+ if (typeof value !== "string") {
13
+ return null;
14
+ }
15
+
16
+ const trimmed = value.trim();
17
+ return trimmed.length > 0 ? trimmed : null;
18
+ }
19
+
20
+ function parseJsonLine(value: string): Record<string, unknown> | null {
21
+ try {
22
+ return toRecord(JSON.parse(value));
23
+ } catch {
24
+ return null;
25
+ }
26
+ }
27
+
28
+ export function extractResponsibleAgentNameFromContent(content: string): string | null {
29
+ const lines = content.split(/\r?\n/);
30
+
31
+ for (let index = lines.length - 1; index >= 0; index -= 1) {
32
+ const trimmed = lines[index]?.trim();
33
+ if (!trimmed) {
34
+ continue;
35
+ }
36
+
37
+ const entry = parseJsonLine(trimmed);
38
+ if (!entry || entry.type !== "custom" || entry.customType !== "active_agent") {
39
+ continue;
40
+ }
41
+
42
+ const data = toRecord(entry.data);
43
+ const normalizedAgentName = normalizeAgentName(data?.name);
44
+ if (normalizedAgentName) {
45
+ return normalizedAgentName;
46
+ }
47
+
48
+ if (data?.name === null) {
49
+ return null;
50
+ }
51
+ }
52
+
53
+ return null;
54
+ }
55
+
56
+ export async function resolveResponsibleAgentName(sessionPath: string): Promise<string | null> {
57
+ if (typeof sessionPath !== "string" || sessionPath.trim().length === 0) {
58
+ return null;
59
+ }
60
+
61
+ try {
62
+ const content = await readFile(sessionPath, "utf8");
63
+ return extractResponsibleAgentNameFromContent(content);
64
+ } catch {
65
+ return null;
66
+ }
67
+ }
68
+
69
+ export function extractPersistedActiveAgentNameFromEntries(
70
+ entries: readonly unknown[],
71
+ ): string | null | undefined {
72
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
73
+ const entry = toRecord(entries[index]);
74
+ if (!entry || entry.type !== "custom" || entry.customType !== "active_agent") {
75
+ continue;
76
+ }
77
+
78
+ const data = toRecord(entry.data);
79
+ const normalizedAgentName = normalizeAgentName(data?.name);
80
+ if (normalizedAgentName) {
81
+ return normalizedAgentName;
82
+ }
83
+
84
+ if (data?.name === null) {
85
+ return null;
86
+ }
87
+
88
+ return null;
89
+ }
90
+
91
+ return undefined;
92
+ }
93
+
94
+ export async function enrichSessionWithResponsibleAgent(
95
+ session: SessionInfo,
96
+ ): Promise<SessionCleanupSession> {
97
+ const responsibleAgentName = await resolveResponsibleAgentName(session.path);
98
+
99
+ return {
100
+ ...session,
101
+ responsibleAgentName,
102
+ };
103
+ }