mirror of
https://github.com/nightly-labs/openbot.git
synced 2026-12-03 21:28:52 +08:00
* Send daily usage counts of each server to its owner's account A server (hosted, server mode, or a local server) counts the AI tasks that start on it and the different members who use it, for each Europe/Warsaw day. It keeps the counts of the last 14 days in server-usage.json and sends each closed day to POST /v1/me/server-usage with the owner's session. A plain desktop app does not count. The account service accepts a day only from the owner of the server and only up to 15 days back. It sends server_usage_day (account generation 26) under the owner's account ID, with kind hosted or self. It does not store the counts and skips owners whose analytics answer is off. * Link analytics to the account by default outside Europe Outside the EEA, the UK, Switzerland, their territories and the European microstates, the setup screen and the web sign-in screen do not show the analytics box, and "Link analytics to my account" is on by default. The Settings switch stays and still turns it off. In Europe, and when the time zone is unknown or not tied to a place, the box shows and starts clear. A European region or a European time zone gives off, so an English (US) system in France stays off. The phone used "on" in every region with no saved choice. It now uses the same rule. The web used "on" for every signed-in browser with no saved choice. It now uses the same rule too. Saved choices and account answers are kept. * Treat an unknown location as outside Europe for the analytics default A device is in the European group only when its region or its time zone is European. When both are unknown or not tied to a place, such as UTC, analytics is linked to the account by default. A failed region read also gives on. * Count server use for each kind of client app Each client app now tells the server its kind: phone, web, desktop, watch or Chrome extension. The web, phone and extension clients add an optional "client" key to the http.request frame. The host peer copies it to the OpenBot-Client-Kind header. The desktop and the watch relay send the header directly. A released host ignores the extra key, and a client that sends no kind counts as "other". The server counts, for each kind and each day, the members who used it and the messages that they sent to agents, tasks, threads and channels. The daily server_usage_day report sends these counts with the totals. The account service accepts them in a bound and adds <kind>_members and <kind>_messages to the event. There is no device or install ID. * Fix the review findings on server usage counts - Count a resent message (same message ID) one time for its app kind. - Add report_id to server_usage_day, so that a dashboard counts each server day one time and adds the servers of an owner. - Use the time zone for the analytics default when the language region cannot be read. - Count Africa/Ceuta as a European time zone. - Say in ANALYTICS.md that a watch that sends through its phone counts as phone, and in PRIVACY.md that each Europe/ time zone counts. * Remove the exports that only their own file uses * Follow the server analytics switch and do not count typing as other - While the analytics switch of the server is off, the server usage reporter counts nothing and drops its closed days. The switch also changes it at run time, as it does for account activity. - Typing from a session whose app is not known counts the member in the day only, not in the other app kind.
157 lines
5.9 KiB
TypeScript
157 lines
5.9 KiB
TypeScript
import { mkdirSync, writeFileSync } from "node:fs";
|
|
import { IncomingMessage, ServerResponse } from "node:http";
|
|
import { Socket } from "node:net";
|
|
import { join, resolve } from "node:path";
|
|
import type { TeamMemberSummary, TeamSessionSummary } from "@openbot/contracts/ipc";
|
|
import { TEAM_API_ROUTES } from "@openbot/contracts/team-api-routes";
|
|
import { Effect } from "effect";
|
|
import type { TeamApiRequestContext } from "../src/main/team-api/request-context";
|
|
import { routeTeam, type TeamRouteDependencies } from "../src/main/team-api/route-team";
|
|
|
|
const OUT = resolve(import.meta.dirname, "../.openbot-build/team-session-end-e2e");
|
|
const ORIGIN = "https://host.test";
|
|
|
|
const checks: { name: string; passed: boolean; detail?: unknown }[] = [];
|
|
function check(name: string, passed: boolean, detail?: unknown) {
|
|
checks.push({ name, passed, ...(detail === undefined ? {} : { detail }) });
|
|
}
|
|
const report: { passed: boolean; checks: typeof checks; error?: string } = { passed: false, checks };
|
|
|
|
const OWNER: TeamMemberSummary = {
|
|
id: "member-a",
|
|
username: "owner",
|
|
email: null,
|
|
name: "Owner",
|
|
role: "owner",
|
|
createdAt: "2026-10-17T00:00:00.000Z",
|
|
disabled: false,
|
|
};
|
|
const MEMBER: TeamMemberSummary = { ...OWNER, id: "member-b", username: "member", name: "Member", role: "member" };
|
|
const SESSION_B: TeamSessionSummary = {
|
|
id: "session-b",
|
|
memberId: MEMBER.id,
|
|
username: MEMBER.username,
|
|
createdAt: "2026-10-17T00:00:00.000Z",
|
|
expiresAt: "2026-10-18T00:00:00.000Z",
|
|
};
|
|
|
|
function recorder() {
|
|
const calls: Record<string, string[]> = {};
|
|
const record = (name: string, value: string) => {
|
|
calls[name] = [...(calls[name] ?? []), value];
|
|
return Effect.void;
|
|
};
|
|
const unused = () => Effect.die(new Error("Not used by this check."));
|
|
const store: TeamRouteDependencies["store"] = {
|
|
logout: (token) => record("store.logout", token),
|
|
changePassword: unused,
|
|
resolveLogo: () => null,
|
|
listMembers: () => [OWNER, MEMBER],
|
|
updateMember: unused,
|
|
removeMember: unused,
|
|
listInvites: () => [],
|
|
revokeInvite: unused,
|
|
listSessions: () => [SESSION_B],
|
|
revokeSession: (sessionId) => record("store.revokeSession", sessionId),
|
|
};
|
|
const remoteScreen: NonNullable<TeamRouteDependencies["remoteScreen"]> = {
|
|
revokeTeamSession: (sessionId) => record("remoteScreen.revokeTeamSession", sessionId),
|
|
revokeMember: (memberId) => record("remoteScreen.revokeMember", memberId),
|
|
};
|
|
const browserView: NonNullable<TeamRouteDependencies["browserView"]> = {
|
|
revokeTeamSession: (sessionId) => record("browserView.revokeTeamSession", sessionId),
|
|
};
|
|
return { calls, store, remoteScreen, browserView };
|
|
}
|
|
|
|
async function send(method: string, path: string, dependencies: TeamRouteDependencies) {
|
|
const request = new IncomingMessage(new Socket());
|
|
const response = new ServerResponse(request);
|
|
const statuses: number[] = [];
|
|
const context: TeamApiRequestContext = {
|
|
request,
|
|
response,
|
|
method,
|
|
url: new URL(path, ORIGIN),
|
|
protocol: 3,
|
|
capabilities: new Set(),
|
|
member: OWNER,
|
|
token: "token-a",
|
|
agentLevel: () => "manage",
|
|
sessionId: "session-a",
|
|
sessionExpiresAt: "2026-10-18T00:00:00.000Z",
|
|
fromChrome: false,
|
|
sentMessage: () => undefined,
|
|
json: (status) => {
|
|
statuses.push(status);
|
|
return "handled";
|
|
},
|
|
empty: (status) => {
|
|
statuses.push(status);
|
|
return "handled";
|
|
},
|
|
};
|
|
const outcome = await routeTeam(context, dependencies);
|
|
return { outcome, status: statuses[0] ?? null };
|
|
}
|
|
|
|
const routes = {
|
|
getPresence: () => ({ serverId: null, members: [], updatedAt: "2026-10-17T00:00:00.000Z" }),
|
|
refreshPresence: () => {},
|
|
};
|
|
|
|
try {
|
|
{
|
|
const { calls, store, remoteScreen, browserView } = recorder();
|
|
const result = await send("POST", TEAM_API_ROUTES.auth.logout, { store, remoteScreen, browserView, ...routes });
|
|
check("a sign-out answers 204", result.status === 204, result);
|
|
check("a sign-out ends the store session", calls["store.logout"]?.join() === "token-a", calls);
|
|
check(
|
|
"a sign-out closes the remote screen of the session",
|
|
calls["remoteScreen.revokeTeamSession"]?.join() === "session-a",
|
|
calls,
|
|
);
|
|
check(
|
|
"a sign-out closes the browser view of the session",
|
|
calls["browserView.revokeTeamSession"]?.join() === "session-a",
|
|
calls,
|
|
);
|
|
}
|
|
{
|
|
const { calls, store, remoteScreen, browserView } = recorder();
|
|
const result = await send("DELETE", TEAM_API_ROUTES.team.session(SESSION_B.id), {
|
|
store,
|
|
remoteScreen,
|
|
browserView,
|
|
...routes,
|
|
});
|
|
check("an owner who ends a session gets 204", result.status === 204, result);
|
|
check("a session end removes the store session", calls["store.revokeSession"]?.join() === "session-b", calls);
|
|
check(
|
|
"a session end closes the remote screen of the session",
|
|
calls["remoteScreen.revokeTeamSession"]?.join() === "session-b",
|
|
calls,
|
|
);
|
|
check(
|
|
"a session end closes the browser view of the session",
|
|
calls["browserView.revokeTeamSession"]?.join() === "session-b",
|
|
calls,
|
|
);
|
|
}
|
|
{
|
|
const { calls, store, remoteScreen } = recorder();
|
|
const result = await send("DELETE", TEAM_API_ROUTES.team.session(SESSION_B.id), { store, remoteScreen, ...routes });
|
|
check("a session end with no browser view still answers 204", result.status === 204, { result, calls });
|
|
}
|
|
report.passed = checks.every((entry) => entry.passed);
|
|
} catch (error) {
|
|
report.error = error instanceof Error ? (error.stack ?? error.message) : String(error);
|
|
} finally {
|
|
mkdirSync(OUT, { recursive: true });
|
|
writeFileSync(join(OUT, "report.json"), `${JSON.stringify(report, null, 2)}\n`);
|
|
for (const entry of checks) process.stdout.write(`${entry.passed ? "PASS" : "FAIL"} ${entry.name}\n`);
|
|
if (report.error) process.stdout.write(`${report.error}\n`);
|
|
process.stdout.write(`Report: ${join(OUT, "report.json")}\n`);
|
|
if (!report.passed) process.exitCode = 1;
|
|
}
|