ai-world-sdk 1.0.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/README.md +1006 -0
- package/dist/__tests__/example.test.d.ts +5 -0
- package/dist/__tests__/example.test.js +533 -0
- package/dist/base.d.ts +96 -0
- package/dist/base.js +181 -0
- package/dist/chat_models/anthropic.d.ts +11 -0
- package/dist/chat_models/anthropic.js +17 -0
- package/dist/chat_models/google.d.ts +11 -0
- package/dist/chat_models/google.js +17 -0
- package/dist/chat_models/openai.d.ts +11 -0
- package/dist/chat_models/openai.js +17 -0
- package/dist/config.d.ts +46 -0
- package/dist/config.js +67 -0
- package/dist/image_generation.d.ts +38 -0
- package/dist/image_generation.js +70 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +77 -0
- package/dist/messages.d.ts +71 -0
- package/dist/messages.js +73 -0
- package/dist/video_generation.d.ts +91 -0
- package/dist/video_generation.js +88 -0
- package/package.json +56 -0
package/dist/messages.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Message types for Langchain SDK
|
|
4
|
+
* Similar to LangChain.js message interface
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.AIMessageChunk = exports.SystemMessage = exports.AIMessage = exports.HumanMessage = void 0;
|
|
8
|
+
class HumanMessage {
|
|
9
|
+
constructor(content) {
|
|
10
|
+
this.role = "user";
|
|
11
|
+
this.content = content;
|
|
12
|
+
}
|
|
13
|
+
toJSON() {
|
|
14
|
+
return {
|
|
15
|
+
role: this.role,
|
|
16
|
+
content: this.content,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.HumanMessage = HumanMessage;
|
|
21
|
+
class AIMessage {
|
|
22
|
+
constructor(content) {
|
|
23
|
+
this.role = "assistant";
|
|
24
|
+
this.content = content;
|
|
25
|
+
}
|
|
26
|
+
toJSON() {
|
|
27
|
+
return {
|
|
28
|
+
role: this.role,
|
|
29
|
+
content: this.content,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.AIMessage = AIMessage;
|
|
34
|
+
class SystemMessage {
|
|
35
|
+
constructor(content) {
|
|
36
|
+
this.role = "system";
|
|
37
|
+
this.content = content;
|
|
38
|
+
}
|
|
39
|
+
toJSON() {
|
|
40
|
+
return {
|
|
41
|
+
role: this.role,
|
|
42
|
+
content: this.content,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.SystemMessage = SystemMessage;
|
|
47
|
+
class AIMessageChunk {
|
|
48
|
+
constructor(data) {
|
|
49
|
+
this.role = "assistant";
|
|
50
|
+
this.type = "AIMessageChunk";
|
|
51
|
+
if (typeof data === "string" || Array.isArray(data)) {
|
|
52
|
+
// 兼容直接传入 content 的情况
|
|
53
|
+
this.content = data;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
// 从序列化的对象创建
|
|
57
|
+
this.content = data.content || "";
|
|
58
|
+
this.id = data.id;
|
|
59
|
+
this.tool_call_chunks = data.tool_call_chunks;
|
|
60
|
+
this.chunk_position = data.chunk_position;
|
|
61
|
+
this.response_metadata = data.response_metadata;
|
|
62
|
+
this.usage_metadata = data.usage_metadata;
|
|
63
|
+
this.additional_kwargs = data.additional_kwargs;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
toJSON() {
|
|
67
|
+
return {
|
|
68
|
+
role: this.role,
|
|
69
|
+
content: this.content,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.AIMessageChunk = AIMessageChunk;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Video Generation Client
|
|
3
|
+
* 视频生成客户端
|
|
4
|
+
*/
|
|
5
|
+
export interface VideoGenerationConfig {
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
token?: string;
|
|
8
|
+
headers?: Record<string, string>;
|
|
9
|
+
}
|
|
10
|
+
export interface VideoGenerationContentText {
|
|
11
|
+
type: "text";
|
|
12
|
+
text: string;
|
|
13
|
+
}
|
|
14
|
+
export interface VideoGenerationContentImage {
|
|
15
|
+
type: "image_url";
|
|
16
|
+
image_url: {
|
|
17
|
+
url: string;
|
|
18
|
+
};
|
|
19
|
+
role: string;
|
|
20
|
+
}
|
|
21
|
+
export type VideoGenerationContent = VideoGenerationContentText | VideoGenerationContentImage;
|
|
22
|
+
export interface VideoGenerationRequest {
|
|
23
|
+
prompt?: string;
|
|
24
|
+
image_url?: string;
|
|
25
|
+
model?: string;
|
|
26
|
+
content?: VideoGenerationContent[];
|
|
27
|
+
callback_url?: string;
|
|
28
|
+
return_last_frame?: boolean;
|
|
29
|
+
service_tier?: string;
|
|
30
|
+
execution_expires_after?: number;
|
|
31
|
+
duration?: number;
|
|
32
|
+
aspect_ratio?: "16:9" | "9:16" | "1:1";
|
|
33
|
+
resolution?: string;
|
|
34
|
+
user?: string;
|
|
35
|
+
}
|
|
36
|
+
export interface ContentGenerationTaskID {
|
|
37
|
+
id: string;
|
|
38
|
+
}
|
|
39
|
+
export interface ContentGenerationError {
|
|
40
|
+
message: string;
|
|
41
|
+
code: string;
|
|
42
|
+
}
|
|
43
|
+
export interface ContentGenerationContent {
|
|
44
|
+
video_url?: string;
|
|
45
|
+
last_frame_url?: string;
|
|
46
|
+
file_url?: string;
|
|
47
|
+
}
|
|
48
|
+
export interface Usage {
|
|
49
|
+
completion_tokens: number;
|
|
50
|
+
}
|
|
51
|
+
export interface ContentGenerationTask {
|
|
52
|
+
id: string;
|
|
53
|
+
model: string;
|
|
54
|
+
status: "queued" | "running" | "succeeded" | "failed" | "cancelled";
|
|
55
|
+
error?: ContentGenerationError;
|
|
56
|
+
content?: ContentGenerationContent;
|
|
57
|
+
usage?: Usage;
|
|
58
|
+
subdivisionlevel?: string;
|
|
59
|
+
fileformat?: string;
|
|
60
|
+
frames?: number;
|
|
61
|
+
framespersecond?: number;
|
|
62
|
+
created_at: number;
|
|
63
|
+
updated_at?: number;
|
|
64
|
+
seed?: number;
|
|
65
|
+
revised_prompt?: string;
|
|
66
|
+
service_tier?: string;
|
|
67
|
+
execution_expires_after?: number;
|
|
68
|
+
}
|
|
69
|
+
export declare class VideoGenerationClient {
|
|
70
|
+
private baseUrl;
|
|
71
|
+
private headers;
|
|
72
|
+
constructor(config?: VideoGenerationConfig);
|
|
73
|
+
/**
|
|
74
|
+
* Create a video generation task
|
|
75
|
+
* 创建视频生成任务
|
|
76
|
+
*/
|
|
77
|
+
create(request: VideoGenerationRequest): Promise<ContentGenerationTaskID>;
|
|
78
|
+
/**
|
|
79
|
+
* Get video generation task status
|
|
80
|
+
* 查询视频生成任务状态
|
|
81
|
+
*/
|
|
82
|
+
get(taskId: string): Promise<ContentGenerationTask>;
|
|
83
|
+
/**
|
|
84
|
+
* Poll video generation task until completion
|
|
85
|
+
* 轮询视频生成任务直到完成
|
|
86
|
+
*/
|
|
87
|
+
poll(taskId: string, options?: {
|
|
88
|
+
interval?: number;
|
|
89
|
+
timeout?: number;
|
|
90
|
+
}): Promise<ContentGenerationTask>;
|
|
91
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Video Generation Client
|
|
4
|
+
* 视频生成客户端
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.VideoGenerationClient = void 0;
|
|
8
|
+
const config_1 = require("./config");
|
|
9
|
+
class VideoGenerationClient {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
// 使用提供的 baseUrl,否则使用全局配置
|
|
12
|
+
const baseUrl = config?.baseUrl || config_1.sdkConfig.getBaseUrl();
|
|
13
|
+
if (!baseUrl) {
|
|
14
|
+
throw new Error("baseUrl is required. Either provide it in config or set it globally using sdkConfig.setBaseUrl()");
|
|
15
|
+
}
|
|
16
|
+
this.baseUrl = baseUrl.replace(/\/$/, "");
|
|
17
|
+
// 合并全局 headers 和配置 headers
|
|
18
|
+
const globalHeaders = config_1.sdkConfig.getHeaders();
|
|
19
|
+
this.headers = {
|
|
20
|
+
"Content-Type": "application/json",
|
|
21
|
+
...globalHeaders,
|
|
22
|
+
...config?.headers,
|
|
23
|
+
};
|
|
24
|
+
// 使用提供的 token,否则使用全局 token
|
|
25
|
+
const token = config?.token || config_1.sdkConfig.getToken();
|
|
26
|
+
if (token) {
|
|
27
|
+
this.headers["Authorization"] = `Bearer ${token}`;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Create a video generation task
|
|
32
|
+
* 创建视频生成任务
|
|
33
|
+
*/
|
|
34
|
+
async create(request) {
|
|
35
|
+
const response = await fetch(`${this.baseUrl}/api/video-proxy/generate`, {
|
|
36
|
+
method: "POST",
|
|
37
|
+
headers: this.headers,
|
|
38
|
+
body: JSON.stringify(request),
|
|
39
|
+
});
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
const errorText = await response.text();
|
|
42
|
+
throw new Error(`Video generation API error: ${response.status} ${errorText}`);
|
|
43
|
+
}
|
|
44
|
+
const data = (await response.json());
|
|
45
|
+
return data;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get video generation task status
|
|
49
|
+
* 查询视频生成任务状态
|
|
50
|
+
*/
|
|
51
|
+
async get(taskId) {
|
|
52
|
+
const response = await fetch(`${this.baseUrl}/api/video-proxy/${taskId}`, {
|
|
53
|
+
method: "GET",
|
|
54
|
+
headers: this.headers,
|
|
55
|
+
});
|
|
56
|
+
if (!response.ok) {
|
|
57
|
+
const errorText = await response.text();
|
|
58
|
+
throw new Error(`Get video task API error: ${response.status} ${errorText}`);
|
|
59
|
+
}
|
|
60
|
+
const data = (await response.json());
|
|
61
|
+
return data;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Poll video generation task until completion
|
|
65
|
+
* 轮询视频生成任务直到完成
|
|
66
|
+
*/
|
|
67
|
+
async poll(taskId, options) {
|
|
68
|
+
const interval = options?.interval || 5000;
|
|
69
|
+
const timeout = options?.timeout || 300000;
|
|
70
|
+
const startTime = Date.now();
|
|
71
|
+
while (true) {
|
|
72
|
+
const task = await this.get(taskId);
|
|
73
|
+
// 检查是否完成或失败
|
|
74
|
+
if (task.status === "succeeded" ||
|
|
75
|
+
task.status === "failed" ||
|
|
76
|
+
task.status === "cancelled") {
|
|
77
|
+
return task;
|
|
78
|
+
}
|
|
79
|
+
// 检查超时
|
|
80
|
+
if (Date.now() - startTime > timeout) {
|
|
81
|
+
throw new Error(`Video generation task polling timeout after ${timeout}ms`);
|
|
82
|
+
}
|
|
83
|
+
// 等待后继续轮询
|
|
84
|
+
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.VideoGenerationClient = VideoGenerationClient;
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ai-world-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "TypeScript SDK for AI World Platform - Chat Models, Image Generation, and Video Generation",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"prepublishOnly": "npm run build",
|
|
10
|
+
"test": "jest",
|
|
11
|
+
"test:watch": "jest --watch",
|
|
12
|
+
"test:non-stream": "jest -t 'ChatGoogleGenerativeAI - 非流式调用'",
|
|
13
|
+
"test:stream": "jest -t 'ChatGoogleGenerativeAI - 流式调用'",
|
|
14
|
+
"test:factory": "jest -t '工厂函数'",
|
|
15
|
+
"test:bind": "jest -t 'bind 方法'",
|
|
16
|
+
"test:batch": "jest -t 'batch 方法'",
|
|
17
|
+
"test:aihubmix-no-stream": "jest -t 'aihubmix.com no stream'",
|
|
18
|
+
"test:aihubmix-stream": "jest -t 'aihubmix.com stream'",
|
|
19
|
+
"test:aihubmix-image": "jest -t 'aihubmix.com 图像生成'",
|
|
20
|
+
"test:seedream-image": "jest -t 'seedream 图像生成'",
|
|
21
|
+
"test:image-generation": "jest -t 'ImageGenerationClient - 基础图像生成'",
|
|
22
|
+
"test:video-generation": "jest -t 'VideoGenerationClient - 轮询视频生成任务'"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"ai-world",
|
|
26
|
+
"ai",
|
|
27
|
+
"llm",
|
|
28
|
+
"langchain",
|
|
29
|
+
"image-generation",
|
|
30
|
+
"video-generation",
|
|
31
|
+
"sdk",
|
|
32
|
+
"typescript",
|
|
33
|
+
"openai",
|
|
34
|
+
"gemini",
|
|
35
|
+
"claude",
|
|
36
|
+
"doubao"
|
|
37
|
+
],
|
|
38
|
+
"author": "",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"dotenv": "^16.0.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^20.0.0",
|
|
45
|
+
"@types/jest": "^29.5.0",
|
|
46
|
+
"@types/dotenv": "^8.2.0",
|
|
47
|
+
"typescript": "^5.0.0",
|
|
48
|
+
"ts-node": "^10.9.2",
|
|
49
|
+
"jest": "^29.5.0",
|
|
50
|
+
"ts-jest": "^29.1.0"
|
|
51
|
+
},
|
|
52
|
+
"files": [
|
|
53
|
+
"dist"
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
|