opencode-auto-rename 1.2.0 → 1.3.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/index.js +21 -26
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Auto-Rename Plugin
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* Automatically names conversations and keeps titles updated as they evolve.
|
|
5
|
+
*
|
|
6
|
+
* - Renames immediately after the first assistant response.
|
|
7
|
+
* - Re-renames every 3 assistant messages to keep the title relevant.
|
|
8
8
|
*/
|
|
9
|
-
const
|
|
9
|
+
const RENAME_EVERY_N = 3;
|
|
10
10
|
export const AutoRenamePlugin = async ({ client }) => {
|
|
11
|
-
// Track the
|
|
12
|
-
const
|
|
13
|
-
const lastRenamed = new Map();
|
|
11
|
+
// Track the assistant message count at which we last renamed each session
|
|
12
|
+
const lastRenamedAtCount = new Map();
|
|
14
13
|
return {
|
|
15
14
|
event: async ({ event }) => {
|
|
16
15
|
if (event.type !== "session.idle")
|
|
@@ -18,32 +17,28 @@ export const AutoRenamePlugin = async ({ client }) => {
|
|
|
18
17
|
const sessionId = event.properties.sessionID;
|
|
19
18
|
if (!sessionId)
|
|
20
19
|
return;
|
|
21
|
-
const now = Date.now();
|
|
22
|
-
// Record when we first see a session and skip it —
|
|
23
|
-
// OpenCode already handles the initial naming.
|
|
24
|
-
if (!firstSeen.has(sessionId)) {
|
|
25
|
-
firstSeen.set(sessionId, now);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
// Only rename if enough time has passed since we first saw it
|
|
29
|
-
// or since the last rename
|
|
30
|
-
const lastRenamedAt = lastRenamed.get(sessionId);
|
|
31
|
-
const baseline = lastRenamedAt ?? firstSeen.get(sessionId);
|
|
32
|
-
if (now - baseline < RENAME_INTERVAL_MS)
|
|
33
|
-
return;
|
|
34
20
|
try {
|
|
35
|
-
//
|
|
21
|
+
// Fetch messages to count assistant responses
|
|
36
22
|
const messages = await client.session.messages({
|
|
37
23
|
path: { id: sessionId },
|
|
38
24
|
});
|
|
39
|
-
const
|
|
40
|
-
|
|
25
|
+
const assistantCount = messages.data?.filter((m) => m.info.role === "assistant").length ?? 0;
|
|
26
|
+
// Need at least one assistant message
|
|
27
|
+
if (assistantCount === 0)
|
|
28
|
+
return;
|
|
29
|
+
const lastCount = lastRenamedAtCount.get(sessionId) ?? 0;
|
|
30
|
+
// Rename on the first assistant message (count=1, lastCount=0)
|
|
31
|
+
// then every RENAME_EVERY_N new assistant messages after that
|
|
32
|
+
const isFirstMessage = lastCount === 0;
|
|
33
|
+
const newMessagesSinceRename = assistantCount - lastCount;
|
|
34
|
+
const shouldRename = isFirstMessage || newMessagesSinceRename >= RENAME_EVERY_N;
|
|
35
|
+
if (!shouldRename)
|
|
41
36
|
return;
|
|
42
|
-
// Use the built-in summarize endpoint to generate a title
|
|
37
|
+
// Use the built-in summarize endpoint to generate a title
|
|
43
38
|
await client.session.summarize({
|
|
44
39
|
path: { id: sessionId },
|
|
45
40
|
});
|
|
46
|
-
|
|
41
|
+
lastRenamedAtCount.set(sessionId, assistantCount);
|
|
47
42
|
}
|
|
48
43
|
catch (err) {
|
|
49
44
|
// Log errors but don't crash the plugin
|