Skip to content

@codesoul-co/hypha-tools / adapter-factory

模块用法

用于把外部或本地 Provider 绑定到 Hypha Port。Adapter factory 模块公开 2 类、3 常量、3 函数、8 接口、1 类型。

从包入口导入

ts
import {
  LoadedToolAdapterProfiles,
  ToolAdapterFactoryRegistry,
  toolAdapterKinds,
  toolAdapterProfileSchema,
  toolAdapterProfilesDocumentSchema,
  loadToolAdapterProfiles,
  parseToolAdapterProfilesDocument,
  registerConcreteToolAdapterFactories,
} from '@codesoul-co/hypha-tools';

import type {
  ConcreteToolAdapterFactoryDependencies,
  LoadedToolAdapterProfile,
  ToolAdapterFactory,
  ToolAdapterFactoryInput,
  ToolAdapterFactoryRegistryOptions,
  ToolAdapterProfile,
  ToolAdapterProfilesDocument,
  ToolSpecReference,
} from '@codesoul-co/hypha-tools';

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

使用要点

  • 9 个类型/接口用于应用代码、Adapter 或测试中的静态契约;请使用 import type,运行时不应依赖它们。
  • 2 个类提供可实例化的运行时实现;构造参数与公开方法在各自条目中完整列出。
  • 3 个函数是该模块的直接操作入口;每个 overload 的必需/可选参数与返回类型均在下方列出。
  • 3 个常量/枚举提供稳定值、Schema、Definition 或默认配置;应复用这些导出,避免在应用中复制内部值。

运行时校验示例

ts
import { toolAdapterProfileSchema } from '@codesoul-co/hypha-tools';

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

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

公共导出

Symbol种类签名说明
LoadedToolAdapterProfilesnew LoadedToolAdapterProfiles(entries: Map<string, LoadedToolAdapterProfile>): LoadedToolAdapterProfilesLoaded Tool Adapter Profiles 类,共公开 5 个构造函数或成员;精确签名见本条目的声明与成员表。
ToolAdapterFactoryRegistrynew ToolAdapterFactoryRegistry(options: ToolAdapterFactoryRegistryOptions): ToolAdapterFactoryRegistryCreates adapters from declarative profiles without allowing profiles to smuggle executable factories or plaintext credentials through configuration.
toolAdapterKinds常量const toolAdapterKinds: readonly ["local_function", "http", "plugin", "mcp_stdio", "mcp_streamable_http", "execution"]adapter-factory 模块导出的 Tool Adapter Kinds 常量。
toolAdapterProfileSchema常量const toolAdapterProfileSchema: z.ZodEffects<z.ZodObject<{ id: z.ZodString; kind: z.ZodEnum<["local_function", "http", "plugin", "mcp_stdio", "mcp_streamable_http", "execution"]>; required: z.ZodDefault<z.ZodBoolean>; toolSpecRef: z.ZodObject<{ id: z.ZodString; version: z.ZodOptional<z.ZodString>; revision: z.ZodOptional<z.ZodString>; }, "strict", z.ZodTypeAny, { id: string; version?: string | undefined; revision?...Tool Adapter Profile 的运行时 Schema。
toolAdapterProfilesDocumentSchema常量const toolAdapterProfilesDocumentSchema: z.ZodObject<{ profiles: z.ZodArray<z.ZodEffects<z.ZodObject<{ id: z.ZodString; kind: z.ZodEnum<["local_function", "http", "plugin", "mcp_stdio", "mcp_streamable_http", "execution"]>; required: z.ZodDefault<z.ZodBoolean>; toolSpecRef: z.ZodObject<{ id: z.ZodString; version: z.ZodOptional<z.ZodString>; revision: z.ZodOptional<z.ZodString>; }, "strict", z.ZodTypeAny, { id: str...Tool Adapter Profiles Document 的运行时 Schema。
loadToolAdapterProfiles函数loadToolAdapterProfiles(input: unknown, registry: ToolAdapterFactoryRegistry): Promise<LoadedToolAdapterProfiles>Load Tool Adapter Profiles 函数,提供 1 个公开调用签名;参数与返回类型见下表。
parseToolAdapterProfilesDocument函数parseToolAdapterProfilesDocument(input: unknown): ToolAdapterProfilesDocumentParse Tool Adapter Profiles Document 函数,提供 1 个公开调用签名;参数与返回类型见下表。
registerConcreteToolAdapterFactories函数registerConcreteToolAdapterFactories(registry: ToolAdapterFactoryRegistry, dependencies?: ConcreteToolAdapterFactoryDependencies): voidRegisters the complete declarative factory surface used by server composition.
ConcreteToolAdapterFactoryDependencies接口interface ConcreteToolAdapterFactoryDependenciesConcrete Tool Adapter Factory Dependencies 接口,共包含 6 个公开字段或方法。
LoadedToolAdapterProfile接口interface LoadedToolAdapterProfileLoaded Tool Adapter Profile 接口,共包含 5 个公开字段或方法。
ToolAdapterFactory接口interface ToolAdapterFactoryTool Adapter Factory 接口,共包含 2 个公开字段或方法。
ToolAdapterFactoryInput接口interface ToolAdapterFactoryInputTool Adapter Factory Input 接口,共包含 4 个公开字段或方法。
ToolAdapterFactoryRegistryOptions接口interface ToolAdapterFactoryRegistryOptionsTool Adapter Factory Registry Options 接口,共包含 2 个公开字段或方法。
ToolAdapterProfile接口interface ToolAdapterProfileTool Adapter Profile 接口,共包含 9 个公开字段或方法。
ToolAdapterProfilesDocument接口interface ToolAdapterProfilesDocumentTool Adapter Profiles Document 接口,共包含 1 个公开字段或方法。
ToolSpecReference接口interface ToolSpecReferenceTool Spec Reference 接口,共包含 3 个公开字段或方法。
ToolAdapterKind类型type ToolAdapterKind = (typeof toolAdapterKinds)[number]Tool Adapter Kind 公共类型别名;完整类型表达式见声明。

LoadedToolAdapterProfiles

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

  • 种类: 类
  • 导入: import { LoadedToolAdapterProfiles } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export declare class LoadedToolAdapterProfiles {
    constructor(entries: Map<string, LoadedToolAdapterProfile>);
    list(): LoadedToolAdapterProfile[];
    get(profileId: string): LoadedToolAdapterProfile | undefined;
    health(): Promise<Record<string, Awaited<ReturnType<ToolAdapter['health']>>>>;
    close(): Promise<void>;
}

公开成员

成员种类签名说明
close方法close(): Promise<void>公开方法;参数与返回类型以签名列为准。
constructor构造函数(entries: Map<string, LoadedToolAdapterProfile>): LoadedToolAdapterProfiles创建该类的实例。
get方法get(profileId: string): LoadedToolAdapterProfile | undefined公开方法;参数与返回类型以签名列为准。
health方法health(): Promise<Record<string, Awaited<ReturnType<ToolAdapter["health"]>>>>公开方法;参数与返回类型以签名列为准。
list方法list(): LoadedToolAdapterProfile[]公开方法;参数与返回类型以签名列为准。

ToolAdapterFactoryRegistry

Creates adapters from declarative profiles without allowing profiles to smuggle executable factories or plaintext credentials through configuration.

  • 种类: 类
  • 导入: import { ToolAdapterFactoryRegistry } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export declare class ToolAdapterFactoryRegistry {
    constructor(options: ToolAdapterFactoryRegistryOptions);
    register(factory: ToolAdapterFactory): void;
    create(untrustedProfile: ToolAdapterProfile): Promise<{
            profile: ToolAdapterProfile;
            toolSpec: ToolSpec;
            adapter: ToolAdapter;
        }>;
}

公开成员

成员种类签名说明
constructor构造函数(options: ToolAdapterFactoryRegistryOptions): ToolAdapterFactoryRegistry创建该类的实例。
create方法create(untrustedProfile: ToolAdapterProfile): Promise<{ profile: ToolAdapterProfile; toolSpec: ToolSpec; adapter: ToolAdapter; }>公开方法;参数与返回类型以签名列为准。
register方法register(factory: ToolAdapterFactory): void公开方法;参数与返回类型以签名列为准。

toolAdapterKinds

adapter-factory 模块导出的 Tool Adapter Kinds 常量。

  • 种类: 常量
  • 导入: import { toolAdapterKinds } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export declare const toolAdapterKinds: readonly ["local_function", "http", "plugin", "mcp_stdio", "mcp_streamable_http", "execution"];

toolAdapterProfileSchema

Tool Adapter Profile 的运行时 Schema。

  • 种类: 常量
  • 导入: import { toolAdapterProfileSchema } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

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

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

toolAdapterProfilesDocumentSchema

Tool Adapter Profiles Document 的运行时 Schema。

  • 种类: 常量
  • 导入: import { toolAdapterProfilesDocumentSchema } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

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

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

loadToolAdapterProfiles

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

  • 种类: 函数
  • 导入: import { loadToolAdapterProfiles } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export declare function loadToolAdapterProfiles(input: unknown, registry: ToolAdapterFactoryRegistry): Promise<LoadedToolAdapterProfiles>;

调用签名

text
loadToolAdapterProfiles(input: unknown, registry: ToolAdapterFactoryRegistry): Promise<LoadedToolAdapterProfiles>

参数

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

返回值

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

parseToolAdapterProfilesDocument

Parse Tool Adapter Profiles Document 函数,提供 1 个公开调用签名;参数与返回类型见下表。

  • 种类: 函数
  • 导入: import { parseToolAdapterProfilesDocument } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export declare function parseToolAdapterProfilesDocument(input: unknown): ToolAdapterProfilesDocument;

调用签名

text
parseToolAdapterProfilesDocument(input: unknown): ToolAdapterProfilesDocument

参数

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

返回值

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

registerConcreteToolAdapterFactories

Registers the complete declarative factory surface used by server composition.

  • 种类: 函数
  • 导入: import { registerConcreteToolAdapterFactories } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export declare function registerConcreteToolAdapterFactories(registry: ToolAdapterFactoryRegistry, dependencies?: ConcreteToolAdapterFactoryDependencies): void;

调用签名

text
registerConcreteToolAdapterFactories(registry: ToolAdapterFactoryRegistry, dependencies?: ConcreteToolAdapterFactoryDependencies): void

Registers the complete declarative factory surface used by server composition.

参数

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

返回值

  • 类型: void
  • 说明: 不返回值。

ConcreteToolAdapterFactoryDependencies

Concrete Tool Adapter Factory Dependencies 接口,共包含 6 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { ConcreteToolAdapterFactoryDependencies } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export interface ConcreteToolAdapterFactoryDependencies {
    localFunctions?: Readonly<Record<string, ToolHandler>>;
    plugins?: Readonly<Record<string, ToolHandler>>;
    mcpPort?: MCPToolInvocationPort;
    prepareMCPConnection?(input: ToolAdapterFactoryInput): Promise<{
        port: MCPToolInvocationPort;
        close?(): Promise<void>;
    }>;
    createExecutionAdapter?(input: ToolAdapterFactoryInput): Promise<ToolAdapter>;
    fetch?: typeof fetch;
}

契约成员

成员种类签名说明
createExecutionAdapter方法createExecutionAdapter?(input: ToolAdapterFactoryInput): Promise<ToolAdapter>公开方法;参数与返回类型以签名列为准。
fetch方法fetch?(input: RequestInfo | URL, init?: RequestInit): Promise<Response> | fetch?(input: string | URL | Request, init?: RequestInit): Promise<Response>公开方法;参数与返回类型以签名列为准。
localFunctions属性localFunctions?: Readonly<Record<string, ToolHandler>>公开属性;类型、只读和可选状态以签名列为准。
mcpPort属性mcpPort?: MCPToolInvocationPort公开属性;类型、只读和可选状态以签名列为准。
plugins属性plugins?: Readonly<Record<string, ToolHandler>>公开属性;类型、只读和可选状态以签名列为准。
prepareMCPConnection方法prepareMCPConnection?(input: ToolAdapterFactoryInput): Promise<{ port: MCPToolInvocationPort; close?(): Promise<void>; }>公开方法;参数与返回类型以签名列为准。

LoadedToolAdapterProfile

Loaded Tool Adapter Profile 接口,共包含 5 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { LoadedToolAdapterProfile } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export interface LoadedToolAdapterProfile {
    profile: ToolAdapterProfile;
    toolSpec?: ToolSpec;
    adapter?: ToolAdapter;
    status: 'ready' | 'degraded';
    error?: string;
}

契约成员

成员种类签名说明
adapter属性adapter?: ToolAdapter<unknown, unknown>公开属性;类型、只读和可选状态以签名列为准。
error属性error?: string公开属性;类型、只读和可选状态以签名列为准。
profile属性profile: ToolAdapterProfile公开属性;类型、只读和可选状态以签名列为准。
status属性status: "degraded" | "ready"公开属性;类型、只读和可选状态以签名列为准。
toolSpec属性toolSpec?: ToolSpec公开属性;类型、只读和可选状态以签名列为准。

ToolAdapterFactory

Tool Adapter Factory 接口,共包含 2 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { ToolAdapterFactory } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export interface ToolAdapterFactory {
    readonly kind: ToolAdapterKind;
    create(input: ToolAdapterFactoryInput): Promise<ToolAdapter>;
}

契约成员

成员种类签名说明
create方法create(input: ToolAdapterFactoryInput): Promise<ToolAdapter>公开方法;参数与返回类型以签名列为准。
kind属性readonly kind: "http" | "execution" | "plugin" | "local_function" | "mcp_stdio" | "mcp_streamable_http"公开属性;类型、只读和可选状态以签名列为准。

ToolAdapterFactoryInput

Tool Adapter Factory Input 接口,共包含 4 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { ToolAdapterFactoryInput } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export interface ToolAdapterFactoryInput {
    profile: ToolAdapterProfile;
    toolSpec: ToolSpec;
    resolveCredential(): Promise<string | null>;
    acquireCredential(): Promise<CredentialLease | null>;
}

契约成员

成员种类签名说明
acquireCredential方法acquireCredential(): Promise<CredentialLease | null>公开方法;参数与返回类型以签名列为准。
profile属性profile: ToolAdapterProfile公开属性;类型、只读和可选状态以签名列为准。
resolveCredential方法resolveCredential(): Promise<string | null>公开方法;参数与返回类型以签名列为准。
toolSpec属性toolSpec: ToolSpec公开属性;类型、只读和可选状态以签名列为准。

ToolAdapterFactoryRegistryOptions

Tool Adapter Factory Registry Options 接口,共包含 2 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { ToolAdapterFactoryRegistryOptions } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export interface ToolAdapterFactoryRegistryOptions {
    resolveToolSpec(reference: ToolSpecReference): Promise<ToolSpec | null>;
    secretResolver?: ToolSecretResolver;
}

契约成员

成员种类签名说明
resolveToolSpec方法resolveToolSpec(reference: ToolSpecReference): Promise<ToolSpec | null>公开方法;参数与返回类型以签名列为准。
secretResolver属性secretResolver?: ToolSecretResolver公开属性;类型、只读和可选状态以签名列为准。

ToolAdapterProfile

Tool Adapter Profile 接口,共包含 9 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { ToolAdapterProfile } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export interface ToolAdapterProfile {
    id: string;
    kind: ToolAdapterKind;
    required?: boolean;
    toolSpecRef: ToolSpecReference;
    endpoint?: string;
    credentialRef?: string;
    requiredCapabilities?: Array<keyof ToolAdapterCapabilities>;
    binding?: {
        localFunctionId?: string;
        pluginId?: string;
        executionPortRef?: string;
        mcpServerId?: string;
        mcpCapabilityId?: string;
        mcpConnectionProfileRef?: string;
    };
    /** @deprecated Use the typed binding object. */
    config?: Record<string, unknown>;
}

契约成员

成员种类签名说明
binding属性binding?: { localFunctionId?: string; pluginId?: string; executionPortRef?: string; mcpServerId?: string; mcpCapabilityId?: string; mcpConnectionProfileRef?: string; }公开属性;类型、只读和可选状态以签名列为准。
config属性config?: Record<string, unknown>公开属性;类型、只读和可选状态以签名列为准。
credentialRef属性credentialRef?: string公开属性;类型、只读和可选状态以签名列为准。
endpoint属性endpoint?: string公开属性;类型、只读和可选状态以签名列为准。
id属性id: string公开属性;类型、只读和可选状态以签名列为准。
kind属性kind: "http" | "execution" | "plugin" | "local_function" | "mcp_stdio" | "mcp_streamable_http"公开属性;类型、只读和可选状态以签名列为准。
required属性required?: boolean公开属性;类型、只读和可选状态以签名列为准。
requiredCapabilities属性requiredCapabilities?: (keyof ToolAdapterCapabilities)[]公开属性;类型、只读和可选状态以签名列为准。
toolSpecRef属性toolSpecRef: ToolSpecReference公开属性;类型、只读和可选状态以签名列为准。

ToolAdapterProfilesDocument

Tool Adapter Profiles Document 接口,共包含 1 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { ToolAdapterProfilesDocument } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export interface ToolAdapterProfilesDocument {
    profiles: ToolAdapterProfile[];
}

契约成员

成员种类签名说明
profiles属性profiles: ToolAdapterProfile[]公开属性;类型、只读和可选状态以签名列为准。

ToolSpecReference

Tool Spec Reference 接口,共包含 3 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { ToolSpecReference } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export interface ToolSpecReference {
    id: string;
    version?: string;
    revision?: string;
}

契约成员

成员种类签名说明
id属性id: string公开属性;类型、只读和可选状态以签名列为准。
revision属性revision?: string公开属性;类型、只读和可选状态以签名列为准。
version属性version?: string公开属性;类型、只读和可选状态以签名列为准。

ToolAdapterKind

Tool Adapter Kind 公共类型别名;完整类型表达式见声明。

  • 种类: 类型
  • 导入: import type { ToolAdapterKind } from '@codesoul-co/hypha-tools';
  • 源码模块: adapter-factory

声明

text
export type ToolAdapterKind = (typeof toolAdapterKinds)[number];