From 8768487aeeda7335538904bef2cfcf40be8d6fcb Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 2 Mar 2026 15:39:18 +0000 Subject: [PATCH] refactor(shared): dedupe protocol schema typing and session/media helpers --- .../protocol/schema/protocol-schemas.ts | 4 +- src/gateway/protocol/schema/types.ts | 379 ++++++------------ src/gateway/server-ws-runtime.ts | 28 +- src/gateway/server/ws-connection.ts | 9 +- src/gateway/session-utils.types.ts | 30 +- .../providers/google/inline-data.ts | 17 +- .../providers/moonshot/video.ts | 15 +- src/media-understanding/providers/shared.ts | 21 + src/shared/session-types.ts | 28 ++ ui/src/ui/types.ts | 46 +-- 10 files changed, 224 insertions(+), 353 deletions(-) create mode 100644 src/shared/session-types.ts diff --git a/src/gateway/protocol/schema/protocol-schemas.ts b/src/gateway/protocol/schema/protocol-schemas.ts index fcddef1eec5..bd20ddbd462 100644 --- a/src/gateway/protocol/schema/protocol-schemas.ts +++ b/src/gateway/protocol/schema/protocol-schemas.ts @@ -146,7 +146,7 @@ import { WizardStepSchema, } from "./wizard.js"; -export const ProtocolSchemas: Record = { +export const ProtocolSchemas = { ConnectParams: ConnectParamsSchema, HelloOk: HelloOkSchema, RequestFrame: RequestFrameSchema, @@ -272,6 +272,6 @@ export const ProtocolSchemas: Record = { UpdateRunParams: UpdateRunParamsSchema, TickEvent: TickEventSchema, ShutdownEvent: ShutdownEventSchema, -}; +} satisfies Record; export const PROTOCOL_VERSION = 3 as const; diff --git a/src/gateway/protocol/schema/types.ts b/src/gateway/protocol/schema/types.ts index 126aadc2921..491b95795e1 100644 --- a/src/gateway/protocol/schema/types.ts +++ b/src/gateway/protocol/schema/types.ts @@ -1,259 +1,124 @@ import type { Static } from "@sinclair/typebox"; -import type { - AgentEventSchema, - AgentIdentityParamsSchema, - AgentIdentityResultSchema, - AgentWaitParamsSchema, - PollParamsSchema, - WakeParamsSchema, -} from "./agent.js"; -import type { - AgentSummarySchema, - AgentsFileEntrySchema, - AgentsCreateParamsSchema, - AgentsCreateResultSchema, - AgentsDeleteParamsSchema, - AgentsDeleteResultSchema, - AgentsFilesGetParamsSchema, - AgentsFilesGetResultSchema, - AgentsFilesListParamsSchema, - AgentsFilesListResultSchema, - AgentsFilesSetParamsSchema, - AgentsFilesSetResultSchema, - AgentsListParamsSchema, - AgentsListResultSchema, - AgentsUpdateParamsSchema, - AgentsUpdateResultSchema, - ModelChoiceSchema, - ModelsListParamsSchema, - ModelsListResultSchema, - SkillsBinsParamsSchema, - SkillsBinsResultSchema, - SkillsInstallParamsSchema, - SkillsStatusParamsSchema, - SkillsUpdateParamsSchema, - ToolCatalogEntrySchema, - ToolCatalogGroupSchema, - ToolCatalogProfileSchema, - ToolsCatalogParamsSchema, - ToolsCatalogResultSchema, -} from "./agents-models-skills.js"; -import type { - ChannelsLogoutParamsSchema, - TalkConfigParamsSchema, - TalkConfigResultSchema, - ChannelsStatusParamsSchema, - ChannelsStatusResultSchema, - TalkModeParamsSchema, - WebLoginStartParamsSchema, - WebLoginWaitParamsSchema, -} from "./channels.js"; -import type { - ConfigApplyParamsSchema, - ConfigGetParamsSchema, - ConfigPatchParamsSchema, - ConfigSchemaParamsSchema, - ConfigSchemaResponseSchema, - ConfigSetParamsSchema, - UpdateRunParamsSchema, -} from "./config.js"; -import type { - CronAddParamsSchema, - CronJobSchema, - CronListParamsSchema, - CronRemoveParamsSchema, - CronRunLogEntrySchema, - CronRunParamsSchema, - CronRunsParamsSchema, - CronStatusParamsSchema, - CronUpdateParamsSchema, -} from "./cron.js"; -import type { - DevicePairApproveParamsSchema, - DevicePairListParamsSchema, - DevicePairRemoveParamsSchema, - DevicePairRejectParamsSchema, - DeviceTokenRevokeParamsSchema, - DeviceTokenRotateParamsSchema, -} from "./devices.js"; -import type { - ExecApprovalsGetParamsSchema, - ExecApprovalsNodeGetParamsSchema, - ExecApprovalsNodeSetParamsSchema, - ExecApprovalsSetParamsSchema, - ExecApprovalsSnapshotSchema, - ExecApprovalRequestParamsSchema, - ExecApprovalResolveParamsSchema, -} from "./exec-approvals.js"; -import type { - ConnectParamsSchema, - ErrorShapeSchema, - EventFrameSchema, - GatewayFrameSchema, - HelloOkSchema, - RequestFrameSchema, - ResponseFrameSchema, - ShutdownEventSchema, - TickEventSchema, -} from "./frames.js"; -import type { - ChatAbortParamsSchema, - ChatEventSchema, - ChatInjectParamsSchema, - LogsTailParamsSchema, - LogsTailResultSchema, -} from "./logs-chat.js"; -import type { - NodeDescribeParamsSchema, - NodeEventParamsSchema, - NodeInvokeParamsSchema, - NodeInvokeResultParamsSchema, - NodeListParamsSchema, - NodePairApproveParamsSchema, - NodePairListParamsSchema, - NodePairRejectParamsSchema, - NodePairRequestParamsSchema, - NodePairVerifyParamsSchema, - NodeRenameParamsSchema, -} from "./nodes.js"; -import type { PushTestParamsSchema, PushTestResultSchema } from "./push.js"; -import type { - SessionsCompactParamsSchema, - SessionsDeleteParamsSchema, - SessionsListParamsSchema, - SessionsPatchParamsSchema, - SessionsPreviewParamsSchema, - SessionsResetParamsSchema, - SessionsResolveParamsSchema, - SessionsUsageParamsSchema, -} from "./sessions.js"; -import type { PresenceEntrySchema, SnapshotSchema, StateVersionSchema } from "./snapshot.js"; -import type { - WizardCancelParamsSchema, - WizardNextParamsSchema, - WizardNextResultSchema, - WizardStartParamsSchema, - WizardStartResultSchema, - WizardStatusParamsSchema, - WizardStatusResultSchema, - WizardStepSchema, -} from "./wizard.js"; +import { ProtocolSchemas } from "./protocol-schemas.js"; -export type ConnectParams = Static; -export type HelloOk = Static; -export type RequestFrame = Static; -export type ResponseFrame = Static; -export type EventFrame = Static; -export type GatewayFrame = Static; -export type Snapshot = Static; -export type PresenceEntry = Static; -export type ErrorShape = Static; -export type StateVersion = Static; -export type AgentEvent = Static; -export type AgentIdentityParams = Static; -export type AgentIdentityResult = Static; -export type PollParams = Static; -export type AgentWaitParams = Static; -export type WakeParams = Static; -export type NodePairRequestParams = Static; -export type NodePairListParams = Static; -export type NodePairApproveParams = Static; -export type NodePairRejectParams = Static; -export type NodePairVerifyParams = Static; -export type NodeRenameParams = Static; -export type NodeListParams = Static; -export type NodeDescribeParams = Static; -export type NodeInvokeParams = Static; -export type NodeInvokeResultParams = Static; -export type NodeEventParams = Static; -export type PushTestParams = Static; -export type PushTestResult = Static; -export type SessionsListParams = Static; -export type SessionsPreviewParams = Static; -export type SessionsResolveParams = Static; -export type SessionsPatchParams = Static; -export type SessionsResetParams = Static; -export type SessionsDeleteParams = Static; -export type SessionsCompactParams = Static; -export type SessionsUsageParams = Static; -export type ConfigGetParams = Static; -export type ConfigSetParams = Static; -export type ConfigApplyParams = Static; -export type ConfigPatchParams = Static; -export type ConfigSchemaParams = Static; -export type ConfigSchemaResponse = Static; -export type WizardStartParams = Static; -export type WizardNextParams = Static; -export type WizardCancelParams = Static; -export type WizardStatusParams = Static; -export type WizardStep = Static; -export type WizardNextResult = Static; -export type WizardStartResult = Static; -export type WizardStatusResult = Static; -export type TalkModeParams = Static; -export type TalkConfigParams = Static; -export type TalkConfigResult = Static; -export type ChannelsStatusParams = Static; -export type ChannelsStatusResult = Static; -export type ChannelsLogoutParams = Static; -export type WebLoginStartParams = Static; -export type WebLoginWaitParams = Static; -export type AgentSummary = Static; -export type AgentsFileEntry = Static; -export type AgentsCreateParams = Static; -export type AgentsCreateResult = Static; -export type AgentsUpdateParams = Static; -export type AgentsUpdateResult = Static; -export type AgentsDeleteParams = Static; -export type AgentsDeleteResult = Static; -export type AgentsFilesListParams = Static; -export type AgentsFilesListResult = Static; -export type AgentsFilesGetParams = Static; -export type AgentsFilesGetResult = Static; -export type AgentsFilesSetParams = Static; -export type AgentsFilesSetResult = Static; -export type AgentsListParams = Static; -export type AgentsListResult = Static; -export type ModelChoice = Static; -export type ModelsListParams = Static; -export type ModelsListResult = Static; -export type SkillsStatusParams = Static; -export type ToolsCatalogParams = Static; -export type ToolCatalogProfile = Static; -export type ToolCatalogEntry = Static; -export type ToolCatalogGroup = Static; -export type ToolsCatalogResult = Static; -export type SkillsBinsParams = Static; -export type SkillsBinsResult = Static; -export type SkillsInstallParams = Static; -export type SkillsUpdateParams = Static; -export type CronJob = Static; -export type CronListParams = Static; -export type CronStatusParams = Static; -export type CronAddParams = Static; -export type CronUpdateParams = Static; -export type CronRemoveParams = Static; -export type CronRunParams = Static; -export type CronRunsParams = Static; -export type CronRunLogEntry = Static; -export type LogsTailParams = Static; -export type LogsTailResult = Static; -export type ExecApprovalsGetParams = Static; -export type ExecApprovalsSetParams = Static; -export type ExecApprovalsNodeGetParams = Static; -export type ExecApprovalsNodeSetParams = Static; -export type ExecApprovalsSnapshot = Static; -export type ExecApprovalRequestParams = Static; -export type ExecApprovalResolveParams = Static; -export type DevicePairListParams = Static; -export type DevicePairApproveParams = Static; -export type DevicePairRejectParams = Static; -export type DevicePairRemoveParams = Static; -export type DeviceTokenRotateParams = Static; -export type DeviceTokenRevokeParams = Static; -export type ChatAbortParams = Static; -export type ChatInjectParams = Static; -export type ChatEvent = Static; -export type UpdateRunParams = Static; -export type TickEvent = Static; -export type ShutdownEvent = Static; +type ProtocolSchemaName = keyof typeof ProtocolSchemas; +type SchemaType = Static<(typeof ProtocolSchemas)[TName]>; + +export type ConnectParams = SchemaType<"ConnectParams">; +export type HelloOk = SchemaType<"HelloOk">; +export type RequestFrame = SchemaType<"RequestFrame">; +export type ResponseFrame = SchemaType<"ResponseFrame">; +export type EventFrame = SchemaType<"EventFrame">; +export type GatewayFrame = SchemaType<"GatewayFrame">; +export type Snapshot = SchemaType<"Snapshot">; +export type PresenceEntry = SchemaType<"PresenceEntry">; +export type ErrorShape = SchemaType<"ErrorShape">; +export type StateVersion = SchemaType<"StateVersion">; +export type AgentEvent = SchemaType<"AgentEvent">; +export type AgentIdentityParams = SchemaType<"AgentIdentityParams">; +export type AgentIdentityResult = SchemaType<"AgentIdentityResult">; +export type PollParams = SchemaType<"PollParams">; +export type AgentWaitParams = SchemaType<"AgentWaitParams">; +export type WakeParams = SchemaType<"WakeParams">; +export type NodePairRequestParams = SchemaType<"NodePairRequestParams">; +export type NodePairListParams = SchemaType<"NodePairListParams">; +export type NodePairApproveParams = SchemaType<"NodePairApproveParams">; +export type NodePairRejectParams = SchemaType<"NodePairRejectParams">; +export type NodePairVerifyParams = SchemaType<"NodePairVerifyParams">; +export type NodeRenameParams = SchemaType<"NodeRenameParams">; +export type NodeListParams = SchemaType<"NodeListParams">; +export type NodeDescribeParams = SchemaType<"NodeDescribeParams">; +export type NodeInvokeParams = SchemaType<"NodeInvokeParams">; +export type NodeInvokeResultParams = SchemaType<"NodeInvokeResultParams">; +export type NodeEventParams = SchemaType<"NodeEventParams">; +export type PushTestParams = SchemaType<"PushTestParams">; +export type PushTestResult = SchemaType<"PushTestResult">; +export type SessionsListParams = SchemaType<"SessionsListParams">; +export type SessionsPreviewParams = SchemaType<"SessionsPreviewParams">; +export type SessionsResolveParams = SchemaType<"SessionsResolveParams">; +export type SessionsPatchParams = SchemaType<"SessionsPatchParams">; +export type SessionsResetParams = SchemaType<"SessionsResetParams">; +export type SessionsDeleteParams = SchemaType<"SessionsDeleteParams">; +export type SessionsCompactParams = SchemaType<"SessionsCompactParams">; +export type SessionsUsageParams = SchemaType<"SessionsUsageParams">; +export type ConfigGetParams = SchemaType<"ConfigGetParams">; +export type ConfigSetParams = SchemaType<"ConfigSetParams">; +export type ConfigApplyParams = SchemaType<"ConfigApplyParams">; +export type ConfigPatchParams = SchemaType<"ConfigPatchParams">; +export type ConfigSchemaParams = SchemaType<"ConfigSchemaParams">; +export type ConfigSchemaResponse = SchemaType<"ConfigSchemaResponse">; +export type WizardStartParams = SchemaType<"WizardStartParams">; +export type WizardNextParams = SchemaType<"WizardNextParams">; +export type WizardCancelParams = SchemaType<"WizardCancelParams">; +export type WizardStatusParams = SchemaType<"WizardStatusParams">; +export type WizardStep = SchemaType<"WizardStep">; +export type WizardNextResult = SchemaType<"WizardNextResult">; +export type WizardStartResult = SchemaType<"WizardStartResult">; +export type WizardStatusResult = SchemaType<"WizardStatusResult">; +export type TalkModeParams = SchemaType<"TalkModeParams">; +export type TalkConfigParams = SchemaType<"TalkConfigParams">; +export type TalkConfigResult = SchemaType<"TalkConfigResult">; +export type ChannelsStatusParams = SchemaType<"ChannelsStatusParams">; +export type ChannelsStatusResult = SchemaType<"ChannelsStatusResult">; +export type ChannelsLogoutParams = SchemaType<"ChannelsLogoutParams">; +export type WebLoginStartParams = SchemaType<"WebLoginStartParams">; +export type WebLoginWaitParams = SchemaType<"WebLoginWaitParams">; +export type AgentSummary = SchemaType<"AgentSummary">; +export type AgentsFileEntry = SchemaType<"AgentsFileEntry">; +export type AgentsCreateParams = SchemaType<"AgentsCreateParams">; +export type AgentsCreateResult = SchemaType<"AgentsCreateResult">; +export type AgentsUpdateParams = SchemaType<"AgentsUpdateParams">; +export type AgentsUpdateResult = SchemaType<"AgentsUpdateResult">; +export type AgentsDeleteParams = SchemaType<"AgentsDeleteParams">; +export type AgentsDeleteResult = SchemaType<"AgentsDeleteResult">; +export type AgentsFilesListParams = SchemaType<"AgentsFilesListParams">; +export type AgentsFilesListResult = SchemaType<"AgentsFilesListResult">; +export type AgentsFilesGetParams = SchemaType<"AgentsFilesGetParams">; +export type AgentsFilesGetResult = SchemaType<"AgentsFilesGetResult">; +export type AgentsFilesSetParams = SchemaType<"AgentsFilesSetParams">; +export type AgentsFilesSetResult = SchemaType<"AgentsFilesSetResult">; +export type AgentsListParams = SchemaType<"AgentsListParams">; +export type AgentsListResult = SchemaType<"AgentsListResult">; +export type ModelChoice = SchemaType<"ModelChoice">; +export type ModelsListParams = SchemaType<"ModelsListParams">; +export type ModelsListResult = SchemaType<"ModelsListResult">; +export type SkillsStatusParams = SchemaType<"SkillsStatusParams">; +export type ToolsCatalogParams = SchemaType<"ToolsCatalogParams">; +export type ToolCatalogProfile = SchemaType<"ToolCatalogProfile">; +export type ToolCatalogEntry = SchemaType<"ToolCatalogEntry">; +export type ToolCatalogGroup = SchemaType<"ToolCatalogGroup">; +export type ToolsCatalogResult = SchemaType<"ToolsCatalogResult">; +export type SkillsBinsParams = SchemaType<"SkillsBinsParams">; +export type SkillsBinsResult = SchemaType<"SkillsBinsResult">; +export type SkillsInstallParams = SchemaType<"SkillsInstallParams">; +export type SkillsUpdateParams = SchemaType<"SkillsUpdateParams">; +export type CronJob = SchemaType<"CronJob">; +export type CronListParams = SchemaType<"CronListParams">; +export type CronStatusParams = SchemaType<"CronStatusParams">; +export type CronAddParams = SchemaType<"CronAddParams">; +export type CronUpdateParams = SchemaType<"CronUpdateParams">; +export type CronRemoveParams = SchemaType<"CronRemoveParams">; +export type CronRunParams = SchemaType<"CronRunParams">; +export type CronRunsParams = SchemaType<"CronRunsParams">; +export type CronRunLogEntry = SchemaType<"CronRunLogEntry">; +export type LogsTailParams = SchemaType<"LogsTailParams">; +export type LogsTailResult = SchemaType<"LogsTailResult">; +export type ExecApprovalsGetParams = SchemaType<"ExecApprovalsGetParams">; +export type ExecApprovalsSetParams = SchemaType<"ExecApprovalsSetParams">; +export type ExecApprovalsNodeGetParams = SchemaType<"ExecApprovalsNodeGetParams">; +export type ExecApprovalsNodeSetParams = SchemaType<"ExecApprovalsNodeSetParams">; +export type ExecApprovalsSnapshot = SchemaType<"ExecApprovalsSnapshot">; +export type ExecApprovalRequestParams = SchemaType<"ExecApprovalRequestParams">; +export type ExecApprovalResolveParams = SchemaType<"ExecApprovalResolveParams">; +export type DevicePairListParams = SchemaType<"DevicePairListParams">; +export type DevicePairApproveParams = SchemaType<"DevicePairApproveParams">; +export type DevicePairRejectParams = SchemaType<"DevicePairRejectParams">; +export type DevicePairRemoveParams = SchemaType<"DevicePairRemoveParams">; +export type DeviceTokenRotateParams = SchemaType<"DeviceTokenRotateParams">; +export type DeviceTokenRevokeParams = SchemaType<"DeviceTokenRevokeParams">; +export type ChatAbortParams = SchemaType<"ChatAbortParams">; +export type ChatInjectParams = SchemaType<"ChatInjectParams">; +export type ChatEvent = SchemaType<"ChatEvent">; +export type UpdateRunParams = SchemaType<"UpdateRunParams">; +export type TickEvent = SchemaType<"TickEvent">; +export type ShutdownEvent = SchemaType<"ShutdownEvent">; diff --git a/src/gateway/server-ws-runtime.ts b/src/gateway/server-ws-runtime.ts index f03235daddf..795a162818f 100644 --- a/src/gateway/server-ws-runtime.ts +++ b/src/gateway/server-ws-runtime.ts @@ -1,25 +1,11 @@ -import type { WebSocketServer } from "ws"; import type { createSubsystemLogger } from "../logging/subsystem.js"; -import type { AuthRateLimiter } from "./auth-rate-limit.js"; -import type { ResolvedGatewayAuth } from "./auth.js"; import type { GatewayRequestContext, GatewayRequestHandlers } from "./server-methods/types.js"; -import { attachGatewayWsConnectionHandler } from "./server/ws-connection.js"; -import type { GatewayWsClient } from "./server/ws-types.js"; +import { + attachGatewayWsConnectionHandler, + type GatewayWsSharedHandlerParams, +} from "./server/ws-connection.js"; -export function attachGatewayWsHandlers(params: { - wss: WebSocketServer; - clients: Set; - port: number; - gatewayHost?: string; - canvasHostEnabled: boolean; - canvasHostServerPort?: number; - resolvedAuth: ResolvedGatewayAuth; - /** Optional rate limiter for auth brute-force protection. */ - rateLimiter?: AuthRateLimiter; - /** Browser-origin fallback limiter (loopback is never exempt). */ - browserRateLimiter?: AuthRateLimiter; - gatewayMethods: string[]; - events: string[]; +type GatewayWsRuntimeParams = GatewayWsSharedHandlerParams & { logGateway: ReturnType; logHealth: ReturnType; logWsControl: ReturnType; @@ -33,7 +19,9 @@ export function attachGatewayWsHandlers(params: { }, ) => void; context: GatewayRequestContext; -}) { +}; + +export function attachGatewayWsHandlers(params: GatewayWsRuntimeParams) { attachGatewayWsConnectionHandler({ wss: params.wss, clients: params.clients, diff --git a/src/gateway/server/ws-connection.ts b/src/gateway/server/ws-connection.ts index c2fad8059e8..1a66cbdfe63 100644 --- a/src/gateway/server/ws-connection.ts +++ b/src/gateway/server/ws-connection.ts @@ -58,7 +58,7 @@ const sanitizeLogValue = (value: string | undefined): string | undefined => { return truncateUtf16Safe(cleaned, LOG_HEADER_MAX_LEN); }; -export function attachGatewayWsConnectionHandler(params: { +export type GatewayWsSharedHandlerParams = { wss: WebSocketServer; clients: Set; port: number; @@ -72,6 +72,9 @@ export function attachGatewayWsConnectionHandler(params: { browserRateLimiter?: AuthRateLimiter; gatewayMethods: string[]; events: string[]; +}; + +export type AttachGatewayWsConnectionHandlerParams = GatewayWsSharedHandlerParams & { logGateway: SubsystemLogger; logHealth: SubsystemLogger; logWsControl: SubsystemLogger; @@ -85,7 +88,9 @@ export function attachGatewayWsConnectionHandler(params: { }, ) => void; buildRequestContext: () => GatewayRequestContext; -}) { +}; + +export function attachGatewayWsConnectionHandler(params: AttachGatewayWsConnectionHandlerParams) { const { wss, clients, diff --git a/src/gateway/session-utils.types.ts b/src/gateway/session-utils.types.ts index 233a3d7c782..711a1997f22 100644 --- a/src/gateway/session-utils.types.ts +++ b/src/gateway/session-utils.types.ts @@ -1,5 +1,10 @@ import type { ChatType } from "../channels/chat-type.js"; import type { SessionEntry } from "../config/sessions.js"; +import type { + GatewayAgentRow as SharedGatewayAgentRow, + SessionsListResultBase, + SessionsPatchResultBase, +} from "../shared/session-types.js"; import type { DeliveryContext } from "../utils/delivery-context.js"; export type GatewaySessionsDefaults = { @@ -44,17 +49,7 @@ export type GatewaySessionRow = { lastAccountId?: string; }; -export type GatewayAgentRow = { - id: string; - name?: string; - identity?: { - name?: string; - theme?: string; - emoji?: string; - avatar?: string; - avatarUrl?: string; - }; -}; +export type GatewayAgentRow = SharedGatewayAgentRow; export type SessionPreviewItem = { role: "user" | "assistant" | "tool" | "system" | "other"; @@ -72,18 +67,9 @@ export type SessionsPreviewResult = { previews: SessionsPreviewEntry[]; }; -export type SessionsListResult = { - ts: number; - path: string; - count: number; - defaults: GatewaySessionsDefaults; - sessions: GatewaySessionRow[]; -}; +export type SessionsListResult = SessionsListResultBase; -export type SessionsPatchResult = { - ok: true; - path: string; - key: string; +export type SessionsPatchResult = SessionsPatchResultBase & { entry: SessionEntry; resolved?: { modelProvider?: string; diff --git a/src/media-understanding/providers/google/inline-data.ts b/src/media-understanding/providers/google/inline-data.ts index e83b52ac102..69fd41871e8 100644 --- a/src/media-understanding/providers/google/inline-data.ts +++ b/src/media-understanding/providers/google/inline-data.ts @@ -1,6 +1,6 @@ import { normalizeGoogleModelId } from "../../../agents/models-config.providers.js"; import { parseGeminiAuth } from "../../../infra/gemini-auth.js"; -import { assertOkOrThrowHttpError, fetchWithTimeoutGuarded, normalizeBaseUrl } from "../shared.js"; +import { assertOkOrThrowHttpError, normalizeBaseUrl, postJsonRequest } from "../shared.js"; export async function generateGeminiInlineDataText(params: { buffer: Buffer; @@ -61,17 +61,14 @@ export async function generateGeminiInlineDataText(params: { ], }; - const { response: res, release } = await fetchWithTimeoutGuarded( + const { response: res, release } = await postJsonRequest({ url, - { - method: "POST", - headers, - body: JSON.stringify(body), - }, - params.timeoutMs, + headers, + body, + timeoutMs: params.timeoutMs, fetchFn, - allowPrivate ? { ssrfPolicy: { allowPrivateNetwork: true } } : undefined, - ); + allowPrivateNetwork: allowPrivate, + }); try { await assertOkOrThrowHttpError(res, params.httpErrorLabel); diff --git a/src/media-understanding/providers/moonshot/video.ts b/src/media-understanding/providers/moonshot/video.ts index c4548900307..0cc6f55a7e3 100644 --- a/src/media-understanding/providers/moonshot/video.ts +++ b/src/media-understanding/providers/moonshot/video.ts @@ -1,5 +1,5 @@ import type { VideoDescriptionRequest, VideoDescriptionResult } from "../../types.js"; -import { assertOkOrThrowHttpError, fetchWithTimeoutGuarded, normalizeBaseUrl } from "../shared.js"; +import { assertOkOrThrowHttpError, normalizeBaseUrl, postJsonRequest } from "../shared.js"; export const DEFAULT_MOONSHOT_VIDEO_BASE_URL = "https://api.moonshot.ai/v1"; const DEFAULT_MOONSHOT_VIDEO_MODEL = "kimi-k2.5"; @@ -84,16 +84,13 @@ export async function describeMoonshotVideo( ], }; - const { response: res, release } = await fetchWithTimeoutGuarded( + const { response: res, release } = await postJsonRequest({ url, - { - method: "POST", - headers, - body: JSON.stringify(body), - }, - params.timeoutMs, + headers, + body, + timeoutMs: params.timeoutMs, fetchFn, - ); + }); try { await assertOkOrThrowHttpError(res, "Moonshot video description failed"); diff --git a/src/media-understanding/providers/shared.ts b/src/media-understanding/providers/shared.ts index 96145b2e7e7..5e62e7cd914 100644 --- a/src/media-understanding/providers/shared.ts +++ b/src/media-understanding/providers/shared.ts @@ -53,6 +53,27 @@ export async function postTranscriptionRequest(params: { ); } +export async function postJsonRequest(params: { + url: string; + headers: Headers; + body: unknown; + timeoutMs: number; + fetchFn: typeof fetch; + allowPrivateNetwork?: boolean; +}) { + return fetchWithTimeoutGuarded( + params.url, + { + method: "POST", + headers: params.headers, + body: JSON.stringify(params.body), + }, + params.timeoutMs, + params.fetchFn, + params.allowPrivateNetwork ? { ssrfPolicy: { allowPrivateNetwork: true } } : undefined, + ); +} + export async function readErrorResponse(res: Response): Promise { try { const text = await res.text(); diff --git a/src/shared/session-types.ts b/src/shared/session-types.ts new file mode 100644 index 00000000000..ca52d394e33 --- /dev/null +++ b/src/shared/session-types.ts @@ -0,0 +1,28 @@ +export type GatewayAgentIdentity = { + name?: string; + theme?: string; + emoji?: string; + avatar?: string; + avatarUrl?: string; +}; + +export type GatewayAgentRow = { + id: string; + name?: string; + identity?: GatewayAgentIdentity; +}; + +export type SessionsListResultBase = { + ts: number; + path: string; + count: number; + defaults: TDefaults; + sessions: TRow[]; +}; + +export type SessionsPatchResultBase = { + ok: true; + path: string; + key: string; + entry: TEntry; +}; diff --git a/ui/src/ui/types.ts b/ui/src/ui/types.ts index 9c6ff164308..852b3d69264 100644 --- a/ui/src/ui/types.ts +++ b/ui/src/ui/types.ts @@ -1,5 +1,10 @@ export type UpdateAvailable = import("../../../src/infra/update-startup.js").UpdateAvailable; import type { ConfigUiHints } from "../../../src/shared/config-ui-hints-types.js"; +import type { + GatewayAgentRow as SharedGatewayAgentRow, + SessionsListResultBase, + SessionsPatchResultBase, +} from "../../../src/shared/session-types.js"; export type { ConfigUiHints } from "../../../src/shared/config-ui-hints-types.js"; export type ChannelsStatusSnapshot = { @@ -314,17 +319,7 @@ export type GatewaySessionsDefaults = { contextTokens: number | null; }; -export type GatewayAgentRow = { - id: string; - name?: string; - identity?: { - name?: string; - theme?: string; - emoji?: string; - avatar?: string; - avatarUrl?: string; - }; -}; +export type GatewayAgentRow = SharedGatewayAgentRow; export type AgentsListResult = { defaultId: string; @@ -422,27 +417,16 @@ export type GatewaySessionRow = { contextTokens?: number; }; -export type SessionsListResult = { - ts: number; - path: string; - count: number; - defaults: GatewaySessionsDefaults; - sessions: GatewaySessionRow[]; -}; +export type SessionsListResult = SessionsListResultBase; -export type SessionsPatchResult = { - ok: true; - path: string; - key: string; - entry: { - sessionId: string; - updatedAt?: number; - thinkingLevel?: string; - verboseLevel?: string; - reasoningLevel?: string; - elevatedLevel?: string; - }; -}; +export type SessionsPatchResult = SessionsPatchResultBase<{ + sessionId: string; + updatedAt?: number; + thinkingLevel?: string; + verboseLevel?: string; + reasoningLevel?: string; + elevatedLevel?: string; +}>; export type { CostUsageDailyEntry,