blob: e28dd61e8dbf1302eacc8b2aeb6d94c2460a20bc [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
export type TextStyleSpec = {
fontSize: number;
lineHeight: number;
fontWeight?: '400' | '500' | '600' | '700';
letterSpacing?: number;
};
export type ServerUiTheme<Element = unknown> = {
colors: {
action: string;
onAction: string;
actionHover: string;
accent: string;
accentText: string;
card: string;
field: string;
raised: string;
pressed: string;
line: string;
text: string;
secondary: string;
muted: string;
danger: string;
warning: string;
};
radius(n: number): number;
lineWidth: number;
type: {
title: TextStyleSpec;
body: TextStyleSpec;
secondary: TextStyleSpec;
caption: TextStyleSpec;
section: TextStyleSpec;
};
chevron(open: boolean): Element;
};
export type ServerEnrollment = {
serverUrl: string;
inviteToken: string;
certSha256?: string;
deviceName: string;
};
export type ServerInviteRole = 'host' | 'device' | 'any';
export type ServerInvite = Omit<ServerEnrollment, 'deviceName'> & {
role?: ServerInviteRole;
};
const invitePrefix = 'maka://server/v1?';
export function parseServerInviteLink(raw: string): ServerInvite {
const link = raw.trim();
if (!link.startsWith(invitePrefix)) {
throw new Error('Invalid Maka Server invitation link.');
}
const params = new URLSearchParams(link.slice(invitePrefix.length));
const serverUrl = params.get('url')?.trim() ?? '';
const inviteToken = params.get('invite')?.trim() ?? '';
const certSha256 = params.get('pin')?.trim() || undefined;
const rawRole = params.get('role')?.trim().toLowerCase();
const role: ServerInviteRole | undefined =
rawRole === 'host' || rawRole === 'device' || rawRole === 'any' ? rawRole : undefined;
if (!serverUrl || !inviteToken) {
throw new Error('Invalid Maka Server invitation link.');
}
return {
serverUrl,
inviteToken,
...(certSha256 ? { certSha256 } : {}),
...(role ? { role } : {}),
};
}
export function formatCompactTime(timestampMs: number, nowMs = Date.now()): string {
const date = new Date(timestampMs);
const now = new Date(nowMs);
const sameDay =
date.getFullYear() === now.getFullYear() &&
date.getMonth() === now.getMonth() &&
date.getDate() === now.getDate();
if (sameDay) {
return date.toLocaleTimeString(undefined, {
hour: '2-digit',
minute: '2-digit',
hour12: false,
});
}
return date.toLocaleString(undefined, {
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
hour12: false,
});
}
export function snippetRuns(snippet: string): { text: string; matched: boolean }[] {
const runs: { text: string; matched: boolean }[] = [];
const pattern = /\[([^\]]+)\]/gu;
let last = 0;
for (const match of snippet.matchAll(pattern)) {
const start = match.index ?? 0;
if (start > last) runs.push({ text: snippet.slice(last, start), matched: false });
runs.push({ text: match[1]!, matched: true });
last = start + match[0].length;
}
if (last < snippet.length) runs.push({ text: snippet.slice(last), matched: false });
return runs;
}
export type MirrorRequest =
| { kind: 'projects' }
| { kind: 'search'; query: string }
| { kind: 'sessions'; rootId?: string; repoIdentity?: string; cursor?: unknown }
| { kind: 'transcript'; rootId: string; sessionId: string; cursor?: unknown };
export type MirrorReply =
| { kind: 'ready'; value: unknown }
| { kind: 'changed' }
| { kind: 'too_large' };
export type MirrorReader = (request: MirrorRequest) => Promise<MirrorReply>;
export type SessionRef = { rootId: string; sessionId: string; kind: 'agent' | 'room' };
export type MirroredSession = SessionRef & {
title: string;
projectId: string | null;
};
export type MirrorMessage = {
messageId: string;
role: string;
bodyText: string;
createdAtMs: number | null;
};
export type MirrorApproval = { requestId: string; summary: string; createdAtMs: number };
export type MirrorTranscript = MirroredSession & {
messages: MirrorMessage[];
pendingApprovals: MirrorApproval[];
nextCursor: unknown | null;
lastSyncedAtMs: number;
};
type MirrorCoverage = { rootId: string; lastSyncedAtMs: number | null };
export type MirrorSessions = {
coverage: MirrorCoverage[];
sessions: MirroredSession[];
nextCursor: unknown | null;
};
export type MirrorProjects = {
projects: {
checkouts: {
rootId: string;
projectId: string;
name: string;
repoIdentity: string | null;
}[];
}[];
};
export type MirrorSearch = {
hits: (SessionRef & { title: string; snippet: string })[];
};
export async function mirrorPage(
read: MirrorReader,
request: MirrorRequest,
): Promise<{
reply: MirrorReply;
restarted: boolean;
}> {
const reply = await read(request);
if (reply.kind === 'changed' && 'cursor' in request && request.cursor != null) {
return { reply: await read({ ...request, cursor: undefined }), restarted: true };
}
return { reply, restarted: false };
}