源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s

This commit is contained in:
agent-radar committed 2026-09-01 20:17:27 +08:00
commit 2ed3415fb7
8018 files changed
+1936476

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
# `@tutti-os/analytics`
Shared renderer analytics infrastructure for Tutti applications.
The package owns the best-effort `ReporterService`, its DI service identifier,
the renderer-to-daemon transport contract, a typed business-event reporter
base, and a no-op implementation for optional integrations. Product
repositories still own event catalogs, event-specific parameter types, domain
enrichment, and daemon HTTP adapters.
```ts
import {
IReporterService,
ReporterService,
type AnalyticsTransport
} from "@tutti-os/analytics";
const transport: AnalyticsTransport = {
async trackEvents(events) {
await desktopDaemon.reportAnalyticsEvents({ events });
}
};
const reporter = new ReporterService({ transport });
await reporter.track("workspace.opened", { source: "dashboard" });
```
Transport failures are intentionally swallowed so analytics never interrupts a
renderer product flow.
Reusable domain packages may accept
`Pick<IReporterService, "trackEvents">` and emit their own domain-owned events.
The product composition root remains responsible for registering the concrete
service and transport.
+42
View File
@@ -0,0 +1,42 @@
{
"name": "@tutti-os/analytics",
"version": "0.1.0",
"private": false,
"type": "module",
"types": "./src/index.ts",
"exports": {
".": "./src/index.ts"
},
"files": [
"dist",
"README.md"
],
"repository": {
"type": "git",
"url": "git+https://github.com/tutti-os/tutti.git",
"directory": "packages/analytics/core"
},
"scripts": {
"build": "tsup --config tsup.config.ts",
"test": "node --test --experimental-strip-types \"./src/**/*.test.ts\"",
"typecheck": "node ../../../tools/scripts/run-tsgo-typecheck.mjs"
},
"dependencies": {
"@tutti-os/infra": "0.1.0"
},
"devDependencies": {
"@tutti-os/config-tsconfig": "workspace:*",
"@types/node": "^24.0.1",
"typescript": "^5.8.3"
},
"publishConfig": {
"access": "public",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"types": "./dist/index.d.ts"
}
}
@@ -0,0 +1,55 @@
import assert from "node:assert/strict";
import test from "node:test";
import {
BaseAnalyticsReporter,
type AnalyticsReporterDependencies
} from "./baseReporter.ts";
import type { ReporterEventInput } from "./reporterService.ts";
test("base analytics reporter normalizes parameter names without owning event semantics", async () => {
const calls: ReporterEventInput[][] = [];
class WorkspaceOpenedReporter extends BaseAnalyticsReporter<{
entrySource: string;
firstOpen: boolean;
}> {
protected readonly eventName = "workspace.opened";
constructor(
params: { entrySource: string; firstOpen: boolean },
dependencies: AnalyticsReporterDependencies
) {
super(params, dependencies);
}
}
const dependencies: AnalyticsReporterDependencies = {
now: () => 1749124800000,
reporterService: {
async trackEvents(events) {
calls.push(events);
}
}
};
await new WorkspaceOpenedReporter(
{
entrySource: "dock",
firstOpen: true
},
dependencies
).report();
assert.deepEqual(calls, [
[
{
clientTS: 1749124800000,
name: "workspace.opened",
params: {
entry_source: "dock",
first_open: true
}
}
]
]);
});
@@ -0,0 +1,74 @@
import type {
IReporterService,
ReporterEventParams
} from "./reporterService.ts";
export type AnalyticsReporterParamValue =
| string
| number
| boolean
| null
| readonly string[]
| readonly number[]
| readonly boolean[];
export type AnalyticsReporterParams = Record<
string,
AnalyticsReporterParamValue
>;
export interface AnalyticsReporterDependencies {
reporterService: Pick<IReporterService, "trackEvents">;
now?: () => number;
}
/**
* Product-neutral base class for typed business-event reporters.
*
* Event names and parameter types remain owned by the consuming domain. This
* base only handles event construction, timestamps, and camelCase-to-snake_case
* protocol parameter names.
*/
export abstract class BaseAnalyticsReporter<TParams extends object> {
protected abstract readonly eventName: string;
private readonly params: TParams;
private readonly reporterService: Pick<IReporterService, "trackEvents">;
private readonly now: () => number;
protected constructor(
params: TParams,
dependencies: AnalyticsReporterDependencies
) {
this.params = params;
this.reporterService = dependencies.reporterService;
this.now = dependencies.now ?? Date.now;
}
async report(): Promise<void> {
await this.reporterService.trackEvents([
{
clientTS: this.now(),
name: this.eventName,
params: this.toProtocolParams()
}
]);
}
protected toProtocolParams(): ReporterEventParams {
return toAnalyticsProtocolParams(this.params);
}
}
export function toAnalyticsProtocolParams(params: object): ReporterEventParams {
return Object.fromEntries(
Object.entries(params).map(([key, value]) => [
toAnalyticsParamName(key),
value
])
);
}
export function toAnalyticsParamName(name: string): string {
return name.replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`);
}
+22
View File
@@ -0,0 +1,22 @@
export {
BaseAnalyticsReporter,
toAnalyticsParamName,
toAnalyticsProtocolParams,
type AnalyticsReporterDependencies,
type AnalyticsReporterParams,
type AnalyticsReporterParamValue
} from "./baseReporter.ts";
export {
NoopReporterService,
noopReporterService
} from "./noopReporterService.ts";
export {
IReporterService,
ReporterService,
type AnalyticsTransport,
type AnalyticsTransportEvent,
type IReporterService as ReporterServiceContract,
type ReporterEventInput,
type ReporterEventParams,
type ReporterServiceDependencies
} from "./reporterService.ts";
@@ -0,0 +1,16 @@
import type {
IReporterService,
ReporterEventInput,
ReporterEventParams
} from "./reporterService.ts";
/** Reporter service used by optional analytics integrations. */
export class NoopReporterService implements IReporterService {
readonly _serviceBrand: undefined;
async track(_name: string, _params?: ReporterEventParams): Promise<void> {}
async trackEvents(_events: ReporterEventInput[]): Promise<void> {}
}
export const noopReporterService: IReporterService = new NoopReporterService();
@@ -0,0 +1,76 @@
import assert from "node:assert/strict";
import test from "node:test";
import {
ReporterService,
type AnalyticsTransportEvent
} from "./reporterService.ts";
test("adds timestamps and daemon transport parameters without mutating input", async () => {
const calls: (readonly AnalyticsTransportEvent[])[] = [];
const params = { mode: "agent", source: "dock" };
const service = new ReporterService({
transport: {
async trackEvents(events) {
calls.push(structuredClone(events));
}
},
commonParams: { mode: "os" },
now: () => 1749124800000
});
await service.trackEvents([
{
name: "workspace.opened",
params
},
{
clientTS: 1749124800001,
name: "screen.viewed"
}
]);
assert.deepEqual(calls, [
[
{
clientTS: 1749124800000,
name: "workspace.opened",
params: {
mode: "os",
source: "dock"
}
},
{
clientTS: 1749124800001,
name: "screen.viewed",
params: {
mode: "os"
}
}
]
]);
assert.deepEqual(params, { mode: "agent", source: "dock" });
});
test("swallows common parameter and transport failures", async () => {
const commonFailure = new ReporterService({
transport: {
async trackEvents() {
throw new Error("should not be reached");
}
},
commonParams() {
throw new Error("preferences unavailable");
}
});
const transportFailure = new ReporterService({
transport: {
async trackEvents() {
throw new Error("daemon offline");
}
}
});
await assert.doesNotReject(() => commonFailure.track("workspace.opened"));
await assert.doesNotReject(() => transportFailure.track("workspace.opened"));
});
@@ -0,0 +1,92 @@
import { createDecorator } from "@tutti-os/infra/di";
export type ReporterEventParams = Record<string, unknown>;
export interface ReporterEventInput {
name: string;
clientTS?: number;
params?: ReporterEventParams;
}
export interface AnalyticsTransportEvent {
name: string;
clientTS: number;
params?: ReporterEventParams;
}
export interface AnalyticsTransport {
trackEvents(events: readonly AnalyticsTransportEvent[]): Promise<void>;
}
export interface ReporterServiceDependencies {
transport: AnalyticsTransport;
commonParams?: ReporterEventParams | (() => ReporterEventParams | undefined);
now?: () => number;
}
export interface IReporterService {
readonly _serviceBrand: undefined;
track(name: string, params?: ReporterEventParams): Promise<void>;
trackEvents(events: ReporterEventInput[]): Promise<void>;
}
export const IReporterService =
createDecorator<IReporterService>("reporter-service");
export class ReporterService implements IReporterService {
readonly _serviceBrand: undefined;
private readonly transport: AnalyticsTransport;
private readonly commonParams:
| ReporterEventParams
| (() => ReporterEventParams | undefined)
| undefined;
private readonly now: () => number;
constructor(dependencies: ReporterServiceDependencies) {
this.transport = dependencies.transport;
this.commonParams = dependencies.commonParams;
this.now = dependencies.now ?? Date.now;
}
async track(name: string, params?: ReporterEventParams): Promise<void> {
await this.trackEvents([{ name, params }]);
}
async trackEvents(events: ReporterEventInput[]): Promise<void> {
if (events.length === 0) {
return;
}
try {
const commonParams =
typeof this.commonParams === "function"
? this.commonParams()
: this.commonParams;
const transportEvents = events.map((event) =>
this.toTransportEvent(event, commonParams)
);
await this.transport.trackEvents(transportEvents);
} catch {
// Analytics is best-effort and must not affect renderer product flows.
}
}
private toTransportEvent(
event: ReporterEventInput,
commonParams: ReporterEventParams | undefined
): AnalyticsTransportEvent {
return {
clientTS: event.clientTS ?? this.now(),
name: event.name,
params:
event.params || commonParams
? {
...event.params,
...commonParams
}
: undefined
};
}
}
+8
View File
@@ -0,0 +1,8 @@
{
"extends": "../../../packages/configs/tsconfig/base.json",
"compilerOptions": {
"allowImportingTsExtensions": true,
"types": ["node"]
},
"include": ["src/**/*"]
}
+11
View File
@@ -0,0 +1,11 @@
import { defineConfig } from "tsup";
export default defineConfig({
clean: true,
dts: true,
entry: {
index: "src/index.ts"
},
format: ["esm"],
sourcemap: true
});