@acosmi/sdk-ts 1.4.1 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +160 -0
- package/README.md +99 -5
- package/dist/browser/index.mjs +2432 -1891
- package/dist/browser/index.mjs.map +1 -1
- package/dist/index.mjs +2432 -1891
- package/dist/index.mjs.map +1 -1
- package/dist/node/adapters/anthropic.cjs +6 -4
- package/dist/node/adapters/anthropic.cjs.map +1 -1
- package/dist/node/adapters/anthropic.d.cts +1 -1
- package/dist/node/adapters/anthropic.d.ts +1 -1
- package/dist/node/adapters/anthropic.mjs +6 -4
- package/dist/node/adapters/anthropic.mjs.map +1 -1
- package/dist/node/adapters/openai.cjs +4 -2
- package/dist/node/adapters/openai.cjs.map +1 -1
- package/dist/node/adapters/openai.d.cts +2 -64
- package/dist/node/adapters/openai.d.ts +2 -64
- package/dist/node/adapters/openai.mjs +4 -2
- package/dist/node/adapters/openai.mjs.map +1 -1
- package/dist/node/index-DEA6LXw6.d.cts +409 -0
- package/dist/node/index-DEA6LXw6.d.ts +409 -0
- package/dist/node/index.cjs +2438 -1890
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.d.cts +2846 -1225
- package/dist/node/index.d.ts +2846 -1225
- package/dist/node/index.mjs +2432 -1891
- package/dist/node/index.mjs.map +1 -1
- package/dist/node/openai-BDodfhsG.d.cts +125 -0
- package/dist/node/openai-vfUUgdvL.d.ts +125 -0
- package/docs/compliance.md +452 -2
- package/examples/agent-runs-stream.ts +118 -0
- package/examples/auth-oauth-flow.ts +97 -0
- package/examples/core-chat.ts +85 -0
- package/package.json +5 -2
- package/dist/node/index-CbG9NqE-.d.cts +0 -1027
- package/dist/node/index-CbG9NqE-.d.ts +0 -1027
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { P as ProviderAdapter, k as ProviderFormat, a as ModelCapabilities, C as ChatRequest, b as ChatResponse, S as StreamEvent } from './index-DEA6LXw6.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Anthropic 内容块
|
|
5
|
+
* 覆盖: text / thinking / redacted_thinking / tool_use / tool_result /
|
|
6
|
+
* server_tool_use / mcp_tool_use / mcp_tool_result
|
|
7
|
+
*/
|
|
8
|
+
interface AnthropicContentBlock {
|
|
9
|
+
type: string;
|
|
10
|
+
text?: string;
|
|
11
|
+
/** tool_use / server_tool_use / mcp_tool_use block ID */
|
|
12
|
+
id?: string;
|
|
13
|
+
/** tool_use function name */
|
|
14
|
+
name?: string;
|
|
15
|
+
/** tool_use arguments (json.RawMessage) */
|
|
16
|
+
input?: unknown;
|
|
17
|
+
/** thinking block content */
|
|
18
|
+
thinking?: string;
|
|
19
|
+
/** text — web_search 搜索引用 */
|
|
20
|
+
citations?: unknown;
|
|
21
|
+
/** thinking — Anthropic 签名 (后续请求必须回传) */
|
|
22
|
+
signature?: string;
|
|
23
|
+
/** redacted_thinking — base64 编码的被审查思考内容 */
|
|
24
|
+
data?: string;
|
|
25
|
+
/** server_tool_use / mcp_tool_use / mcp_tool_result — 服务端工具来源 */
|
|
26
|
+
server_name?: string;
|
|
27
|
+
/** mcp_tool_use — MCP 调用者上下文 */
|
|
28
|
+
caller?: unknown;
|
|
29
|
+
/** tool_result / mcp_tool_result — 工具执行结果 */
|
|
30
|
+
tool_use_id?: string;
|
|
31
|
+
content?: unknown;
|
|
32
|
+
is_error?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/** Anthropic token 用量 */
|
|
35
|
+
interface AnthropicUsage {
|
|
36
|
+
input_tokens: number;
|
|
37
|
+
output_tokens: number;
|
|
38
|
+
cache_creation_input_tokens?: number;
|
|
39
|
+
cache_read_input_tokens?: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Anthropic 原生格式同步响应
|
|
43
|
+
* POST /managed-models/:id/anthropic 返回此格式 (无 response.Success 包装)
|
|
44
|
+
*/
|
|
45
|
+
interface AnthropicResponse {
|
|
46
|
+
id: string;
|
|
47
|
+
/** "message" */
|
|
48
|
+
type: string;
|
|
49
|
+
/** "assistant" */
|
|
50
|
+
role: string;
|
|
51
|
+
content: AnthropicContentBlock[];
|
|
52
|
+
model: string;
|
|
53
|
+
stop_reason: string;
|
|
54
|
+
stop_sequence?: string | null;
|
|
55
|
+
usage: AnthropicUsage;
|
|
56
|
+
}
|
|
57
|
+
/** 提取所有 text 类型内容块的文本,拼接返回 */
|
|
58
|
+
declare function anthropicResponseTextContent(r: AnthropicResponse): string;
|
|
59
|
+
/** 提取所有 thinking 类型内容块的文本,拼接返回 */
|
|
60
|
+
declare function anthropicResponseThinkingContent(r: AnthropicResponse): string;
|
|
61
|
+
/** 返回所有 tool_use 类型的内容块 */
|
|
62
|
+
declare function anthropicResponseToolUseBlocks(r: AnthropicResponse): AnthropicContentBlock[];
|
|
63
|
+
|
|
64
|
+
/** 实现 ProviderAdapter, 用于所有非 Anthropic 厂商 */
|
|
65
|
+
declare class OpenAIAdapter implements ProviderAdapter {
|
|
66
|
+
format(): ProviderFormat;
|
|
67
|
+
endpointSuffix(): string;
|
|
68
|
+
/**
|
|
69
|
+
* 构建 OpenAI 兼容格式请求体
|
|
70
|
+
* 不注入 Anthropic betas, 扩展字段 (thinking/effort/speed) 以通用 JSON 传递
|
|
71
|
+
*/
|
|
72
|
+
buildRequestBody(_caps: ModelCapabilities, req: ChatRequest): Record<string, unknown>;
|
|
73
|
+
/**
|
|
74
|
+
* 解析 OpenAI 格式同步响应为 ChatResponse
|
|
75
|
+
* 兼容 APIResponse 包装 {"code":0,"data":{...}} 和裸 OpenAI JSON 两种格式
|
|
76
|
+
*/
|
|
77
|
+
parseResponse(bodyInput: Uint8Array | string): ChatResponse;
|
|
78
|
+
/**
|
|
79
|
+
* 解析 OpenAI SSE 行
|
|
80
|
+
* [DONE] 标记流结束
|
|
81
|
+
*/
|
|
82
|
+
parseStreamLine(eventType: string, data: string): {
|
|
83
|
+
event: StreamEvent;
|
|
84
|
+
done: boolean;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* 把 Anthropic 心智模型的 thinking/effort 翻译成 OpenAI `reasoning_effort` 字段值
|
|
89
|
+
* 返回空串表示不设置
|
|
90
|
+
*/
|
|
91
|
+
declare function resolveOpenAIReasoningEffort(req: ChatRequest): string;
|
|
92
|
+
/**
|
|
93
|
+
* 把 outputConfig 翻译成 OpenAI response_format
|
|
94
|
+
* 返回 null 表示不设置
|
|
95
|
+
*/
|
|
96
|
+
declare function resolveOpenAIResponseFormat(req: ChatRequest): Record<string, unknown> | null;
|
|
97
|
+
/**
|
|
98
|
+
* 解析 OpenAI 格式响应并转换为 AnthropicResponse
|
|
99
|
+
* 用于 chatMessagesOpenAI 方法, 使 Hub 层无需感知 provider 差异
|
|
100
|
+
*/
|
|
101
|
+
declare function parseOpenAIResponseToAnthropic(raw: string | Uint8Array): AnthropicResponse;
|
|
102
|
+
/**
|
|
103
|
+
* 将 OpenAI SSE chunks 转换为 Anthropic 兼容的 StreamEvent
|
|
104
|
+
* 有状态: 跨 chunk 追踪 block 索引
|
|
105
|
+
*/
|
|
106
|
+
declare class OpenAIStreamConverter {
|
|
107
|
+
private messageStarted;
|
|
108
|
+
private thinkingStarted;
|
|
109
|
+
private thinkingStopped;
|
|
110
|
+
private textStarted;
|
|
111
|
+
/** OpenAI tool_call index → Anthropic block index */
|
|
112
|
+
private toolBlockIndex;
|
|
113
|
+
private blockIndex;
|
|
114
|
+
/**
|
|
115
|
+
* 将一行 OpenAI SSE data 转换为零或多个 Anthropic 格式 StreamEvent
|
|
116
|
+
* 返回 { events, done }
|
|
117
|
+
*/
|
|
118
|
+
convert(data: string): {
|
|
119
|
+
events: StreamEvent[];
|
|
120
|
+
done: boolean;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
declare function newOpenAIStreamConverter(): OpenAIStreamConverter;
|
|
124
|
+
|
|
125
|
+
export { type AnthropicResponse as A, OpenAIAdapter as O, type AnthropicContentBlock as a, type AnthropicUsage as b, anthropicResponseTextContent as c, anthropicResponseThinkingContent as d, anthropicResponseToolUseBlocks as e, OpenAIStreamConverter as f, resolveOpenAIResponseFormat as g, newOpenAIStreamConverter as n, parseOpenAIResponseToAnthropic as p, resolveOpenAIReasoningEffort as r };
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { P as ProviderAdapter, k as ProviderFormat, a as ModelCapabilities, C as ChatRequest, b as ChatResponse, S as StreamEvent } from './index-DEA6LXw6.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Anthropic 内容块
|
|
5
|
+
* 覆盖: text / thinking / redacted_thinking / tool_use / tool_result /
|
|
6
|
+
* server_tool_use / mcp_tool_use / mcp_tool_result
|
|
7
|
+
*/
|
|
8
|
+
interface AnthropicContentBlock {
|
|
9
|
+
type: string;
|
|
10
|
+
text?: string;
|
|
11
|
+
/** tool_use / server_tool_use / mcp_tool_use block ID */
|
|
12
|
+
id?: string;
|
|
13
|
+
/** tool_use function name */
|
|
14
|
+
name?: string;
|
|
15
|
+
/** tool_use arguments (json.RawMessage) */
|
|
16
|
+
input?: unknown;
|
|
17
|
+
/** thinking block content */
|
|
18
|
+
thinking?: string;
|
|
19
|
+
/** text — web_search 搜索引用 */
|
|
20
|
+
citations?: unknown;
|
|
21
|
+
/** thinking — Anthropic 签名 (后续请求必须回传) */
|
|
22
|
+
signature?: string;
|
|
23
|
+
/** redacted_thinking — base64 编码的被审查思考内容 */
|
|
24
|
+
data?: string;
|
|
25
|
+
/** server_tool_use / mcp_tool_use / mcp_tool_result — 服务端工具来源 */
|
|
26
|
+
server_name?: string;
|
|
27
|
+
/** mcp_tool_use — MCP 调用者上下文 */
|
|
28
|
+
caller?: unknown;
|
|
29
|
+
/** tool_result / mcp_tool_result — 工具执行结果 */
|
|
30
|
+
tool_use_id?: string;
|
|
31
|
+
content?: unknown;
|
|
32
|
+
is_error?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/** Anthropic token 用量 */
|
|
35
|
+
interface AnthropicUsage {
|
|
36
|
+
input_tokens: number;
|
|
37
|
+
output_tokens: number;
|
|
38
|
+
cache_creation_input_tokens?: number;
|
|
39
|
+
cache_read_input_tokens?: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Anthropic 原生格式同步响应
|
|
43
|
+
* POST /managed-models/:id/anthropic 返回此格式 (无 response.Success 包装)
|
|
44
|
+
*/
|
|
45
|
+
interface AnthropicResponse {
|
|
46
|
+
id: string;
|
|
47
|
+
/** "message" */
|
|
48
|
+
type: string;
|
|
49
|
+
/** "assistant" */
|
|
50
|
+
role: string;
|
|
51
|
+
content: AnthropicContentBlock[];
|
|
52
|
+
model: string;
|
|
53
|
+
stop_reason: string;
|
|
54
|
+
stop_sequence?: string | null;
|
|
55
|
+
usage: AnthropicUsage;
|
|
56
|
+
}
|
|
57
|
+
/** 提取所有 text 类型内容块的文本,拼接返回 */
|
|
58
|
+
declare function anthropicResponseTextContent(r: AnthropicResponse): string;
|
|
59
|
+
/** 提取所有 thinking 类型内容块的文本,拼接返回 */
|
|
60
|
+
declare function anthropicResponseThinkingContent(r: AnthropicResponse): string;
|
|
61
|
+
/** 返回所有 tool_use 类型的内容块 */
|
|
62
|
+
declare function anthropicResponseToolUseBlocks(r: AnthropicResponse): AnthropicContentBlock[];
|
|
63
|
+
|
|
64
|
+
/** 实现 ProviderAdapter, 用于所有非 Anthropic 厂商 */
|
|
65
|
+
declare class OpenAIAdapter implements ProviderAdapter {
|
|
66
|
+
format(): ProviderFormat;
|
|
67
|
+
endpointSuffix(): string;
|
|
68
|
+
/**
|
|
69
|
+
* 构建 OpenAI 兼容格式请求体
|
|
70
|
+
* 不注入 Anthropic betas, 扩展字段 (thinking/effort/speed) 以通用 JSON 传递
|
|
71
|
+
*/
|
|
72
|
+
buildRequestBody(_caps: ModelCapabilities, req: ChatRequest): Record<string, unknown>;
|
|
73
|
+
/**
|
|
74
|
+
* 解析 OpenAI 格式同步响应为 ChatResponse
|
|
75
|
+
* 兼容 APIResponse 包装 {"code":0,"data":{...}} 和裸 OpenAI JSON 两种格式
|
|
76
|
+
*/
|
|
77
|
+
parseResponse(bodyInput: Uint8Array | string): ChatResponse;
|
|
78
|
+
/**
|
|
79
|
+
* 解析 OpenAI SSE 行
|
|
80
|
+
* [DONE] 标记流结束
|
|
81
|
+
*/
|
|
82
|
+
parseStreamLine(eventType: string, data: string): {
|
|
83
|
+
event: StreamEvent;
|
|
84
|
+
done: boolean;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* 把 Anthropic 心智模型的 thinking/effort 翻译成 OpenAI `reasoning_effort` 字段值
|
|
89
|
+
* 返回空串表示不设置
|
|
90
|
+
*/
|
|
91
|
+
declare function resolveOpenAIReasoningEffort(req: ChatRequest): string;
|
|
92
|
+
/**
|
|
93
|
+
* 把 outputConfig 翻译成 OpenAI response_format
|
|
94
|
+
* 返回 null 表示不设置
|
|
95
|
+
*/
|
|
96
|
+
declare function resolveOpenAIResponseFormat(req: ChatRequest): Record<string, unknown> | null;
|
|
97
|
+
/**
|
|
98
|
+
* 解析 OpenAI 格式响应并转换为 AnthropicResponse
|
|
99
|
+
* 用于 chatMessagesOpenAI 方法, 使 Hub 层无需感知 provider 差异
|
|
100
|
+
*/
|
|
101
|
+
declare function parseOpenAIResponseToAnthropic(raw: string | Uint8Array): AnthropicResponse;
|
|
102
|
+
/**
|
|
103
|
+
* 将 OpenAI SSE chunks 转换为 Anthropic 兼容的 StreamEvent
|
|
104
|
+
* 有状态: 跨 chunk 追踪 block 索引
|
|
105
|
+
*/
|
|
106
|
+
declare class OpenAIStreamConverter {
|
|
107
|
+
private messageStarted;
|
|
108
|
+
private thinkingStarted;
|
|
109
|
+
private thinkingStopped;
|
|
110
|
+
private textStarted;
|
|
111
|
+
/** OpenAI tool_call index → Anthropic block index */
|
|
112
|
+
private toolBlockIndex;
|
|
113
|
+
private blockIndex;
|
|
114
|
+
/**
|
|
115
|
+
* 将一行 OpenAI SSE data 转换为零或多个 Anthropic 格式 StreamEvent
|
|
116
|
+
* 返回 { events, done }
|
|
117
|
+
*/
|
|
118
|
+
convert(data: string): {
|
|
119
|
+
events: StreamEvent[];
|
|
120
|
+
done: boolean;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
declare function newOpenAIStreamConverter(): OpenAIStreamConverter;
|
|
124
|
+
|
|
125
|
+
export { type AnthropicResponse as A, OpenAIAdapter as O, type AnthropicContentBlock as a, type AnthropicUsage as b, anthropicResponseTextContent as c, anthropicResponseThinkingContent as d, anthropicResponseToolUseBlocks as e, OpenAIStreamConverter as f, resolveOpenAIResponseFormat as g, newOpenAIStreamConverter as n, parseOpenAIResponseToAnthropic as p, resolveOpenAIReasoningEffort as r };
|