mirror of
http://192.168.0.88:13333/lywsvip/openclaw-zero-token.git
synced 2026-05-20 18:09:41 +08:00
Major upgrade from e26988a38 to upstream v2026.3.28 (f9b107928).
Key changes:
- Upstream src/, ui/, extensions/ (89 bundled extensions)
- Zero-token web providers preserved in src/zero-token/
- AskOnce plugin restored and registered as CLI command
- Added missing packages: @anthropic-ai/vertex-sdk, @modelcontextprotocol/sdk
- Fixed tsconfig rootDir, skipLibCheck for plugin-sdk DTS build
- Added askonce to bundled plugin metadata and package.json exports
- Fixed AskOnce CLI command registration (missing commands metadata)
- Restored AskOnce adapter imports (correct 5-level relative paths)
- Removed stale migration artifacts from root directory
170 lines
4.8 KiB
TypeScript
170 lines
4.8 KiB
TypeScript
import { updateSessionStoreEntry, type SessionEntry } from "../config/sessions.js";
|
|
import type { AgentEventPayload } from "../infra/agent-events.js";
|
|
import { loadSessionEntry } from "./session-utils.js";
|
|
import type { GatewaySessionRow, SessionRunStatus } from "./session-utils.types.js";
|
|
|
|
type LifecyclePhase = "start" | "end" | "error";
|
|
|
|
type LifecycleEventLike = Pick<AgentEventPayload, "ts"> & {
|
|
data?: {
|
|
phase?: unknown;
|
|
startedAt?: unknown;
|
|
endedAt?: unknown;
|
|
aborted?: unknown;
|
|
stopReason?: unknown;
|
|
};
|
|
};
|
|
|
|
type LifecycleSessionShape = Pick<
|
|
GatewaySessionRow,
|
|
"updatedAt" | "status" | "startedAt" | "endedAt" | "runtimeMs" | "abortedLastRun"
|
|
>;
|
|
|
|
type PersistedLifecycleSessionShape = Pick<
|
|
SessionEntry,
|
|
"updatedAt" | "status" | "startedAt" | "endedAt" | "runtimeMs" | "abortedLastRun"
|
|
>;
|
|
|
|
export type GatewaySessionLifecycleSnapshot = Partial<LifecycleSessionShape>;
|
|
|
|
function isFiniteTimestamp(value: unknown): value is number {
|
|
return typeof value === "number" && Number.isFinite(value) && value > 0;
|
|
}
|
|
|
|
function resolveLifecyclePhase(event: LifecycleEventLike): LifecyclePhase | null {
|
|
const phase = typeof event.data?.phase === "string" ? event.data.phase : "";
|
|
return phase === "start" || phase === "end" || phase === "error" ? phase : null;
|
|
}
|
|
|
|
function resolveTerminalStatus(event: LifecycleEventLike): SessionRunStatus {
|
|
const phase = resolveLifecyclePhase(event);
|
|
if (phase === "error") {
|
|
return "failed";
|
|
}
|
|
|
|
const stopReason = typeof event.data?.stopReason === "string" ? event.data.stopReason : "";
|
|
if (stopReason === "aborted") {
|
|
return "killed";
|
|
}
|
|
|
|
return event.data?.aborted === true ? "timeout" : "done";
|
|
}
|
|
|
|
function resolveLifecycleStartedAt(
|
|
existingStartedAt: number | undefined,
|
|
event: LifecycleEventLike,
|
|
): number | undefined {
|
|
if (isFiniteTimestamp(event.data?.startedAt)) {
|
|
return event.data.startedAt;
|
|
}
|
|
if (isFiniteTimestamp(existingStartedAt)) {
|
|
return existingStartedAt;
|
|
}
|
|
return isFiniteTimestamp(event.ts) ? event.ts : undefined;
|
|
}
|
|
|
|
function resolveLifecycleEndedAt(event: LifecycleEventLike): number | undefined {
|
|
if (isFiniteTimestamp(event.data?.endedAt)) {
|
|
return event.data.endedAt;
|
|
}
|
|
return isFiniteTimestamp(event.ts) ? event.ts : undefined;
|
|
}
|
|
|
|
function resolveRuntimeMs(params: {
|
|
startedAt?: number;
|
|
endedAt?: number;
|
|
existingRuntimeMs?: number;
|
|
}): number | undefined {
|
|
const { startedAt, endedAt, existingRuntimeMs } = params;
|
|
if (isFiniteTimestamp(startedAt) && isFiniteTimestamp(endedAt)) {
|
|
return Math.max(0, endedAt - startedAt);
|
|
}
|
|
if (
|
|
typeof existingRuntimeMs === "number" &&
|
|
Number.isFinite(existingRuntimeMs) &&
|
|
existingRuntimeMs >= 0
|
|
) {
|
|
return existingRuntimeMs;
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
export function deriveGatewaySessionLifecycleSnapshot(params: {
|
|
session?: Partial<LifecycleSessionShape> | null;
|
|
event: LifecycleEventLike;
|
|
}): GatewaySessionLifecycleSnapshot {
|
|
const phase = resolveLifecyclePhase(params.event);
|
|
if (!phase) {
|
|
return {};
|
|
}
|
|
|
|
const existing = params.session ?? undefined;
|
|
if (phase === "start") {
|
|
const startedAt = resolveLifecycleStartedAt(existing?.startedAt, params.event);
|
|
const updatedAt = startedAt ?? existing?.updatedAt;
|
|
return {
|
|
updatedAt,
|
|
status: "running",
|
|
startedAt,
|
|
endedAt: undefined,
|
|
runtimeMs: undefined,
|
|
abortedLastRun: false,
|
|
};
|
|
}
|
|
|
|
const startedAt = resolveLifecycleStartedAt(existing?.startedAt, params.event);
|
|
const endedAt = resolveLifecycleEndedAt(params.event);
|
|
const updatedAt = endedAt ?? existing?.updatedAt;
|
|
return {
|
|
updatedAt,
|
|
status: resolveTerminalStatus(params.event),
|
|
startedAt,
|
|
endedAt,
|
|
runtimeMs: resolveRuntimeMs({
|
|
startedAt,
|
|
endedAt,
|
|
existingRuntimeMs: existing?.runtimeMs,
|
|
}),
|
|
abortedLastRun: resolveTerminalStatus(params.event) === "killed",
|
|
};
|
|
}
|
|
|
|
export function derivePersistedSessionLifecyclePatch(params: {
|
|
entry?: Partial<PersistedLifecycleSessionShape> | null;
|
|
event: LifecycleEventLike;
|
|
}): Partial<PersistedLifecycleSessionShape> {
|
|
const snapshot = deriveGatewaySessionLifecycleSnapshot({
|
|
session: params.entry ?? undefined,
|
|
event: params.event,
|
|
});
|
|
return {
|
|
...snapshot,
|
|
updatedAt: typeof snapshot.updatedAt === "number" ? snapshot.updatedAt : undefined,
|
|
};
|
|
}
|
|
|
|
export async function persistGatewaySessionLifecycleEvent(params: {
|
|
sessionKey: string;
|
|
event: LifecycleEventLike;
|
|
}): Promise<void> {
|
|
const phase = resolveLifecyclePhase(params.event);
|
|
if (!phase) {
|
|
return;
|
|
}
|
|
|
|
const sessionEntry = loadSessionEntry(params.sessionKey);
|
|
if (!sessionEntry.entry) {
|
|
return;
|
|
}
|
|
|
|
await updateSessionStoreEntry({
|
|
storePath: sessionEntry.storePath,
|
|
sessionKey: sessionEntry.canonicalKey,
|
|
update: async (entry) =>
|
|
derivePersistedSessionLifecyclePatch({
|
|
entry,
|
|
event: params.event,
|
|
}),
|
|
});
|
|
}
|