( currentDraft: string, input: SetLocalMemoryEntryStatusInput, )
| 424 | } |
| 425 | |
| 426 | export function setLocalMemoryEntryStatusDraft( |
| 427 | currentDraft: string, |
| 428 | input: SetLocalMemoryEntryStatusInput, |
| 429 | ): SetLocalMemoryEntryStatusResult { |
| 430 | const id = input.id.trim(); |
| 431 | if (!id || (input.status !== 'active' && input.status !== 'archived')) { |
| 432 | return { ok: false, reason: 'invalid_id' }; |
| 433 | } |
| 434 | |
| 435 | const section = findLocalMemoryEntrySection(currentDraft, id); |
| 436 | if (!section) return { ok: false, reason: 'not_found' }; |
| 437 | |
| 438 | const now = |
| 439 | Number.isFinite(input.now) && input.now !== undefined |
| 440 | ? Math.max(0, Math.floor(input.now)) |
| 441 | : Date.now(); |
| 442 | const lines = currentDraft.split(/\r?\n/); |
| 443 | const meta = { |
| 444 | ...(section.meta ?? {}), |
| 445 | id: section.id, |
| 446 | status: input.status, |
| 447 | updatedAt: String(now), |
| 448 | ...(input.status === 'archived' && input.recordLifecycleMetadata |
| 449 | ? { archivedAt: String(now) } |
| 450 | : {}), |
| 451 | ...(input.status === 'archived' && input.recordLifecycleMetadata && input.archiveReason |
| 452 | ? { archiveReason: normalizeMetaValue(input.archiveReason) } |
| 453 | : {}), |
| 454 | }; |
| 455 | const metaLine = `<!-- maka-memory: ${serializeMetaComment(meta)} -->`; |
| 456 | |
| 457 | if (section.metaLineIndex !== undefined) { |
| 458 | lines[section.metaLineIndex] = metaLine; |
| 459 | } else { |
| 460 | lines.splice(section.headingLineIndex + 1, 0, metaLine); |
| 461 | } |
| 462 | |
| 463 | const draft = lines.join('\n'); |
| 464 | if (new TextEncoder().encode(draft).byteLength > LOCAL_MEMORY_MAX_BYTES) { |
| 465 | return { ok: false, reason: 'oversize' }; |
| 466 | } |
| 467 | return { ok: true, draft }; |
| 468 | } |
| 469 | |
| 470 | export function approveLocalMemoryProposalDraft( |
| 471 | memoryDraft: string, |
no test coverage detected