wendkeep 0.37.0 → 0.38.1
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 +21 -0
- package/README.md +2 -0
- package/README.pt-BR.md +2 -0
- package/bin/wendkeep.mjs +7 -1
- package/hooks/brain-inject.mjs +87 -80
- package/hooks/change-context.mjs +8 -4
- package/hooks/decision-capture.mjs +4 -2
- package/hooks/obsidian-common.mjs +701 -619
- package/hooks/plan-capture.mjs +8 -7
- package/hooks/pricing.json +53 -53
- package/hooks/session-ensure.mjs +413 -392
- package/hooks/session-identity.mjs +97 -0
- package/hooks/session-observability.mjs +16 -2
- package/hooks/session-start.mjs +340 -317
- package/hooks/session-stop.mjs +1153 -1141
- package/hooks/subagent-stop.mjs +7 -5
- package/hooks/task-log.mjs +5 -4
- package/hooks/token-usage.mjs +941 -941
- package/hooks/vault-health.mjs +198 -195
- package/package.json +1 -1
- package/src/change.mjs +14 -2
- package/src/rebuild-costs.mjs +5 -5
- package/src/session.mjs +37 -0
package/hooks/session-ensure.mjs
CHANGED
|
@@ -1,392 +1,413 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { existsSync, readFileSync, renameSync, statSync, writeFileSync } from 'fs';
|
|
3
|
-
import { basename, dirname, join } from 'path';
|
|
4
|
-
import {
|
|
5
|
-
controlPath,
|
|
6
|
-
ensureDir,
|
|
7
|
-
formatDate,
|
|
8
|
-
formatHourMinute,
|
|
9
|
-
formatLocalIso,
|
|
10
|
-
formatTime,
|
|
11
|
-
getVaultBase,
|
|
12
|
-
warnIfDefaultVault,
|
|
13
|
-
debugLog,
|
|
14
|
-
readControl,
|
|
15
|
-
readHookInput,
|
|
16
|
-
readSessionRegistry,
|
|
17
|
-
sessionFileName,
|
|
18
|
-
sessionFolderRel,
|
|
19
|
-
sessionSummaryFromInput,
|
|
20
|
-
isUsableSummary,
|
|
21
|
-
providerMeta,
|
|
22
|
-
shouldReuseActiveSession,
|
|
23
|
-
isPlaceholderSessionFile,
|
|
24
|
-
toVaultRelative,
|
|
25
|
-
uniquePath,
|
|
26
|
-
upsertSessionRegistry,
|
|
27
|
-
VAULT_COMPLEMENT_RULES,
|
|
28
|
-
wikilinkFromRel,
|
|
29
|
-
writeControl,
|
|
30
|
-
writeHookOutput,
|
|
31
|
-
yamlQuote,
|
|
32
|
-
} from './obsidian-common.mjs';
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
-
|
|
59
|
-
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
- **
|
|
70
|
-
- **
|
|
71
|
-
- **
|
|
72
|
-
- **
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
`
|
|
137
|
-
`
|
|
138
|
-
|
|
139
|
-
'
|
|
140
|
-
'
|
|
141
|
-
'
|
|
142
|
-
'',
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
next =
|
|
153
|
-
next = next.replace(/^
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
return content.replace(/^
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
next = next.replace(
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
const
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
const
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, readFileSync, renameSync, statSync, writeFileSync } from 'fs';
|
|
3
|
+
import { basename, dirname, join } from 'path';
|
|
4
|
+
import {
|
|
5
|
+
controlPath,
|
|
6
|
+
ensureDir,
|
|
7
|
+
formatDate,
|
|
8
|
+
formatHourMinute,
|
|
9
|
+
formatLocalIso,
|
|
10
|
+
formatTime,
|
|
11
|
+
getVaultBase,
|
|
12
|
+
warnIfDefaultVault,
|
|
13
|
+
debugLog,
|
|
14
|
+
readControl,
|
|
15
|
+
readHookInput,
|
|
16
|
+
readSessionRegistry,
|
|
17
|
+
sessionFileName,
|
|
18
|
+
sessionFolderRel,
|
|
19
|
+
sessionSummaryFromInput,
|
|
20
|
+
isUsableSummary,
|
|
21
|
+
providerMeta,
|
|
22
|
+
shouldReuseActiveSession,
|
|
23
|
+
isPlaceholderSessionFile,
|
|
24
|
+
toVaultRelative,
|
|
25
|
+
uniquePath,
|
|
26
|
+
upsertSessionRegistry,
|
|
27
|
+
VAULT_COMPLEMENT_RULES,
|
|
28
|
+
wikilinkFromRel,
|
|
29
|
+
writeControl,
|
|
30
|
+
writeHookOutput,
|
|
31
|
+
yamlQuote,
|
|
32
|
+
} from './obsidian-common.mjs';
|
|
33
|
+
import { resolveSessionIdentity } from './session-identity.mjs';
|
|
34
|
+
|
|
35
|
+
function sessionIdFromInput(input) {
|
|
36
|
+
return input.session_id || input.sessionId || input.codex_session_id || '';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function buildSessionContent({ relPath, now, summary = 'session', sessionId = '', reason = 'Sessão criada automaticamente pelo hook UserPromptSubmit.' }) {
|
|
40
|
+
const date = formatDate(now);
|
|
41
|
+
const startedAt = formatLocalIso(now);
|
|
42
|
+
const titleTime = formatTime(now).slice(0, 5);
|
|
43
|
+
const objective = summary === 'session' ? 'Preencher durante a sessão.' : summary;
|
|
44
|
+
const provider = providerMeta();
|
|
45
|
+
|
|
46
|
+
return `---
|
|
47
|
+
type: session
|
|
48
|
+
date: ${date}
|
|
49
|
+
started_at: ${startedAt}
|
|
50
|
+
ended_at:
|
|
51
|
+
provider: ${provider.id}
|
|
52
|
+
session_id: ${sessionId ? yamlQuote(sessionId) : ''}
|
|
53
|
+
status: active
|
|
54
|
+
summary: ${yamlQuote(summary)}
|
|
55
|
+
cssclasses:
|
|
56
|
+
- topic-session
|
|
57
|
+
tags:
|
|
58
|
+
- sessao
|
|
59
|
+
- ${provider.tag}
|
|
60
|
+
- llm
|
|
61
|
+
source: ${provider.source}
|
|
62
|
+
related:
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
# ${titleTime} - ${summary}
|
|
66
|
+
|
|
67
|
+
## Metadados
|
|
68
|
+
|
|
69
|
+
- **Provider:** ${provider.label}
|
|
70
|
+
- **Início:** ${startedAt}
|
|
71
|
+
- **Fim:**
|
|
72
|
+
- **Status:** active
|
|
73
|
+
- **Arquivo:** \`${relPath}\`
|
|
74
|
+
|
|
75
|
+
## Objetivo da sessão
|
|
76
|
+
|
|
77
|
+
> ${objective}
|
|
78
|
+
|
|
79
|
+
## Resumo vivo
|
|
80
|
+
|
|
81
|
+
> Esta seção pode ser atualizada ao longo da sessão, mas o histórico de iterações deve ser preservado.
|
|
82
|
+
|
|
83
|
+
## Iterações
|
|
84
|
+
|
|
85
|
+
### ${titleTime} - Início da sessão
|
|
86
|
+
|
|
87
|
+
${reason}
|
|
88
|
+
|
|
89
|
+
## Decisões geradas nesta sessão
|
|
90
|
+
|
|
91
|
+
Nenhuma decisão registrada ainda.
|
|
92
|
+
|
|
93
|
+
## Bugs gerados nesta sessão
|
|
94
|
+
|
|
95
|
+
Nenhum bug registrado ainda.
|
|
96
|
+
|
|
97
|
+
## Aprendizados gerados nesta sessão
|
|
98
|
+
|
|
99
|
+
Nenhum aprendizado registrado ainda.
|
|
100
|
+
|
|
101
|
+
## Arquivos consultados
|
|
102
|
+
|
|
103
|
+
Nenhum arquivo registrado ainda.
|
|
104
|
+
|
|
105
|
+
## Arquivos criados ou alterados
|
|
106
|
+
|
|
107
|
+
Nenhum arquivo registrado ainda.
|
|
108
|
+
|
|
109
|
+
## Pendências
|
|
110
|
+
|
|
111
|
+
Nenhuma pendência identificada automaticamente.
|
|
112
|
+
|
|
113
|
+
## Encerramento
|
|
114
|
+
|
|
115
|
+
Sessão ainda em andamento.
|
|
116
|
+
`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function allocateSessionPath(vaultBase, now, summary = 'session') {
|
|
120
|
+
const folderRel = sessionFolderRel(now, vaultBase);
|
|
121
|
+
const folderAbs = join(vaultBase, folderRel);
|
|
122
|
+
ensureDir(folderAbs);
|
|
123
|
+
|
|
124
|
+
const baseName = sessionFileName(now, summary);
|
|
125
|
+
const filePath = uniquePath(join(folderAbs, baseName));
|
|
126
|
+
return {
|
|
127
|
+
absPath: filePath,
|
|
128
|
+
relPath: toVaultRelative(vaultBase, filePath),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function buildAdditionalContext({ relPath, startedAt, vaultBase }) {
|
|
133
|
+
const controlRel = toVaultRelative(vaultBase, controlPath(vaultBase));
|
|
134
|
+
return [
|
|
135
|
+
'<obsidian_session>',
|
|
136
|
+
`Sessão Obsidian ativa: ${relPath}`,
|
|
137
|
+
`Controle atualizado: ${controlRel}`,
|
|
138
|
+
`Início: ${startedAt}`,
|
|
139
|
+
'',
|
|
140
|
+
'Use a sessão ativa como log desta conversa.',
|
|
141
|
+
'Antes de registrar informações, leia `.brain/CURRENT_SESSION.md` no vault.',
|
|
142
|
+
'Nunca sobrescreva o histórico anterior. Registre cada iteração como `### HH:MM - Título` DENTRO da seção `## Iterações` (logo antes de `## Decisões geradas nesta sessão`). Cada iteração deve trazer contexto conversado suficiente: pedido do usuário, investigação/ações, evidências relevantes e estado final. NUNCA escreva iterações após `## Encerramento`.',
|
|
143
|
+
'',
|
|
144
|
+
...VAULT_COMPLEMENT_RULES,
|
|
145
|
+
'Não registre chaves, tokens, senhas ou segredos; substitua por `[REDACTED_SECRET]`.',
|
|
146
|
+
`Wikilink da sessão: ${wikilinkFromRel(relPath)}`,
|
|
147
|
+
'</obsidian_session>',
|
|
148
|
+
].join('\n');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function updateSessionFrontmatter(content) {
|
|
152
|
+
let next = content;
|
|
153
|
+
next = next.replace(/^status:.*$/m, 'status: active');
|
|
154
|
+
next = next.replace(/^ended_at:.*$/m, 'ended_at:');
|
|
155
|
+
return next;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function upsertSummaryFrontmatter(content, summary) {
|
|
159
|
+
if (/^summary:/m.test(content)) return content.replace(/^summary:.*$/m, `summary: ${yamlQuote(summary)}`);
|
|
160
|
+
return content.replace(/^status:.*$/m, (line) => `${line}\nsummary: ${yamlQuote(summary)}`);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function updateSessionDescription(content, { relPath, summary, startedAt }) {
|
|
164
|
+
const startedDate = startedAt ? new Date(startedAt) : new Date();
|
|
165
|
+
const titleTime = Number.isFinite(startedDate.getTime()) ? formatTime(startedDate).slice(0, 5) : '';
|
|
166
|
+
let next = upsertSummaryFrontmatter(content, summary);
|
|
167
|
+
if (titleTime) {
|
|
168
|
+
next = next.replace(/^# .+$/m, `# ${titleTime} - ${summary}`);
|
|
169
|
+
}
|
|
170
|
+
next = next.replace(/- \*\*Arquivo:\*\* `[^`]+`/m, `- **Arquivo:** \`${relPath}\``);
|
|
171
|
+
next = next.replace(
|
|
172
|
+
/(## Objetivo da sessão\n\n)>[^\n]*/m,
|
|
173
|
+
`$1> ${summary === 'session' ? 'Preencher durante a sessão.' : summary}`,
|
|
174
|
+
);
|
|
175
|
+
return next;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function maybeRetitleSession({ vaultBase, relPath, startedAt, input }) {
|
|
179
|
+
const summary = sessionSummaryFromInput(input);
|
|
180
|
+
if (!isUsableSummary(summary)) return { relPath, summary, changed: false };
|
|
181
|
+
|
|
182
|
+
const currentPath = join(vaultBase, relPath);
|
|
183
|
+
if (!existsSync(currentPath)) return { relPath, summary, changed: false };
|
|
184
|
+
|
|
185
|
+
let nextRelPath = relPath;
|
|
186
|
+
if (isPlaceholderSessionFile(relPath)) {
|
|
187
|
+
const startedDate = startedAt ? new Date(startedAt) : new Date();
|
|
188
|
+
const baseDate = Number.isFinite(startedDate.getTime()) ? startedDate : new Date();
|
|
189
|
+
const nextPath = uniquePath(join(dirname(currentPath), sessionFileName(baseDate, summary)));
|
|
190
|
+
if (nextPath !== currentPath) {
|
|
191
|
+
renameSync(currentPath, nextPath);
|
|
192
|
+
nextRelPath = toVaultRelative(vaultBase, nextPath);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const sessionPath = join(vaultBase, nextRelPath);
|
|
197
|
+
const content = readFileSync(sessionPath, 'utf-8');
|
|
198
|
+
const updated = updateSessionDescription(content, { relPath: nextRelPath, summary, startedAt });
|
|
199
|
+
if (updated !== content) writeFileSync(sessionPath, updated, 'utf-8');
|
|
200
|
+
|
|
201
|
+
return { relPath: nextRelPath, summary, changed: nextRelPath !== relPath || updated !== content };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function stripClosingSection(content) {
|
|
205
|
+
const marker = '\n## Encerramento';
|
|
206
|
+
const index = content.indexOf(marker);
|
|
207
|
+
if (index === -1) return content;
|
|
208
|
+
return `${content.slice(0, index).trimEnd()}\n`;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function reopenSessionFile(sessionPath) {
|
|
212
|
+
const content = readFileSync(sessionPath, 'utf-8');
|
|
213
|
+
const reopened = stripClosingSection(updateSessionFrontmatter(content));
|
|
214
|
+
writeFileSync(sessionPath, reopened, 'utf-8');
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function findSessionForInput(vaultBase, input, control) {
|
|
218
|
+
const sessionId = sessionIdFromInput(input);
|
|
219
|
+
const registry = readSessionRegistry(vaultBase);
|
|
220
|
+
const registered = sessionId ? registry.sessions[sessionId] : null;
|
|
221
|
+
|
|
222
|
+
if (registered?.session_file) {
|
|
223
|
+
return {
|
|
224
|
+
sessionId,
|
|
225
|
+
relPath: registered.session_file,
|
|
226
|
+
startedAt: registered.started_at || control.started_at || '',
|
|
227
|
+
fromRegistry: true,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (sessionId && control.session_id === sessionId && control.last_session_file) {
|
|
232
|
+
return {
|
|
233
|
+
sessionId,
|
|
234
|
+
relPath: control.last_session_file,
|
|
235
|
+
startedAt: control.started_at || '',
|
|
236
|
+
fromRegistry: false,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return { sessionId, relPath: '', startedAt: '', fromRegistry: false };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function activateExistingSession({ vaultBase, relPath, startedAt, sessionId, input, now, identity }) {
|
|
244
|
+
const sessionPath = join(vaultBase, relPath);
|
|
245
|
+
if (!existsSync(sessionPath)) return false;
|
|
246
|
+
|
|
247
|
+
reopenSessionFile(sessionPath);
|
|
248
|
+
const nextStartedAt = startedAt || formatLocalIso(now);
|
|
249
|
+
writeControl(vaultBase, {
|
|
250
|
+
status: 'active',
|
|
251
|
+
session_file: relPath,
|
|
252
|
+
last_session_file: relPath,
|
|
253
|
+
started_at: nextStartedAt,
|
|
254
|
+
ended_at: '',
|
|
255
|
+
session_id: sessionId,
|
|
256
|
+
last_logged_turn_id: '',
|
|
257
|
+
});
|
|
258
|
+
upsertSessionRegistry(vaultBase, sessionId, {
|
|
259
|
+
session_file: relPath,
|
|
260
|
+
status: 'active',
|
|
261
|
+
started_at: nextStartedAt,
|
|
262
|
+
ended_at: '',
|
|
263
|
+
transcript_path: identity.transcriptPath,
|
|
264
|
+
transcript_id: identity.transcriptId,
|
|
265
|
+
provider: identity.provider,
|
|
266
|
+
});
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function createSession({ vaultBase, sessionId, input, now, identity }) {
|
|
271
|
+
const summary = sessionSummaryFromInput(input);
|
|
272
|
+
const { absPath, relPath } = allocateSessionPath(vaultBase, now, summary);
|
|
273
|
+
const startedAt = formatLocalIso(now);
|
|
274
|
+
writeFileSync(absPath, buildSessionContent({ relPath, now, summary, sessionId }), 'utf-8');
|
|
275
|
+
writeControl(vaultBase, {
|
|
276
|
+
status: 'active',
|
|
277
|
+
session_file: relPath,
|
|
278
|
+
last_session_file: relPath,
|
|
279
|
+
started_at: startedAt,
|
|
280
|
+
ended_at: '',
|
|
281
|
+
session_id: sessionId,
|
|
282
|
+
last_logged_turn_id: '',
|
|
283
|
+
});
|
|
284
|
+
upsertSessionRegistry(vaultBase, sessionId, {
|
|
285
|
+
session_file: relPath,
|
|
286
|
+
status: 'active',
|
|
287
|
+
started_at: startedAt,
|
|
288
|
+
ended_at: '',
|
|
289
|
+
transcript_path: identity.transcriptPath,
|
|
290
|
+
transcript_id: identity.transcriptId,
|
|
291
|
+
provider: identity.provider,
|
|
292
|
+
});
|
|
293
|
+
return { relPath, startedAt };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function outputActiveContext({ relPath, startedAt, vaultBase, message }) {
|
|
297
|
+
writeHookOutput({
|
|
298
|
+
hookSpecificOutput: {
|
|
299
|
+
hookEventName: 'UserPromptSubmit',
|
|
300
|
+
additionalContext: buildAdditionalContext({ relPath, startedAt, vaultBase }),
|
|
301
|
+
},
|
|
302
|
+
systemMessage: message,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function main() {
|
|
307
|
+
const input = readHookInput();
|
|
308
|
+
const vaultBase = getVaultBase(input);
|
|
309
|
+
warnIfDefaultVault(input);
|
|
310
|
+
const now = new Date();
|
|
311
|
+
const identity = resolveSessionIdentity(vaultBase, input, providerMeta().id);
|
|
312
|
+
if (identity.state !== 'resolved') {
|
|
313
|
+
writeHookOutput({
|
|
314
|
+
hookSpecificOutput: {
|
|
315
|
+
hookEventName: 'UserPromptSubmit',
|
|
316
|
+
additionalContext: `<obsidian_session_deferred>Memória global disponível, sem escrita vinculada: ${identity.diagnostics.join('; ')}.</obsidian_session_deferred>`,
|
|
317
|
+
},
|
|
318
|
+
systemMessage: `[wendkeep] Identidade de sessão adiada: ${identity.diagnostics.join('; ')}`,
|
|
319
|
+
});
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
const sessionId = identity.canonicalConversationId;
|
|
323
|
+
|
|
324
|
+
// Fast path: skip all writes if control file touched < 5 min ago and session matches
|
|
325
|
+
try {
|
|
326
|
+
const ctrlPath = controlPath(vaultBase);
|
|
327
|
+
const { mtimeMs } = statSync(ctrlPath);
|
|
328
|
+
if ((now.getTime() - mtimeMs) / 1000 < 300) {
|
|
329
|
+
const ctrl = readControl(vaultBase);
|
|
330
|
+
if (
|
|
331
|
+
ctrl.status === 'active' &&
|
|
332
|
+
ctrl.session_file &&
|
|
333
|
+
!isPlaceholderSessionFile(ctrl.session_file) &&
|
|
334
|
+
existsSync(join(vaultBase, ctrl.session_file)) &&
|
|
335
|
+
ctrl.session_id === sessionId
|
|
336
|
+
) {
|
|
337
|
+
writeHookOutput({});
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
} catch (err) {
|
|
342
|
+
debugLog('session-ensure fast-path skipped:', err);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const control = readControl(vaultBase);
|
|
346
|
+
|
|
347
|
+
if (control.status === 'active' && control.session_file) {
|
|
348
|
+
const activePath = join(vaultBase, control.session_file);
|
|
349
|
+
// Sem reuso-por-janela: só reaproveita a nota do control quando não há
|
|
350
|
+
// identidade pra checar (!sessionId) ou quando é a MESMA conversa. Conversa
|
|
351
|
+
// concorrente recente NÃO pode herdar a nota ativa do ponteiro global.
|
|
352
|
+
if (existsSync(activePath) && control.session_id === sessionId) {
|
|
353
|
+
const titled = maybeRetitleSession({
|
|
354
|
+
vaultBase,
|
|
355
|
+
relPath: control.session_file,
|
|
356
|
+
startedAt: control.started_at,
|
|
357
|
+
input,
|
|
358
|
+
});
|
|
359
|
+
const activeRelPath = titled.relPath;
|
|
360
|
+
if (titled.changed || control.session_file !== activeRelPath || control.session_id !== sessionId) {
|
|
361
|
+
writeControl(vaultBase, {
|
|
362
|
+
status: 'active',
|
|
363
|
+
session_file: activeRelPath,
|
|
364
|
+
last_session_file: activeRelPath,
|
|
365
|
+
started_at: control.started_at,
|
|
366
|
+
ended_at: '',
|
|
367
|
+
session_id: sessionId || control.session_id,
|
|
368
|
+
last_logged_turn_id: control.last_logged_turn_id || '',
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
upsertSessionRegistry(vaultBase, sessionId || control.session_id, {
|
|
372
|
+
session_file: activeRelPath,
|
|
373
|
+
status: 'active',
|
|
374
|
+
started_at: control.started_at,
|
|
375
|
+
ended_at: '',
|
|
376
|
+
transcript_path: identity.transcriptPath,
|
|
377
|
+
transcript_id: identity.transcriptId,
|
|
378
|
+
provider: identity.provider,
|
|
379
|
+
});
|
|
380
|
+
writeHookOutput({});
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const registered = readSessionRegistry(vaultBase).sessions?.[sessionId];
|
|
386
|
+
const resolvedTarget = registered?.session_file
|
|
387
|
+
? { sessionId, relPath: registered.session_file, startedAt: registered.started_at || '' }
|
|
388
|
+
: { sessionId, relPath: '', startedAt: '' };
|
|
389
|
+
if (resolvedTarget.relPath && activateExistingSession({ vaultBase, relPath: resolvedTarget.relPath, startedAt: resolvedTarget.startedAt, sessionId, input, now, identity })) {
|
|
390
|
+
outputActiveContext({
|
|
391
|
+
relPath: resolvedTarget.relPath,
|
|
392
|
+
startedAt: resolvedTarget.startedAt || formatLocalIso(now),
|
|
393
|
+
vaultBase,
|
|
394
|
+
message: `Sessão Obsidian reaberta em ${resolvedTarget.relPath}. ${basename(controlPath(vaultBase))} atualizado.`,
|
|
395
|
+
});
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const created = createSession({ vaultBase, sessionId, input, now, identity });
|
|
400
|
+
outputActiveContext({
|
|
401
|
+
relPath: created.relPath,
|
|
402
|
+
startedAt: created.startedAt,
|
|
403
|
+
vaultBase,
|
|
404
|
+
message: `Sessão Obsidian criada em ${created.relPath}. ${basename(controlPath(vaultBase))} atualizado.`,
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
try {
|
|
409
|
+
main();
|
|
410
|
+
} catch (error) {
|
|
411
|
+
process.stderr.write(`[wendkeep] UserPromptSubmit falhou: ${error.message}\n`);
|
|
412
|
+
writeHookOutput({});
|
|
413
|
+
}
|