Skip to content

@codesoul-co/hypha-serving-cache / schemas

模块用法

用于声明并运行时校验契约。Schemas 模块公开 6 常量、4 函数。

从包入口导入

ts
import {
  cachedModelResponseProjectionSchema,
  cacheEntrySchema,
  cachePolicySchema,
  cacheScopeSchema,
  servingCacheJsonSchemas,
  servingCacheJsonValueSchema,
  validateCachedModelResponseProjection,
  validateCacheEntry,
} from '@codesoul-co/hypha-serving-cache';

// 完整导出列表见下方。

使用要点

  • 4 个函数是该模块的直接操作入口;每个 overload 的必需/可选参数与返回类型均在下方列出。
  • 6 个常量/枚举提供稳定值、Schema、Definition 或默认配置;应复用这些导出,避免在应用中复制内部值。

运行时校验示例

ts
import { cachedModelResponseProjectionSchema } from '@codesoul-co/hypha-serving-cache';

declare function loadExternalInput(): unknown;
const input: unknown = loadExternalInput();
const parsed = cachedModelResponseProjectionSchema.parse(input);

配置、网络请求或持久化数据等不可信输入应先通过 Runtime Schema,再传给只接受已校验契约的函数或类。

公共导出

Symbol种类签名说明
cachedModelResponseProjectionSchema常量const cachedModelResponseProjectionSchema: z.ZodObject<{ schemaVersion: z.ZodLiteral<"1.0">; providerId: z.ZodOptional<z.ZodString>; model: z.ZodOptional<z.ZodString>; content: z.ZodString; toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{ id: z.ZodString; toolId: z.ZodString; arguments: z.ZodType<unknown, z.ZodTypeDef, unknown>; }, "strict", z.ZodTypeAny, { id: string; toolId: string; arguments?: unknown; }, { id...Cached Model Response Projection 的运行时 Schema。
cacheEntrySchema常量const cacheEntrySchema: z.ZodObject<{ schemaVersion: z.ZodOptional<z.ZodLiteral<"1.0">>; keyVersion: z.ZodOptional<z.ZodLiteral<"1">>; key: z.ZodString; value: z.ZodType<unknown, z.ZodTypeDef, unknown>; createdAt: z.ZodNumber; expiresAt: z.ZodOptional<z.ZodNumber>; sizeBytes: z.ZodOptional<z.ZodNumber>; metadata: z.ZodOptional<z.ZodObject<{ provider: z.ZodString; model: z.ZodString; cacheType: z.ZodEnum<["exact", ...Cache Entry 的运行时 Schema。
cachePolicySchema常量const cachePolicySchema: z.ZodObject<{ enabled: z.ZodBoolean; mode: z.ZodEnum<["off", "read", "write", "readwrite"]>; ttlMs: z.ZodOptional<z.ZodNumber>; respectNoCache: z.ZodOptional<z.ZodBoolean>; failureMode: z.ZodOptional<z.ZodEnum<["bypass", "strict"]>>; scopeRequirement: z.ZodOptional<z.ZodEnum<["none", "user", "session"]>>; operationTimeoutMs: z.ZodOptional<z.ZodNumber>; singleflight: z.ZodOptional<z.ZodBool...Cache Policy 的运行时 Schema。
cacheScopeSchema常量const cacheScopeSchema: z.ZodObject<{ tenantId: z.ZodOptional<z.ZodString>; userId: z.ZodOptional<z.ZodString>; projectId: z.ZodOptional<z.ZodString>; sessionId: z.ZodOptional<z.ZodString>; domainPackId: z.ZodOptional<z.ZodString>; }, "strict", z.ZodTypeAny, { tenantId?: string | undefined; userId?: string | undefined; projectId?: string | undefined; sessionId?: string | undefined; domainPackId?: string | undefine...Cache Scope 的运行时 Schema。
servingCacheJsonSchemas常量const servingCacheJsonSchemas: { readonly CacheScope: { readonly type: "object"; readonly properties: { [k: string]: { type: string; minLength: number; }; }; readonly additionalProperties: false; }; readonly CachedModelResponseProjection: { readonly type: "object"; readonly required: readonly ["schemaVersion", "content"]; readonly properties: { readonly schemaVersion: { readonly const: "1.0"; }; readonly providerI...schemas 模块导出的 Serving Cache JSON Schemas 常量。
servingCacheJsonValueSchema常量const servingCacheJsonValueSchema: z.ZodType<unknown, z.ZodTypeDef, unknown>Serving Cache JSON Value 的运行时 Schema。
validateCachedModelResponseProjection函数validateCachedModelResponseProjection(input: unknown): CachedModelResponseProjectionValidate Cached Model Response Projection 函数,提供 1 个公开调用签名;参数与返回类型见下表。
validateCacheEntry函数validateCacheEntry<T = unknown>(input: unknown): CacheEntry<T>Validate Cache Entry 函数,提供 1 个公开调用签名;参数与返回类型见下表。
validateCachePolicy函数validateCachePolicy(input: unknown): CachePolicyValidate Cache Policy 函数,提供 1 个公开调用签名;参数与返回类型见下表。
validateServingCacheJsonValue函数validateServingCacheJsonValue(input: unknown): unknownValidate Serving Cache JSON Value 函数,提供 1 个公开调用签名;参数与返回类型见下表。

cachedModelResponseProjectionSchema

Cached Model Response Projection 的运行时 Schema。

  • 种类: 常量
  • 导入: import { cachedModelResponseProjectionSchema } from '@codesoul-co/hypha-serving-cache';
  • 源码模块: schemas

声明

text
export declare const cachedModelResponseProjectionSchema: z.ZodObject<{ schemaVersion: z.ZodLiteral<"1.0">; providerId: z.ZodOptional<z.ZodString>; model: z.ZodOptional<z.ZodString>; content: z.ZodString; toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{ id: z.ZodString; toolId: z.ZodString; arguments: z.ZodType<unknown, z.ZodTypeDef, unknown>; }, "strict", z.ZodTypeAny, { id: string; toolId: string; arguments?: unknown; }, { id: string; toolId: string; arguments?: unknown; }>, "many">>; usage: z.ZodOptional<z.ZodObject<{ inputTokens: z.ZodOptional<z.ZodNumber>; outputTokens: z.ZodOptional<z.ZodNumber>; totalTokens: z.ZodOptional<z.ZodNumber>; cacheHitTokens: z.ZodOptional<z.ZodNumber>; cacheMissTokens: z.ZodOptional<z.ZodNumber>; }, "strict", z.ZodTypeAny, { inputTokens?: number | undefined; outputTokens?: number | undefined; totalTokens?: number | undefined; cacheHitTokens?: number | undefined; cacheMissTokens?: number | undefined; }, { inputTokens?: number | undefined; outputTokens?: number | undefined; totalTokens?: number | undefined; cacheHitTokens?: number | undefined; cacheMissTokens?: number | undefined; }>>; }, "strict", z.ZodTypeAny, { schemaVersion: "1.0"; content: string; providerId?: string | undefined; model?: string | undefined; toolCalls?: { id: string; toolId: string; arguments?: unknown; }[] | undefined; usage?: { inputTokens?: number | undefined; outputTokens?: number | undefined; totalTokens?: number | undefined; cacheHitTokens?: number | undefined; cacheMissTokens?: number | undefined; } | undefined; }, { schemaVersion: "1.0"; content: string; providerId?: string | undefined; model?: string | undefined; toolCalls?: { id: string; toolId: string; arguments?: unknown; }[] | undefined; usage?: { inputTokens?: number | undefined; outputTokens?: number | undefined; totalTokens?: number | undefined; cacheHitTokens?: number | undefined; cacheMissTokens?: number | undefined; } | undefined; }>;

cacheEntrySchema

Cache Entry 的运行时 Schema。

  • 种类: 常量
  • 导入: import { cacheEntrySchema } from '@codesoul-co/hypha-serving-cache';
  • 源码模块: schemas

声明

text
// 精确类型由包入口解析;完整编译器展开定义见源码链接。
export declare const cacheEntrySchema: (typeof import('@codesoul-co/hypha-serving-cache'))['cacheEntrySchema'];

该常量的编译器展开类型已压缩;它的公共名称、顶层类型与源码位置仍保留在本条目中。相关输入/输出字段请使用同模块导出的接口、类型或 Runtime Schema。

cachePolicySchema

Cache Policy 的运行时 Schema。

  • 种类: 常量
  • 导入: import { cachePolicySchema } from '@codesoul-co/hypha-serving-cache';
  • 源码模块: schemas

声明

text
export declare const cachePolicySchema: z.ZodObject<{ enabled: z.ZodBoolean; mode: z.ZodEnum<["off", "read", "write", "readwrite"]>; ttlMs: z.ZodOptional<z.ZodNumber>; respectNoCache: z.ZodOptional<z.ZodBoolean>; failureMode: z.ZodOptional<z.ZodEnum<["bypass", "strict"]>>; scopeRequirement: z.ZodOptional<z.ZodEnum<["none", "user", "session"]>>; operationTimeoutMs: z.ZodOptional<z.ZodNumber>; singleflight: z.ZodOptional<z.ZodBoolean>; maxEntryBytes: z.ZodOptional<z.ZodNumber>; circuitBreaker: z.ZodOptional<z.ZodObject<{ failureThreshold: z.ZodNumber; resetTimeoutMs: z.ZodNumber; }, "strict", z.ZodTypeAny, { failureThreshold: number; resetTimeoutMs: number; }, { failureThreshold: number; resetTimeoutMs: number; }>>; }, "strict", z.ZodTypeAny, { enabled: boolean; mode: "off" | "read" | "write" | "readwrite"; ttlMs?: number | undefined; respectNoCache?: boolean | undefined; failureMode?: "strict" | "bypass" | undefined; scopeRequirement?: "none" | "user" | "session" | undefined; operationTimeoutMs?: number | undefined; singleflight?: boolean | undefined; maxEntryBytes?: number | undefined; circuitBreaker?: { failureThreshold: number; resetTimeoutMs: number; } | undefined; }, { enabled: boolean; mode: "off" | "read" | "write" | "readwrite"; ttlMs?: number | undefined; respectNoCache?: boolean | undefined; failureMode?: "strict" | "bypass" | undefined; scopeRequirement?: "none" | "user" | "session" | undefined; operationTimeoutMs?: number | undefined; singleflight?: boolean | undefined; maxEntryBytes?: number | undefined; circuitBreaker?: { failureThreshold: number; resetTimeoutMs: number; } | undefined; }>;

cacheScopeSchema

Cache Scope 的运行时 Schema。

  • 种类: 常量
  • 导入: import { cacheScopeSchema } from '@codesoul-co/hypha-serving-cache';
  • 源码模块: schemas

声明

text
export declare const cacheScopeSchema: z.ZodObject<{ tenantId: z.ZodOptional<z.ZodString>; userId: z.ZodOptional<z.ZodString>; projectId: z.ZodOptional<z.ZodString>; sessionId: z.ZodOptional<z.ZodString>; domainPackId: z.ZodOptional<z.ZodString>; }, "strict", z.ZodTypeAny, { tenantId?: string | undefined; userId?: string | undefined; projectId?: string | undefined; sessionId?: string | undefined; domainPackId?: string | undefined; }, { tenantId?: string | undefined; userId?: string | undefined; projectId?: string | undefined; sessionId?: string | undefined; domainPackId?: string | undefined; }>;

servingCacheJsonSchemas

schemas 模块导出的 Serving Cache JSON Schemas 常量。

  • 种类: 常量
  • 导入: import { servingCacheJsonSchemas } from '@codesoul-co/hypha-serving-cache';
  • 源码模块: schemas

声明

text
export declare const servingCacheJsonSchemas: { readonly CacheScope: { readonly type: "object"; readonly properties: { [k: string]: { type: string; minLength: number; }; }; readonly additionalProperties: false; }; readonly CachedModelResponseProjection: { readonly type: "object"; readonly required: readonly ["schemaVersion", "content"]; readonly properties: { readonly schemaVersion: { readonly const: "1.0"; }; readonly providerId: { readonly type: "string"; readonly minLength: 1; }; readonly model: { readonly type: "string"; readonly minLength: 1; }; readonly content: {}; readonly toolCalls: { readonly type: "array"; }; readonly usage: { readonly type: "object"; }; }; readonly additionalProperties: false; }; readonly CacheEntry: { readonly type: "object"; readonly required: readonly ["key", "value", "createdAt"]; readonly properties: { readonly schemaVersion: { readonly const: "1.0"; }; readonly keyVersion: { readonly const: "1"; }; readonly key: { readonly type: "string"; readonly minLength: 1; }; readonly value: {}; readonly createdAt: { readonly type: "integer"; readonly minimum: 0; }; readonly expiresAt: { readonly type: "integer"; readonly minimum: 0; }; readonly sizeBytes: { readonly type: "integer"; readonly minimum: 0; }; readonly metadata: { readonly type: "object"; }; }; readonly additionalProperties: false; }; readonly CachePolicy: { readonly type: "object"; readonly required: readonly ["enabled", "mode"]; readonly properties: { readonly enabled: { readonly type: "boolean"; }; readonly mode: { readonly enum: readonly ["off", "read", "write", "readwrite"]; }; readonly ttlMs: { readonly type: "number"; readonly exclusiveMinimum: 0; }; readonly respectNoCache: { readonly type: "boolean"; }; readonly failureMode: { readonly enum: readonly ["bypass", "strict"]; }; readonly scopeRequirement: { readonly enum: readonly ["none", "user", "session"]; }; readonly operationTimeoutMs: { readonly type: "integer"; readonly minimum: 1; }; readonly singleflight: { readonly type: "boolean"; }; readonly maxEntryBytes: { readonly type: "integer"; readonly minimum: 1; }; readonly circuitBreaker: { readonly type: "object"; }; }; readonly additionalProperties: false; }; };

servingCacheJsonValueSchema

Serving Cache JSON Value 的运行时 Schema。

  • 种类: 常量
  • 导入: import { servingCacheJsonValueSchema } from '@codesoul-co/hypha-serving-cache';
  • 源码模块: schemas

声明

text
export declare const servingCacheJsonValueSchema: z.ZodType<unknown, z.ZodTypeDef, unknown>;

validateCachedModelResponseProjection

Validate Cached Model Response Projection 函数,提供 1 个公开调用签名;参数与返回类型见下表。

  • 种类: 函数
  • 导入: import { validateCachedModelResponseProjection } from '@codesoul-co/hypha-serving-cache';
  • 源码模块: schemas

声明

text
export declare function validateCachedModelResponseProjection(input: unknown): CachedModelResponseProjection;

调用签名

text
validateCachedModelResponseProjection(input: unknown): CachedModelResponseProjection

参数

参数类型必需说明
inputunknown必需参数;接受的值由类型列定义。

返回值

  • 类型: CachedModelResponseProjection
  • 说明: 返回值契约由上述类型定义。

validateCacheEntry

Validate Cache Entry 函数,提供 1 个公开调用签名;参数与返回类型见下表。

  • 种类: 函数
  • 导入: import { validateCacheEntry } from '@codesoul-co/hypha-serving-cache';
  • 源码模块: schemas

声明

text
export declare function validateCacheEntry<T = unknown>(input: unknown): CacheEntry<T>;

调用签名

text
validateCacheEntry<T = unknown>(input: unknown): CacheEntry<T>

参数

参数类型必需说明
inputunknown必需参数;接受的值由类型列定义。

返回值

  • 类型: CacheEntry<T>
  • 说明: 返回值契约由上述类型定义。

validateCachePolicy

Validate Cache Policy 函数,提供 1 个公开调用签名;参数与返回类型见下表。

  • 种类: 函数
  • 导入: import { validateCachePolicy } from '@codesoul-co/hypha-serving-cache';
  • 源码模块: schemas

声明

text
export declare function validateCachePolicy(input: unknown): CachePolicy;

调用签名

text
validateCachePolicy(input: unknown): CachePolicy

参数

参数类型必需说明
inputunknown必需参数;接受的值由类型列定义。

返回值

  • 类型: CachePolicy
  • 说明: 返回值契约由上述类型定义。

validateServingCacheJsonValue

Validate Serving Cache JSON Value 函数,提供 1 个公开调用签名;参数与返回类型见下表。

  • 种类: 函数
  • 导入: import { validateServingCacheJsonValue } from '@codesoul-co/hypha-serving-cache';
  • 源码模块: schemas

声明

text
export declare function validateServingCacheJsonValue(input: unknown): unknown;

调用签名

text
validateServingCacheJsonValue(input: unknown): unknown

参数

参数类型必需说明
inputunknown必需参数;接受的值由类型列定义。

返回值

  • 类型: unknown
  • 说明: 返回值契约由上述类型定义。