Skip to content

@codesoul-co/hypha-inference / cache

模块用法

用于读写或协调缓存状态。Cache 模块公开 2 类、4 函数、3 接口、1 类型。

从包入口导入

ts
import {
  InferenceCacheManager,
  InferenceCacheOperationTimeoutError,
  hashContent,
  inferenceCacheScopeHash,
  isKvCacheExpired,
  runInferenceCacheOperation,
} from '@codesoul-co/hypha-inference';

import type {
  InferenceCacheManagerOptions,
  KvCacheCreateInput,
  PrefixCacheCreateInput,
  InferenceCacheOperation,
} from '@codesoul-co/hypha-inference';

使用要点

  • 4 个类型/接口用于应用代码、Adapter 或测试中的静态契约;请使用 import type,运行时不应依赖它们。
  • 2 个类提供可实例化的运行时实现;构造参数与公开方法在各自条目中完整列出。
  • 4 个函数是该模块的直接操作入口;每个 overload 的必需/可选参数与返回类型均在下方列出。

公共导出

Symbol种类签名说明
InferenceCacheManagernew InferenceCacheManager(options: InferenceCacheManagerOptions): InferenceCacheManagerInference Cache Manager 类,共公开 5 个构造函数或成员;精确签名见本条目的声明与成员表。
InferenceCacheOperationTimeoutErrornew InferenceCacheOperationTimeoutError(operation: InferenceCacheOperation, timeoutMs: number): InferenceCacheOperationTimeoutErrorInference Cache Operation Timeout Error 类,共公开 11 个构造函数或成员;精确签名见本条目的声明与成员表。
hashContent函数hashContent(content: string): stringHash Content 函数,提供 1 个公开调用签名;参数与返回类型见下表。
inferenceCacheScopeHash函数inferenceCacheScopeHash(scope: InferenceCacheScope | undefined): stringInference Cache Scope Hash 函数,提供 1 个公开调用签名;参数与返回类型见下表。
isKvCacheExpired函数isKvCacheExpired(ref: KvCacheRef, now?: Date): booleanIs Kv Cache Expired 函数,提供 1 个公开调用签名;参数与返回类型见下表。
runInferenceCacheOperation函数runInferenceCacheOperation<T>(operation: InferenceCacheOperation, task: () => Promise<T>, timeoutMs: number): Promise<T>Run Inference Cache Operation 函数,提供 1 个公开调用签名;参数与返回类型见下表。
InferenceCacheManagerOptions接口interface InferenceCacheManagerOptionsInference Cache Manager Options 接口,共包含 4 个公开字段或方法。
KvCacheCreateInput接口interface KvCacheCreateInputKv Cache Create Input 接口,共包含 7 个公开字段或方法。
PrefixCacheCreateInput接口interface PrefixCacheCreateInputPrefix Cache Create Input 接口,共包含 6 个公开字段或方法。
InferenceCacheOperation类型type InferenceCacheOperation = 'prefix_read' | 'prefix_write' | 'kv_read' | 'kv_write' | 'invalidate'Inference Cache Operation 公共类型别名;完整类型表达式见声明。

InferenceCacheManager

Inference Cache Manager 类,共公开 5 个构造函数或成员;精确签名见本条目的声明与成员表。

  • 种类: 类
  • 导入: import { InferenceCacheManager } from '@codesoul-co/hypha-inference';
  • 源码模块: cache

声明

text
export declare class InferenceCacheManager {
    constructor(options: InferenceCacheManagerOptions);
    putPrefix(input: PrefixCacheCreateInput): Promise<PrefixCacheRef>;
    getPrefix(ref: PrefixCacheRef): Promise<string | null>;
    putKv(input: KvCacheCreateInput, value: unknown): Promise<KvCacheRef>;
    getKv(ref: KvCacheRef): Promise<unknown | null>;
}

公开成员

成员种类签名说明
constructor构造函数(options: InferenceCacheManagerOptions): InferenceCacheManager创建该类的实例。
getKv方法getKv(ref: KvCacheRef): Promise<unknown | null>公开方法;参数与返回类型以签名列为准。
getPrefix方法getPrefix(ref: PrefixCacheRef): Promise<string | null>公开方法;参数与返回类型以签名列为准。
putKv方法putKv(input: KvCacheCreateInput, value: unknown): Promise<KvCacheRef>公开方法;参数与返回类型以签名列为准。
putPrefix方法putPrefix(input: PrefixCacheCreateInput): Promise<PrefixCacheRef>公开方法;参数与返回类型以签名列为准。

InferenceCacheOperationTimeoutError

Inference Cache Operation Timeout Error 类,共公开 11 个构造函数或成员;精确签名见本条目的声明与成员表。

  • 种类: 类
  • 导入: import { InferenceCacheOperationTimeoutError } from '@codesoul-co/hypha-inference';
  • 源码模块: cache

声明

text
export declare class InferenceCacheOperationTimeoutError extends Error {
    readonly operation: InferenceCacheOperation;
    readonly timeoutMs: number;
    readonly code = "INFERENCE_CACHE_OPERATION_TIMEOUT";
    constructor(operation: InferenceCacheOperation, timeoutMs: number);
}

公开成员

成员种类签名说明
cause属性cause?: unknown公开属性;类型、只读和可选状态以签名列为准。
code属性readonly code: "INFERENCE_CACHE_OPERATION_TIMEOUT"公开属性;类型、只读和可选状态以签名列为准。
constructor构造函数(operation: InferenceCacheOperation, timeoutMs: number): InferenceCacheOperationTimeoutError创建该类的实例。
message属性message: string公开属性;类型、只读和可选状态以签名列为准。
name属性name: string公开属性;类型、只读和可选状态以签名列为准。
operation属性readonly operation: InferenceCacheOperation公开属性;类型、只读和可选状态以签名列为准。
stack属性stack?: string公开属性;类型、只读和可选状态以签名列为准。
static captureStackTrace方法static captureStackTrace(targetObject: object, constructorOpt?: Function): voidCreates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called. js const myObject = {}; Error.captureStackTrace(myObject); myObject.stack; // Similar to `new Error().stack` The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}. The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace. The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance: js function a() { b(); } function b() { c(); } function c() { // Create an error without stack trace to avoid calculating the stack trace twice. const { stackTraceLimit } = Error; Error.stackTraceLimit = 0; const error = new Error(); Error.stackTraceLimit = stackTraceLimit; // Capture the stack trace above function b Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace throw error; } a();
static prepareStackTrace方法static prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any公开方法;参数与返回类型以签名列为准。
static stackTraceLimit属性static stackTraceLimit: numberThe Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)). The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames.
timeoutMs属性readonly timeoutMs: number公开属性;类型、只读和可选状态以签名列为准。

hashContent

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

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

声明

text
export declare function hashContent(content: string): string;

调用签名

text
hashContent(content: string): string

参数

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

返回值

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

inferenceCacheScopeHash

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

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

声明

text
export declare function inferenceCacheScopeHash(scope: InferenceCacheScope | undefined): string;

调用签名

text
inferenceCacheScopeHash(scope: InferenceCacheScope | undefined): string

参数

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

返回值

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

isKvCacheExpired

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

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

声明

text
export declare function isKvCacheExpired(ref: KvCacheRef, now?: Date): boolean;

调用签名

text
isKvCacheExpired(ref: KvCacheRef, now?: Date): boolean

参数

参数类型必需说明
refKvCacheRef必需参数;接受的值由类型列定义。
nowDate可选参数;接受的值由类型列定义。

返回值

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

runInferenceCacheOperation

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

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

声明

text
export declare function runInferenceCacheOperation<T>(operation: InferenceCacheOperation, task: () => Promise<T>, timeoutMs: number): Promise<T>;

调用签名

text
runInferenceCacheOperation<T>(operation: InferenceCacheOperation, task: () => Promise<T>, timeoutMs: number): Promise<T>

参数

参数类型必需说明
operationInferenceCacheOperation必需参数;接受的值由类型列定义。
task() => Promise<T>必需参数;接受的值由类型列定义。
timeoutMsnumber必需参数;接受的值由类型列定义。

返回值

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

InferenceCacheManagerOptions

Inference Cache Manager Options 接口,共包含 4 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { InferenceCacheManagerOptions } from '@codesoul-co/hypha-inference';
  • 源码模块: cache

声明

text
export interface InferenceCacheManagerOptions {
    prefixCache: PrefixCacheProvider;
    kvCache: KvCacheProvider;
    now?: () => Date;
    operationTimeoutMs?: number;
}

契约成员

成员种类签名说明
kvCache属性kvCache: KvCacheProvider公开属性;类型、只读和可选状态以签名列为准。
now方法now?(): Date公开方法;参数与返回类型以签名列为准。
operationTimeoutMs属性operationTimeoutMs?: number公开属性;类型、只读和可选状态以签名列为准。
prefixCache属性prefixCache: PrefixCacheProvider公开属性;类型、只读和可选状态以签名列为准。

KvCacheCreateInput

Kv Cache Create Input 接口,共包含 7 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { KvCacheCreateInput } from '@codesoul-co/hypha-inference';
  • 源码模块: cache

声明

text
export interface KvCacheCreateInput {
    id: string;
    provider: string;
    modelAlias: string;
    scope: KvCacheScope;
    cacheScope?: InferenceCacheScope;
    ttlMs?: number;
    metadata?: Record<string, unknown>;
}

契约成员

成员种类签名说明
cacheScope属性cacheScope?: InferenceCacheScope公开属性;类型、只读和可选状态以签名列为准。
id属性id: string公开属性;类型、只读和可选状态以签名列为准。
metadata属性metadata?: Record<string, unknown>公开属性;类型、只读和可选状态以签名列为准。
modelAlias属性modelAlias: string公开属性;类型、只读和可选状态以签名列为准。
provider属性provider: string公开属性;类型、只读和可选状态以签名列为准。
scope属性scope: KvCacheScope公开属性;类型、只读和可选状态以签名列为准。
ttlMs属性ttlMs?: number公开属性;类型、只读和可选状态以签名列为准。

PrefixCacheCreateInput

Prefix Cache Create Input 接口,共包含 6 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { PrefixCacheCreateInput } from '@codesoul-co/hypha-inference';
  • 源码模块: cache

声明

text
export interface PrefixCacheCreateInput {
    id: string;
    version: string;
    content: string;
    tokenCount?: number;
    cacheScope?: InferenceCacheScope;
    metadata?: Record<string, unknown>;
}

契约成员

成员种类签名说明
cacheScope属性cacheScope?: InferenceCacheScope公开属性;类型、只读和可选状态以签名列为准。
content属性content: string公开属性;类型、只读和可选状态以签名列为准。
id属性id: string公开属性;类型、只读和可选状态以签名列为准。
metadata属性metadata?: Record<string, unknown>公开属性;类型、只读和可选状态以签名列为准。
tokenCount属性tokenCount?: number公开属性;类型、只读和可选状态以签名列为准。
version属性version: string公开属性;类型、只读和可选状态以签名列为准。

InferenceCacheOperation

Inference Cache Operation 公共类型别名;完整类型表达式见声明。

  • 种类: 类型
  • 导入: import type { InferenceCacheOperation } from '@codesoul-co/hypha-inference';
  • 源码模块: cache

声明

text
export type InferenceCacheOperation = 'prefix_read' | 'prefix_write' | 'kv_read' | 'kv_write' | 'invalidate';