Makelore 2.0 initial clean snapshot
This commit is contained in:
47
src/lib/project-agent-session.ts
Normal file
47
src/lib/project-agent-session.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import type { OpencodeSession } from '@/types/opencode';
|
||||
|
||||
function record(value: unknown): Record<string, unknown> | null {
|
||||
return value !== null && typeof value === 'object'
|
||||
? value as Record<string, unknown>
|
||||
: null;
|
||||
}
|
||||
|
||||
function timestamp(value: unknown): number | null {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
||||
if (typeof value !== 'string') return null;
|
||||
const parsed = Date.parse(value);
|
||||
return Number.isFinite(parsed) ? parsed : null;
|
||||
}
|
||||
|
||||
function hasPositiveNumber(value: unknown): boolean {
|
||||
if (typeof value === 'number') return Number.isFinite(value) && value > 0;
|
||||
const item = record(value);
|
||||
return item ? Object.values(item).some(hasPositiveNumber) : false;
|
||||
}
|
||||
|
||||
function hasMeaningfulSummary(value: unknown): boolean {
|
||||
if (typeof value === 'string') return value.trim().length > 0;
|
||||
if (typeof value === 'number') return Number.isFinite(value) && value > 0;
|
||||
if (typeof value === 'boolean') return value;
|
||||
if (Array.isArray(value)) return value.length > 0;
|
||||
const item = record(value);
|
||||
return item ? Object.values(item).some(hasMeaningfulSummary) : false;
|
||||
}
|
||||
|
||||
function hasConversationActivity(session: OpencodeSession): boolean {
|
||||
const time = record(session.time);
|
||||
const created = timestamp(time?.created ?? session.createdAt);
|
||||
const updated = timestamp(time?.updated ?? session.updatedAt);
|
||||
return Boolean(created !== null && updated !== null && updated > created)
|
||||
|| hasPositiveNumber(session.cost)
|
||||
|| hasPositiveNumber(session.tokens)
|
||||
|| hasMeaningfulSummary(session.summary);
|
||||
}
|
||||
|
||||
export function findPrimaryProjectAgentSession(
|
||||
sessions: OpencodeSession[],
|
||||
agentId: string,
|
||||
): OpencodeSession | undefined {
|
||||
const matches = sessions.filter((session) => session.agent === agentId);
|
||||
return matches.find(hasConversationActivity) ?? matches[0];
|
||||
}
|
||||
Reference in New Issue
Block a user