@tansr/api-client 0.2.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/LICENSE +21 -0
- package/README.md +129 -0
- package/dist/client.d.ts +78 -0
- package/dist/client.js +150 -0
- package/dist/errors.d.ts +47 -0
- package/dist/errors.js +48 -0
- package/dist/http.d.ts +57 -0
- package/dist/http.js +308 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +11 -0
- package/dist/sse.d.ts +27 -0
- package/dist/sse.js +119 -0
- package/dist/stream.d.ts +9 -0
- package/dist/stream.js +87 -0
- package/dist/types.d.ts +196 -0
- package/dist/types.js +9 -0
- package/package.json +38 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @tansr/api-client 类型面。
|
|
3
|
+
*
|
|
4
|
+
* 薄客户端选型:三协议(OpenAI chat / Anthropic messages / OpenAI responses)
|
|
5
|
+
* 的请求与响应只钉常用字段,其余经索引签名放行——网关对数据面是 passthrough
|
|
6
|
+
* (不做协议转换),完整字段语义以上游协议文档为准;钉死全量类型会把上游
|
|
7
|
+
* 演进变成本包的破坏性变更,与零依赖薄客户端的定位相悖。
|
|
8
|
+
*/
|
|
9
|
+
/** 逐请求元信息:消费 x-tansr-request-id / x-tansr-cost / x-tansr-balance 响应头。 */
|
|
10
|
+
export interface ResponseMeta {
|
|
11
|
+
/** 网关请求标识(恒有;回执排障用)。 */
|
|
12
|
+
readonly requestId: string | null;
|
|
13
|
+
/** 实结成本(仅非流式;流式成本流末才可知,网关不回传此头,恒 null)。 */
|
|
14
|
+
readonly cost: string | null;
|
|
15
|
+
/** 台账余额(非流式为终审值;流式为预扣后快照)。 */
|
|
16
|
+
readonly balance: string | null;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 非流式返回值挂载 meta(不可枚举属性:JSON.stringify(响应) 不含 meta,
|
|
20
|
+
* 持久化原始响应体不被污染;注意展开运算 { ...res } 会丢弃它,先取后展开)。
|
|
21
|
+
*/
|
|
22
|
+
export type WithMeta<T> = T & {
|
|
23
|
+
readonly meta: ResponseMeta;
|
|
24
|
+
};
|
|
25
|
+
/** 逐请求选项:create/stream 均收。 */
|
|
26
|
+
export interface RequestOptions {
|
|
27
|
+
/** 取消信号:中止请求与流式读取(原样上抛调用方的中止异常)。 */
|
|
28
|
+
readonly signal?: AbortSignal;
|
|
29
|
+
/** 覆盖构造器 timeoutMs(毫秒;一次调用的总预算,覆盖整个请求生命周期——含流式全程与 GET 429 重试等待)。 */
|
|
30
|
+
readonly timeoutMs?: number;
|
|
31
|
+
}
|
|
32
|
+
export interface ChatMessageParam {
|
|
33
|
+
/** 'system' | 'user' | 'assistant' | 'tool' 等。 */
|
|
34
|
+
role: string;
|
|
35
|
+
content: string | ReadonlyArray<Record<string, unknown>> | null;
|
|
36
|
+
[key: string]: unknown;
|
|
37
|
+
}
|
|
38
|
+
export interface ChatCompletionsCreateParams {
|
|
39
|
+
model: string;
|
|
40
|
+
messages: readonly ChatMessageParam[];
|
|
41
|
+
max_tokens?: number;
|
|
42
|
+
max_completion_tokens?: number;
|
|
43
|
+
temperature?: number;
|
|
44
|
+
top_p?: number;
|
|
45
|
+
stop?: string | readonly string[];
|
|
46
|
+
tools?: ReadonlyArray<Record<string, unknown>>;
|
|
47
|
+
response_format?: Record<string, unknown>;
|
|
48
|
+
[key: string]: unknown;
|
|
49
|
+
}
|
|
50
|
+
export interface ChatCompletionUsage {
|
|
51
|
+
prompt_tokens?: number;
|
|
52
|
+
completion_tokens?: number;
|
|
53
|
+
total_tokens?: number;
|
|
54
|
+
[key: string]: unknown;
|
|
55
|
+
}
|
|
56
|
+
export interface ChatCompletionChoice {
|
|
57
|
+
index?: number;
|
|
58
|
+
message?: {
|
|
59
|
+
role?: string;
|
|
60
|
+
content?: string | null;
|
|
61
|
+
[key: string]: unknown;
|
|
62
|
+
};
|
|
63
|
+
finish_reason?: string | null;
|
|
64
|
+
[key: string]: unknown;
|
|
65
|
+
}
|
|
66
|
+
export interface ChatCompletion {
|
|
67
|
+
id?: string;
|
|
68
|
+
object?: string;
|
|
69
|
+
created?: number;
|
|
70
|
+
model?: string;
|
|
71
|
+
choices?: ChatCompletionChoice[];
|
|
72
|
+
usage?: ChatCompletionUsage;
|
|
73
|
+
[key: string]: unknown;
|
|
74
|
+
}
|
|
75
|
+
export interface ChatCompletionChunkChoice {
|
|
76
|
+
index?: number;
|
|
77
|
+
delta?: {
|
|
78
|
+
role?: string;
|
|
79
|
+
content?: string | null;
|
|
80
|
+
[key: string]: unknown;
|
|
81
|
+
};
|
|
82
|
+
finish_reason?: string | null;
|
|
83
|
+
[key: string]: unknown;
|
|
84
|
+
}
|
|
85
|
+
/** 流式逐帧(SSE data 解析产物;终帧可携 usage——网关已向上游要求 include_usage)。 */
|
|
86
|
+
export interface ChatCompletionChunk {
|
|
87
|
+
id?: string;
|
|
88
|
+
object?: string;
|
|
89
|
+
choices?: ChatCompletionChunkChoice[];
|
|
90
|
+
usage?: ChatCompletionUsage | null;
|
|
91
|
+
[key: string]: unknown;
|
|
92
|
+
}
|
|
93
|
+
export interface AnthropicMessageParam {
|
|
94
|
+
/** 'user' | 'assistant'。 */
|
|
95
|
+
role: string;
|
|
96
|
+
content: string | ReadonlyArray<Record<string, unknown>>;
|
|
97
|
+
[key: string]: unknown;
|
|
98
|
+
}
|
|
99
|
+
export interface MessagesCreateParams {
|
|
100
|
+
model: string;
|
|
101
|
+
messages: readonly AnthropicMessageParam[];
|
|
102
|
+
/** Anthropic 协议必填。 */
|
|
103
|
+
max_tokens: number;
|
|
104
|
+
system?: string | ReadonlyArray<Record<string, unknown>>;
|
|
105
|
+
temperature?: number;
|
|
106
|
+
tools?: ReadonlyArray<Record<string, unknown>>;
|
|
107
|
+
[key: string]: unknown;
|
|
108
|
+
}
|
|
109
|
+
export interface AnthropicUsage {
|
|
110
|
+
input_tokens?: number;
|
|
111
|
+
output_tokens?: number;
|
|
112
|
+
cache_creation_input_tokens?: number;
|
|
113
|
+
cache_read_input_tokens?: number;
|
|
114
|
+
[key: string]: unknown;
|
|
115
|
+
}
|
|
116
|
+
export interface AnthropicMessage {
|
|
117
|
+
id?: string;
|
|
118
|
+
type?: string;
|
|
119
|
+
role?: string;
|
|
120
|
+
model?: string;
|
|
121
|
+
content?: Array<Record<string, unknown>>;
|
|
122
|
+
stop_reason?: string | null;
|
|
123
|
+
stop_sequence?: string | null;
|
|
124
|
+
usage?: AnthropicUsage;
|
|
125
|
+
[key: string]: unknown;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* 流式逐帧:message_start / content_block_delta / message_stop 等。`event:` 行
|
|
129
|
+
* 语义已归一到 type 字段(data 缺 type 时以事件名补位),消费侧统一按 type 分派。
|
|
130
|
+
*/
|
|
131
|
+
export interface MessageStreamEvent {
|
|
132
|
+
type: string;
|
|
133
|
+
[key: string]: unknown;
|
|
134
|
+
}
|
|
135
|
+
export interface ResponsesCreateParams {
|
|
136
|
+
model: string;
|
|
137
|
+
input?: string | ReadonlyArray<unknown>;
|
|
138
|
+
instructions?: string;
|
|
139
|
+
max_output_tokens?: number;
|
|
140
|
+
temperature?: number;
|
|
141
|
+
tools?: ReadonlyArray<Record<string, unknown>>;
|
|
142
|
+
[key: string]: unknown;
|
|
143
|
+
}
|
|
144
|
+
export interface ResponseObject {
|
|
145
|
+
id?: string;
|
|
146
|
+
object?: string;
|
|
147
|
+
status?: string;
|
|
148
|
+
model?: string;
|
|
149
|
+
output?: Array<Record<string, unknown>>;
|
|
150
|
+
output_text?: string;
|
|
151
|
+
usage?: Record<string, unknown>;
|
|
152
|
+
[key: string]: unknown;
|
|
153
|
+
}
|
|
154
|
+
/** 流式逐帧:response.created / response.output_text.delta / response.completed 等(按 type 分派)。 */
|
|
155
|
+
export interface ResponseStreamEvent {
|
|
156
|
+
type: string;
|
|
157
|
+
[key: string]: unknown;
|
|
158
|
+
}
|
|
159
|
+
export interface ModelInfo {
|
|
160
|
+
/** 模型句柄(数据面 model 字段直接用它)。 */
|
|
161
|
+
id: string;
|
|
162
|
+
object?: string;
|
|
163
|
+
created?: number;
|
|
164
|
+
/** 'tansr'(平台)| 'org' | 'user'。 */
|
|
165
|
+
owned_by?: string;
|
|
166
|
+
[key: string]: unknown;
|
|
167
|
+
}
|
|
168
|
+
export interface ModelsListResponse {
|
|
169
|
+
object?: string;
|
|
170
|
+
data: ModelInfo[];
|
|
171
|
+
[key: string]: unknown;
|
|
172
|
+
}
|
|
173
|
+
export interface UsageGetParams {
|
|
174
|
+
/** 统计窗口,形如 '7d'(1..90 天;缺省 7d)。 */
|
|
175
|
+
readonly window?: string;
|
|
176
|
+
/** 聚合维度:day/model/source/key/session,至多 3 维;数组会按逗号拼接。缺省 day,source。 */
|
|
177
|
+
readonly groupBy?: string | readonly string[];
|
|
178
|
+
}
|
|
179
|
+
export interface UsageRow {
|
|
180
|
+
requests?: number;
|
|
181
|
+
inTokens?: number;
|
|
182
|
+
outTokens?: number;
|
|
183
|
+
cost?: string;
|
|
184
|
+
day?: string;
|
|
185
|
+
modelId?: string | null;
|
|
186
|
+
source?: string;
|
|
187
|
+
keyId?: string;
|
|
188
|
+
sessionId?: string;
|
|
189
|
+
[key: string]: unknown;
|
|
190
|
+
}
|
|
191
|
+
export interface UsageReport {
|
|
192
|
+
window?: string;
|
|
193
|
+
groupBy?: string[];
|
|
194
|
+
data: UsageRow[];
|
|
195
|
+
[key: string]: unknown;
|
|
196
|
+
}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tansr/api-client",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Thin zero-dependency TypeScript client for the Tansr gateway: OpenAI/Anthropic/Responses-shaped data plane, models catalog & usage, SSE streaming (public, MIT).",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=20.3"
|
|
10
|
+
},
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"tansr",
|
|
23
|
+
"openai",
|
|
24
|
+
"anthropic",
|
|
25
|
+
"chat-completions",
|
|
26
|
+
"sse",
|
|
27
|
+
"api-client"
|
|
28
|
+
],
|
|
29
|
+
"homepage": "https://tansr.com",
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"README.md",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
}
|
|
38
|
+
}
|