aps-agent/server/provider-sdk/index.ts

80 lines
2.7 KiB
TypeScript
Raw Normal View History

/**
* Provider 契约(架构设计第 6 节):业务系统接入平台的唯一形状。
*
* Provider 是能力适配器,不是业务系统,也不承载模型上下文。三个方法:
* describe() 声明自己有哪些能力
* invoke() 执行一次能力调用
* health() 健康状态
* 能力怎么描述见 docs/spec/capability-description.md,错误分类见 ./errors.js。
*/
import type { ExecutionReceipt } from '@aps/contracts';
import type { ErrorKind } from './errors.js';
export type { ErrorKind } from './errors.js';
export { ERROR_KINDS, isErrorKind, ProviderError } from './errors.js';
/** 能力声明(架构设计 6):read 不改状态;draft 只写隔离草稿;write 必须绑定唯一目标与幂等键 */
export interface Capability {
/** 形如 aps.schedule.preview */
id: string;
/** 界面标题(可省,缺省用 description) */
title?: string;
/** 给模型看的一句话用途(40 字以内) */
description: string;
effect: 'read' | 'draft' | 'write';
mode: 'sync' | 'async';
/** 何时不用:同族能力的边界写在这里 */
whenNotToUse?: string;
/** 业务黑话与同义词 */
synonyms?: string[];
inputSchema: Record<string, unknown>;
outputSchema: Record<string, unknown>;
idempotency: 'none' | 'key' | 'natural';
/** 这个能力可能返回的错误分类 */
errors: ErrorKind[];
}
export interface ProviderManifest {
id: string;
displayName: string;
version: string;
/** 兼容的平台协议版本(当前 1) */
platformVersion: number;
capabilities: Capability[];
}
/** 一次调用的上下文:Core 注入,Provider 不自己找身份与凭据 */
export interface ExecutionContext {
executionId: string;
sessionId: string;
projectId: string | null;
/** 平台用户到业务系统的账号映射结果(A4.1 起生效;现在是占位) */
subject: { userId: string; tenantId: string };
/** 幂等键:Provider 侧若支持幂等,应带回执里透传 */
idempotencyKey?: string;
/** 截止时间(epoch ms):超时由 Core 判定,Provider 应尽力遵守 */
deadlineAt: number;
}
export interface CapabilityCall {
capabilityId: string;
params: unknown;
}
export interface HealthStatus {
status: 'ok' | 'degraded' | 'down';
detail?: string;
checkedAt: number;
}
export interface Provider {
describe(): Promise<ProviderManifest>;
invoke(context: ExecutionContext, call: CapabilityCall): Promise<ExecutionReceipt>;
health(): Promise<HealthStatus>;
}
/** 可选的执行状态查询:异步能力(mode=async)应实现 */
export interface ExecutionStatusProvider {
status(executionId: string): Promise<ExecutionReceipt>;
}