mirror of
https://github.com/yuangyaa/openclaw.git
synced 2026-07-14 11:07:29 +08:00
refactor(shared): centralize assistant identity and usage timeseries types
This commit is contained in:
@@ -3,6 +3,7 @@ import { resolveAgentIdentity } from "../agents/identity.js";
|
|||||||
import { loadAgentIdentity } from "../commands/agents.config.js";
|
import { loadAgentIdentity } from "../commands/agents.config.js";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import { normalizeAgentId } from "../routing/session-key.js";
|
import { normalizeAgentId } from "../routing/session-key.js";
|
||||||
|
import { coerceIdentityValue } from "../shared/assistant-identity-values.js";
|
||||||
import {
|
import {
|
||||||
isAvatarHttpUrl,
|
isAvatarHttpUrl,
|
||||||
isAvatarImageDataUrl,
|
isAvatarImageDataUrl,
|
||||||
@@ -26,20 +27,6 @@ export type AssistantIdentity = {
|
|||||||
emoji?: string;
|
emoji?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
function coerceIdentityValue(value: string | undefined, maxLength: number): string | undefined {
|
|
||||||
if (typeof value !== "string") {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const trimmed = value.trim();
|
|
||||||
if (!trimmed) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
if (trimmed.length <= maxLength) {
|
|
||||||
return trimmed;
|
|
||||||
}
|
|
||||||
return trimmed.slice(0, maxLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
function isAvatarUrl(value: string): boolean {
|
function isAvatarUrl(value: string): boolean {
|
||||||
return isAvatarHttpUrl(value) || isAvatarImageDataUrl(value);
|
return isAvatarHttpUrl(value) || isAvatarImageDataUrl(value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
import type { NormalizedUsage } from "../agents/usage.js";
|
import type { NormalizedUsage } from "../agents/usage.js";
|
||||||
|
import type {
|
||||||
|
SessionUsageTimePoint as SharedSessionUsageTimePoint,
|
||||||
|
SessionUsageTimeSeries as SharedSessionUsageTimeSeries,
|
||||||
|
} from "../shared/session-usage-timeseries-types.js";
|
||||||
|
|
||||||
export type CostBreakdown = {
|
export type CostBreakdown = {
|
||||||
total?: number;
|
total?: number;
|
||||||
@@ -141,22 +145,9 @@ export type DiscoveredSession = {
|
|||||||
firstUserMessage?: string;
|
firstUserMessage?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SessionUsageTimePoint = {
|
export type SessionUsageTimePoint = SharedSessionUsageTimePoint;
|
||||||
timestamp: number;
|
|
||||||
input: number;
|
|
||||||
output: number;
|
|
||||||
cacheRead: number;
|
|
||||||
cacheWrite: number;
|
|
||||||
totalTokens: number;
|
|
||||||
cost: number;
|
|
||||||
cumulativeTokens: number;
|
|
||||||
cumulativeCost: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SessionUsageTimeSeries = {
|
export type SessionUsageTimeSeries = SharedSessionUsageTimeSeries;
|
||||||
sessionId?: string;
|
|
||||||
points: SessionUsageTimePoint[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SessionLogEntry = {
|
export type SessionLogEntry = {
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
|
|||||||
16
src/shared/assistant-identity-values.ts
Normal file
16
src/shared/assistant-identity-values.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
export function coerceIdentityValue(
|
||||||
|
value: string | undefined,
|
||||||
|
maxLength: number,
|
||||||
|
): string | undefined {
|
||||||
|
if (typeof value !== "string") {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const trimmed = value.trim();
|
||||||
|
if (!trimmed) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
if (trimmed.length <= maxLength) {
|
||||||
|
return trimmed;
|
||||||
|
}
|
||||||
|
return trimmed.slice(0, maxLength);
|
||||||
|
}
|
||||||
16
src/shared/session-usage-timeseries-types.ts
Normal file
16
src/shared/session-usage-timeseries-types.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
export type SessionUsageTimePoint = {
|
||||||
|
timestamp: number;
|
||||||
|
input: number;
|
||||||
|
output: number;
|
||||||
|
cacheRead: number;
|
||||||
|
cacheWrite: number;
|
||||||
|
totalTokens: number;
|
||||||
|
cost: number;
|
||||||
|
cumulativeTokens: number;
|
||||||
|
cumulativeCost: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SessionUsageTimeSeries = {
|
||||||
|
sessionId?: string;
|
||||||
|
points: SessionUsageTimePoint[];
|
||||||
|
};
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { coerceIdentityValue } from "../../../src/shared/assistant-identity-values.js";
|
||||||
|
|
||||||
const MAX_ASSISTANT_NAME = 50;
|
const MAX_ASSISTANT_NAME = 50;
|
||||||
const MAX_ASSISTANT_AVATAR = 200;
|
const MAX_ASSISTANT_AVATAR = 200;
|
||||||
|
|
||||||
@@ -10,20 +12,6 @@ export type AssistantIdentity = {
|
|||||||
avatar: string | null;
|
avatar: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
function coerceIdentityValue(value: string | undefined, maxLength: number): string | undefined {
|
|
||||||
if (typeof value !== "string") {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const trimmed = value.trim();
|
|
||||||
if (!trimmed) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
if (trimmed.length <= maxLength) {
|
|
||||||
return trimmed;
|
|
||||||
}
|
|
||||||
return trimmed.slice(0, maxLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function normalizeAssistantIdentity(
|
export function normalizeAssistantIdentity(
|
||||||
input?: Partial<AssistantIdentity> | null,
|
input?: Partial<AssistantIdentity> | null,
|
||||||
): AssistantIdentity {
|
): AssistantIdentity {
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
import type {
|
||||||
|
SessionUsageTimePoint as SharedSessionUsageTimePoint,
|
||||||
|
SessionUsageTimeSeries as SharedSessionUsageTimeSeries,
|
||||||
|
} from "../../../src/shared/session-usage-timeseries-types.js";
|
||||||
import type { SessionsUsageResult as SharedSessionsUsageResult } from "../../../src/shared/usage-types.js";
|
import type { SessionsUsageResult as SharedSessionsUsageResult } from "../../../src/shared/usage-types.js";
|
||||||
|
|
||||||
export type SessionsUsageEntry = SharedSessionsUsageResult["sessions"][number];
|
export type SessionsUsageEntry = SharedSessionsUsageResult["sessions"][number];
|
||||||
@@ -13,19 +17,6 @@ export type CostUsageSummary = {
|
|||||||
totals: SessionsUsageTotals;
|
totals: SessionsUsageTotals;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SessionUsageTimePoint = {
|
export type SessionUsageTimePoint = SharedSessionUsageTimePoint;
|
||||||
timestamp: number;
|
|
||||||
input: number;
|
|
||||||
output: number;
|
|
||||||
cacheRead: number;
|
|
||||||
cacheWrite: number;
|
|
||||||
totalTokens: number;
|
|
||||||
cost: number;
|
|
||||||
cumulativeTokens: number;
|
|
||||||
cumulativeCost: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SessionUsageTimeSeries = {
|
export type SessionUsageTimeSeries = SharedSessionUsageTimeSeries;
|
||||||
sessionId?: string;
|
|
||||||
points: SessionUsageTimePoint[];
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user