Add ez-assistant and kerberos service folders

This commit is contained in:
kelin
2026-02-11 14:56:03 -05:00
parent e4e8ae1b87
commit 9ccfb36923
4471 changed files with 746463 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
type EnvelopeTimestampZone = string;
function formatUtcTimestamp(date: Date): string {
const yyyy = String(date.getUTCFullYear()).padStart(4, "0");
const mm = String(date.getUTCMonth() + 1).padStart(2, "0");
const dd = String(date.getUTCDate()).padStart(2, "0");
const hh = String(date.getUTCHours()).padStart(2, "0");
const min = String(date.getUTCMinutes()).padStart(2, "0");
return `${yyyy}-${mm}-${dd}T${hh}:${min}Z`;
}
function formatZonedTimestamp(date: Date, timeZone?: string): string {
const parts = new Intl.DateTimeFormat("en-US", {
timeZone,
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
hourCycle: "h23",
timeZoneName: "short",
}).formatToParts(date);
const pick = (type: string) => parts.find((part) => part.type === type)?.value;
const yyyy = pick("year");
const mm = pick("month");
const dd = pick("day");
const hh = pick("hour");
const min = pick("minute");
const tz = [...parts]
.reverse()
.find((part) => part.type === "timeZoneName")
?.value?.trim();
if (!yyyy || !mm || !dd || !hh || !min) {
throw new Error("Missing date parts for envelope timestamp formatting.");
}
return `${yyyy}-${mm}-${dd} ${hh}:${min}${tz ? ` ${tz}` : ""}`;
}
export function formatEnvelopeTimestamp(date: Date, zone: EnvelopeTimestampZone = "utc"): string {
const normalized = zone.trim().toLowerCase();
if (normalized === "utc" || normalized === "gmt") return formatUtcTimestamp(date);
if (normalized === "local" || normalized === "host") return formatZonedTimestamp(date);
return formatZonedTimestamp(date, zone);
}
export function formatLocalEnvelopeTimestamp(date: Date): string {
return formatEnvelopeTimestamp(date, "local");
}
export function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

View File

@@ -0,0 +1,20 @@
import { expect } from "vitest";
import { normalizeChatType } from "../../src/channels/chat-type.js";
import { resolveConversationLabel } from "../../src/channels/conversation-label.js";
import { validateSenderIdentity } from "../../src/channels/sender-identity.js";
import type { MsgContext } from "../../src/auto-reply/templating.js";
export function expectInboundContextContract(ctx: MsgContext) {
expect(validateSenderIdentity(ctx)).toEqual([]);
expect(ctx.Body).toBeTypeOf("string");
expect(ctx.BodyForAgent).toBeTypeOf("string");
expect(ctx.BodyForCommands).toBeTypeOf("string");
const chatType = normalizeChatType(ctx.ChatType);
if (chatType && chatType !== "direct") {
const label = ctx.ConversationLabel?.trim() || resolveConversationLabel(ctx);
expect(label).toBeTruthy();
}
}

View File

@@ -0,0 +1,31 @@
function stripAnsi(input: string): string {
let out = "";
for (let i = 0; i < input.length; i++) {
const code = input.charCodeAt(i);
if (code !== 27) {
out += input[i];
continue;
}
const next = input[i + 1];
if (next !== "[") continue;
i += 1;
while (i + 1 < input.length) {
i += 1;
const c = input[i];
if (!c) break;
const isLetter = (c >= "A" && c <= "Z") || (c >= "a" && c <= "z") || c === "~";
if (isLetter) break;
}
}
return out;
}
export function normalizeTestText(input: string): string {
return stripAnsi(input)
.replaceAll("\r\n", "\n")
.replaceAll("…", "...")
.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "?")
.replace(/[\uD800-\uDFFF]/g, "?");
}

View File

@@ -0,0 +1,16 @@
import path from "node:path";
export function isPathWithinBase(base: string, target: string): boolean {
if (process.platform === "win32") {
const normalizedBase = path.win32.normalize(path.win32.resolve(base));
const normalizedTarget = path.win32.normalize(path.win32.resolve(target));
const rel = path.win32.relative(normalizedBase.toLowerCase(), normalizedTarget.toLowerCase());
return rel === "" || (!rel.startsWith("..") && !path.win32.isAbsolute(rel));
}
const normalizedBase = path.resolve(base);
const normalizedTarget = path.resolve(target);
const rel = path.relative(normalizedBase, normalizedTarget);
return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
}

View File

@@ -0,0 +1,25 @@
export type PollOptions = {
timeoutMs?: number;
intervalMs?: number;
};
function sleep(ms: number) {
return new Promise<void>((resolve) => setTimeout(resolve, ms));
}
export async function pollUntil<T>(
fn: () => Promise<T | null | undefined>,
opts: PollOptions = {},
): Promise<T | undefined> {
const timeoutMs = opts.timeoutMs ?? 2000;
const intervalMs = opts.intervalMs ?? 25;
const start = Date.now();
while (Date.now() - start < timeoutMs) {
const value = await fn();
if (value !== null && value !== undefined) return value;
await sleep(intervalMs);
}
return undefined;
}

View File

@@ -0,0 +1,102 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
type EnvValue = string | undefined | ((home: string) => string | undefined);
type EnvSnapshot = {
home: string | undefined;
userProfile: string | undefined;
homeDrive: string | undefined;
homePath: string | undefined;
stateDir: string | undefined;
};
function snapshotEnv(): EnvSnapshot {
return {
home: process.env.HOME,
userProfile: process.env.USERPROFILE,
homeDrive: process.env.HOMEDRIVE,
homePath: process.env.HOMEPATH,
stateDir: process.env.CLAWDBOT_STATE_DIR,
};
}
function restoreEnv(snapshot: EnvSnapshot) {
const restoreKey = (key: string, value: string | undefined) => {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
};
restoreKey("HOME", snapshot.home);
restoreKey("USERPROFILE", snapshot.userProfile);
restoreKey("HOMEDRIVE", snapshot.homeDrive);
restoreKey("HOMEPATH", snapshot.homePath);
restoreKey("CLAWDBOT_STATE_DIR", snapshot.stateDir);
}
function snapshotExtraEnv(keys: string[]): Record<string, string | undefined> {
const snapshot: Record<string, string | undefined> = {};
for (const key of keys) snapshot[key] = process.env[key];
return snapshot;
}
function restoreExtraEnv(snapshot: Record<string, string | undefined>) {
for (const [key, value] of Object.entries(snapshot)) {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
}
function setTempHome(base: string) {
process.env.HOME = base;
process.env.USERPROFILE = base;
process.env.CLAWDBOT_STATE_DIR = path.join(base, ".clawdbot");
if (process.platform !== "win32") return;
const match = base.match(/^([A-Za-z]:)(.*)$/);
if (!match) return;
process.env.HOMEDRIVE = match[1];
process.env.HOMEPATH = match[2] || "\\";
}
export async function withTempHome<T>(
fn: (home: string) => Promise<T>,
opts: { env?: Record<string, EnvValue>; prefix?: string } = {},
): Promise<T> {
const base = await fs.mkdtemp(path.join(os.tmpdir(), opts.prefix ?? "moltbot-test-home-"));
const snapshot = snapshotEnv();
const envKeys = Object.keys(opts.env ?? {});
for (const key of envKeys) {
if (key === "HOME" || key === "USERPROFILE" || key === "HOMEDRIVE" || key === "HOMEPATH") {
throw new Error(`withTempHome: use built-in home env (got ${key})`);
}
}
const envSnapshot = snapshotExtraEnv(envKeys);
setTempHome(base);
await fs.mkdir(path.join(base, ".clawdbot", "agents", "main", "sessions"), { recursive: true });
if (opts.env) {
for (const [key, raw] of Object.entries(opts.env)) {
const value = typeof raw === "function" ? raw(base) : raw;
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
}
try {
return await fn(base);
} finally {
restoreExtraEnv(envSnapshot);
restoreEnv(snapshot);
try {
await fs.rm(base, {
recursive: true,
force: true,
maxRetries: 10,
retryDelay: 50,
});
} catch {
// ignore cleanup failures in tests
}
}
}