Agent/types/index.d.ts
2026-08-06 22:17:39 +08:00

139 lines
2.9 KiB
TypeScript

/**
* Agent · MCP 智能体 Web 客户端 — 全局类型声明
*
* 本项目使用 TypeScript 工具链做类型检查(tsc --noEmit),
* 源码使用 JSDoc 风格的 JavaScript(allowJs: true)。
*/
// ============================================================
// MCP 协议相关类型(JSON-RPC 2.0)
// ============================================================
export type JsonRpcId = number | string | null;
export interface JsonRpcRequest<P = unknown> {
jsonrpc: '2.0';
id: JsonRpcId;
method: string;
params?: P;
}
export interface JsonRpcResponse<R = unknown> {
jsonrpc: '2.0';
id: JsonRpcId;
result?: R;
error?: JsonRpcError;
}
export interface JsonRpcError {
code: number;
message: string;
data?: unknown;
}
export interface JsonRpcNotification<P = unknown> {
jsonrpc: '2.0';
method: string;
params?: P;
}
// ============================================================
// MCP 工具相关类型
// ============================================================
export interface McpToolSchema {
type: 'function';
function: {
name: string;
description?: string;
parameters: Record<string, unknown>;
};
}
export interface McpToolCall {
id: string;
name: string;
arguments: Record<string, unknown>;
}
export interface McpToolResult {
ok: boolean;
result?: unknown;
error?: string;
errorName?: string;
}
// ============================================================
// 模型源 client 接口(4 个 client 共用)
// ============================================================
export type ModelSource = 'ollama' | 'omlx' | 'sparkle' | 'anthropic';
export type Protocol = 'anthropic' | 'openai';
export interface ModelConfig {
baseUrl: string;
apiKey?: string;
model: string;
thinkingField?: string;
}
export interface ChatRequest {
messages: ChatMessage[];
tools?: McpToolSchema[];
signal?: AbortSignal;
onEvent?: (event: ChatEvent) => void;
}
export interface ChatMessage {
role: 'system' | 'user' | 'assistant' | 'tool';
content: string | null;
tool_calls?: Array<{
id: string;
type: 'function';
function: { name: string; arguments: string };
}>;
tool_call_id?: string;
}
export interface ChatResult {
text: string;
thinking: string;
toolCalls: McpToolCall[];
usage?: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
}
export type ChatEvent =
| { type: 'thinking'; value: string }
| { type: 'content'; value: string }
| { type: 'usage'; value: ChatResult['usage'] };
// ============================================================
// 假名映射(认知隔离)
// ============================================================
export interface PidEntry {
name?: string;
id?: string | number;
team?: string;
}
export type PidMap = Record<string, PidEntry>;
export type TokenKind =
| 'NAME'
| 'ID'
| 'STATION'
| 'CAR'
| 'TRAIN'
| 'TEAM';
export interface MaskingConfig {
enabled: boolean;
ttlMs: number;
}