@codehz/ai 0.4.6 → 0.7.1
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/README.md +221 -75
- package/dist/index.d.mts +670 -523
- package/dist/index.mjs +3677 -2207
- package/dist/index.mjs.map +1 -1
- package/package.json +19 -8
- package/.github/workflows/publish.yml +0 -56
- package/.oxfmtrc.json +0 -12
- package/.oxlintrc.json +0 -34
- package/AGENTS.md +0 -37
- package/src/adapters/chat-completions.ts +0 -624
- package/src/adapters/index.ts +0 -44
- package/src/adapters/messages.ts +0 -635
- package/src/adapters/mock.ts +0 -934
- package/src/adapters/ollama.ts +0 -526
- package/src/adapters/responses.ts +0 -818
- package/src/core/aggregator.ts +0 -428
- package/src/core/client.ts +0 -36
- package/src/core/collect-stream.ts +0 -19
- package/src/core/errors.ts +0 -105
- package/src/core/event-factory.ts +0 -151
- package/src/core/index.ts +0 -18
- package/src/core/merge-auxiliary.ts +0 -22
- package/src/core/normalize.ts +0 -65
- package/src/core/validation.ts +0 -404
- package/src/helpers/adapter-auxiliary.ts +0 -155
- package/src/helpers/adapter-base.ts +0 -218
- package/src/helpers/adapter-security.ts +0 -126
- package/src/helpers/auxiliary-collector.ts +0 -166
- package/src/helpers/incremental-stream-parser.ts +0 -142
- package/src/helpers/index.ts +0 -87
- package/src/helpers/mapping.ts +0 -192
- package/src/helpers/provider-request-options.ts +0 -25
- package/src/helpers/provider-stream.ts +0 -147
- package/src/helpers/reasoning-level.ts +0 -86
- package/src/helpers/request-mapper.ts +0 -94
- package/src/helpers/synthetic-stream.ts +0 -188
- package/src/helpers/usage-mapping.ts +0 -110
- package/src/index.ts +0 -17
- package/src/types/adapter.ts +0 -42
- package/src/types/content.ts +0 -15
- package/src/types/events.ts +0 -138
- package/src/types/index.ts +0 -49
- package/src/types/items.ts +0 -57
- package/src/types/request.ts +0 -52
- package/src/types/response.ts +0 -68
- package/tsdown.config.ts +0 -10
package/src/types/items.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Canonical Item 类型体系
|
|
3
|
-
*
|
|
4
|
-
* 覆盖统一请求/响应中的所有 item 类型。
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type { ContentBlock } from "./content.js";
|
|
8
|
-
|
|
9
|
-
// ── Input item types ──────────────────────────────────────────
|
|
10
|
-
|
|
11
|
-
export type MessageItem = {
|
|
12
|
-
type: "message";
|
|
13
|
-
id?: string;
|
|
14
|
-
role: "user" | "assistant";
|
|
15
|
-
content: ContentBlock[];
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type ReasoningItem = {
|
|
19
|
-
type: "reasoning";
|
|
20
|
-
id?: string;
|
|
21
|
-
visibility: "full" | "summary" | "redacted" | "opaque";
|
|
22
|
-
content: ContentBlock[];
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export type ToolCallItem = {
|
|
26
|
-
type: "tool_call";
|
|
27
|
-
id: string;
|
|
28
|
-
name: string;
|
|
29
|
-
argumentsText: string;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export type ToolResultItem = {
|
|
33
|
-
type: "tool_result";
|
|
34
|
-
callId: string;
|
|
35
|
-
toolName: string;
|
|
36
|
-
outcome: "success" | "error" | "rejected";
|
|
37
|
-
content: ContentBlock[];
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export type OpaqueItem = {
|
|
41
|
-
type: "opaque";
|
|
42
|
-
id?: string;
|
|
43
|
-
source: "responses" | "messages" | "chat.completions" | string;
|
|
44
|
-
purpose: "replay" | "provider_state" | "unknown";
|
|
45
|
-
payload: unknown;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
// ── Aliases ───────────────────────────────────────────────────
|
|
49
|
-
|
|
50
|
-
/** 可出现在请求 input 中的 item 类型 */
|
|
51
|
-
export type InputItem = MessageItem | ReasoningItem | ToolCallItem | ToolResultItem | OpaqueItem;
|
|
52
|
-
|
|
53
|
-
/** 可出现在响应 output 中的 item 类型(不含 ToolResultItem) */
|
|
54
|
-
export type OutputItem = MessageItem | ReasoningItem | ToolCallItem | OpaqueItem;
|
|
55
|
-
|
|
56
|
-
/** replay 材料的类型等价于 InputItem */
|
|
57
|
-
export type ReplayItem = InputItem;
|
package/src/types/request.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AIRequest — 统一请求模型
|
|
3
|
-
*
|
|
4
|
-
* 所有 adapter 都接受同一形状的 canonical request。
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type { InstructionBlock } from "./content.js";
|
|
8
|
-
import type { InputItem } from "./items.js";
|
|
9
|
-
|
|
10
|
-
// ── 工具定义 ──────────────────────────────────────────────────
|
|
11
|
-
|
|
12
|
-
export type ToolDefinition = {
|
|
13
|
-
name: string;
|
|
14
|
-
description?: string;
|
|
15
|
-
inputSchema: Record<string, unknown>;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type ToolChoice = "auto" | "none" | { type: "tool"; name: string };
|
|
19
|
-
|
|
20
|
-
// ── include 控制 ──────────────────────────────────────────────
|
|
21
|
-
|
|
22
|
-
export type IncludeSettings = {
|
|
23
|
-
usage?: "off" | "best_effort";
|
|
24
|
-
billing?: "off" | "best_effort";
|
|
25
|
-
providerMetadata?: "off" | "best_effort";
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
// ── reasoning level ───────────────────────────────────────────
|
|
29
|
-
|
|
30
|
-
/** Portable reasoning / thinking effort. Mapped per-adapter to provider wire fields. */
|
|
31
|
-
export type ReasoningLevel = "none" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
|
|
32
|
-
|
|
33
|
-
// ── 统一请求 ──────────────────────────────────────────────────
|
|
34
|
-
|
|
35
|
-
export type AIRequest = {
|
|
36
|
-
instructions?: string | InstructionBlock[];
|
|
37
|
-
input: InputItem[];
|
|
38
|
-
tools?: ToolDefinition[];
|
|
39
|
-
toolChoice?: ToolChoice;
|
|
40
|
-
include?: IncludeSettings;
|
|
41
|
-
metadata?: Record<string, string>;
|
|
42
|
-
temperature?: number;
|
|
43
|
-
maxOutputTokens?: number;
|
|
44
|
-
/**
|
|
45
|
-
* Portable reasoning effort. Adapters map this to provider-native fields
|
|
46
|
-
* (e.g. Responses `reasoning.effort`, Chat Completions `reasoning_effort`,
|
|
47
|
-
* Messages `thinking`, Ollama `think`). Unsupported levels throw.
|
|
48
|
-
*/
|
|
49
|
-
reasoningLevel?: ReasoningLevel;
|
|
50
|
-
/** AbortSignal 用于打断请求。abort 时 fetch 调用会被取消,流迭代器抛出 AbortError。 */
|
|
51
|
-
signal?: AbortSignal;
|
|
52
|
-
};
|
package/src/types/response.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AIResponse — 统一终结结果模型
|
|
3
|
-
*
|
|
4
|
-
* 流结束后由聚合器产出,用于承载当前轮的规范化输出、replay 材料及辅助信息。
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type { OutputItem, ReplayItem, ToolCallItem } from "./items.js";
|
|
8
|
-
|
|
9
|
-
// ── StopReason ────────────────────────────────────────────────
|
|
10
|
-
|
|
11
|
-
export type StopReason = "end_turn" | "tool_call" | "max_output_tokens" | "content_filter" | "error" | "unknown";
|
|
12
|
-
|
|
13
|
-
// ── 辅助信息类型 ──────────────────────────────────────────────
|
|
14
|
-
|
|
15
|
-
export type Usage = {
|
|
16
|
-
/** Provider prompt / input token count */
|
|
17
|
-
inputTokens?: number;
|
|
18
|
-
/** Provider completion / output token count */
|
|
19
|
-
outputTokens?: number;
|
|
20
|
-
/** Reasoning tokens when provider exposes output breakdown (e.g. OpenAI Responses) */
|
|
21
|
-
reasoningTokens?: number;
|
|
22
|
-
totalTokens?: number;
|
|
23
|
-
/** Tokens read from prompt cache (OpenAI cached_tokens, Anthropic cache_read_input_tokens) */
|
|
24
|
-
cachedInputTokens?: number;
|
|
25
|
-
/** Tokens written to prompt cache (Anthropic cache_creation_input_tokens) */
|
|
26
|
-
cacheWriteInputTokens?: number;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export type BillingInfo = {
|
|
30
|
-
amount?: number;
|
|
31
|
-
currency?: string;
|
|
32
|
-
isEstimated: boolean;
|
|
33
|
-
source: "provider" | "lookup" | "derived" | "unknown";
|
|
34
|
-
raw?: unknown;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export type AuxiliaryInfo = {
|
|
38
|
-
usageSource?: "stream" | "final" | "header" | "lookup" | "derived";
|
|
39
|
-
billingSource?: "stream" | "final" | "header" | "lookup" | "derived";
|
|
40
|
-
providerUsage?: unknown;
|
|
41
|
-
providerBilling?: unknown;
|
|
42
|
-
providerMetadata?: Record<string, unknown>;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export type BackendTrace = {
|
|
46
|
-
requestId?: string;
|
|
47
|
-
rawResponseId?: string;
|
|
48
|
-
adapter: "chat-completions" | "messages" | "responses" | "ollama" | "mock";
|
|
49
|
-
isSyntheticStream: boolean;
|
|
50
|
-
metadataSources?: string[];
|
|
51
|
-
warnings?: string[];
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
// ── 统一响应 ──────────────────────────────────────────────────
|
|
55
|
-
|
|
56
|
-
export type AIResponse = {
|
|
57
|
-
id?: string;
|
|
58
|
-
output: OutputItem[];
|
|
59
|
-
replay: ReplayItem[];
|
|
60
|
-
text: string;
|
|
61
|
-
toolCalls: ToolCallItem[];
|
|
62
|
-
stopReason?: StopReason;
|
|
63
|
-
usage?: Usage;
|
|
64
|
-
billing?: BillingInfo;
|
|
65
|
-
auxiliary?: AuxiliaryInfo;
|
|
66
|
-
warnings?: string[];
|
|
67
|
-
backend: BackendTrace;
|
|
68
|
-
};
|