@open-dy/ai_coding_sdk 1.0.11 → 1.0.13
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 +31 -0
- package/dist/ai_platform_llm/client.d.ts +29 -0
- package/dist/ai_platform_llm/client.js +107 -0
- package/dist/ai_platform_llm/core.d.ts +21 -0
- package/dist/ai_platform_llm/core.js +193 -0
- package/dist/ai_platform_llm/index.d.ts +3 -0
- package/dist/ai_platform_llm/index.js +21 -0
- package/dist/ai_platform_llm/types.d.ts +292 -0
- package/dist/ai_platform_llm/types.js +2 -0
- package/dist/auth/access-token.d.ts +139 -0
- package/dist/auth/access-token.js +99 -0
- package/dist/auth/core.d.ts +15 -0
- package/dist/auth/core.js +76 -0
- package/dist/auth/index.d.ts +4 -0
- package/dist/auth/index.js +9 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/openapi/account/firm.d.ts +31 -0
- package/dist/openapi/account/firm.js +23 -0
- package/dist/openapi/account/homepage-data.d.ts +85 -0
- package/dist/openapi/account/homepage-data.js +109 -0
- package/dist/openapi/account/index.d.ts +16 -8
- package/dist/openapi/account/index.js +21 -8
- package/dist/openapi/account/mobile-hash.d.ts +20 -0
- package/dist/openapi/account/mobile-hash.js +22 -0
- package/dist/openapi/account/public-info.d.ts +32 -0
- package/dist/openapi/account/public-info.js +22 -0
- package/dist/openapi/business/index.d.ts +2 -1
- package/dist/openapi/business/index.js +7 -0
- package/dist/openapi/business/task.d.ts +116 -0
- package/dist/openapi/business/task.js +71 -0
- package/dist/openapi/comment/index.d.ts +2 -0
- package/dist/openapi/comment/index.js +7 -1
- package/dist/openapi/comment/manage.d.ts +118 -0
- package/dist/openapi/comment/manage.js +88 -0
- package/dist/openapi/content/index.d.ts +2 -0
- package/dist/openapi/content/index.js +5 -1
- package/dist/openapi/content/video-data.d.ts +94 -0
- package/dist/openapi/content/video-data.js +56 -0
- package/dist/openapi/im/group.d.ts +119 -0
- package/dist/openapi/im/group.js +91 -0
- package/dist/openapi/im/index.d.ts +2 -1
- package/dist/openapi/im/index.js +8 -0
- package/dist/openapi/index.d.ts +21 -11
- package/dist/openapi/index.js +64 -23
- package/dist/openapi/live/index.d.ts +2 -1
- package/dist/openapi/live/index.js +5 -0
- package/dist/openapi/live/room.d.ts +60 -0
- package/dist/openapi/live/room.js +42 -0
- package/dist/openapi/music/collection.d.ts +42 -0
- package/dist/openapi/music/collection.js +20 -0
- package/dist/openapi/music/index.d.ts +2 -0
- package/dist/openapi/music/index.js +6 -0
- package/dist/openapi/relation/fans.d.ts +72 -0
- package/dist/openapi/relation/fans.js +71 -0
- package/dist/openapi/relation/index.d.ts +4 -1
- package/dist/openapi/relation/index.js +10 -0
- package/dist/openapi/relation/social.d.ts +56 -0
- package/dist/openapi/relation/social.js +40 -0
- package/package.json +58 -2
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@ npm install @open-dy/ai_coding_sdk
|
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
|
+
### OpenAPI
|
|
12
13
|
|
|
13
14
|
```ts
|
|
14
15
|
import { searchVideo, type CallOptions } from '@open-dy/ai_coding_sdk/openapi';
|
|
@@ -24,6 +25,36 @@ const result = await searchVideo(
|
|
|
24
25
|
);
|
|
25
26
|
```
|
|
26
27
|
|
|
28
|
+
### Auth
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { getUserAccessToken, refreshUserAccessToken, refreshUserToken } from '@open-dy/ai_coding_sdk/auth';
|
|
32
|
+
|
|
33
|
+
const token = await getUserAccessToken({
|
|
34
|
+
client_key: process.env.CLIENT_KEY!,
|
|
35
|
+
client_secret: process.env.CLIENT_SECRET!,
|
|
36
|
+
code,
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### AI Platform LLM
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import {
|
|
44
|
+
AIPlatformLLMClient,
|
|
45
|
+
} from '@open-dy/ai_coding_sdk/ai_platform_llm';
|
|
46
|
+
|
|
47
|
+
const client = new AIPlatformLLMClient({
|
|
48
|
+
accessToken: process.env.DELEGATE_TOKEN,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const result = await client.generate({
|
|
52
|
+
Messages: [{ Role: 'user', Content: '你好' }],
|
|
53
|
+
ModelTenant: 'douyin',
|
|
54
|
+
ModelKey: 'Doubao-Seed-1.6-back',
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
27
58
|
## License
|
|
28
59
|
|
|
29
60
|
Apache-2.0
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { AIPlatformCallOptions, AIPlatformEvent, AIPlatformLLMClientOptions, AIPlatformOpenAPIResponse, GenerateImageRequest, GenerateImageResponseData, GenerateRequest, GenerateResponseData, GenerateVideoRequest, GenerateVideoResponseData, QueryVideoTaskRequest, QueryVideoTaskResponseData, StreamGenerateRequest } from './types';
|
|
2
|
+
export declare class AIPlatformLLMClient {
|
|
3
|
+
private readonly accessToken?;
|
|
4
|
+
private readonly appEnv;
|
|
5
|
+
private readonly timeoutMs;
|
|
6
|
+
private readonly imageTimeoutMs;
|
|
7
|
+
private readonly streamTimeoutMs;
|
|
8
|
+
private readonly defaultHeaders;
|
|
9
|
+
private readonly gatewayOrigin;
|
|
10
|
+
constructor(options?: AIPlatformLLMClientOptions);
|
|
11
|
+
generate(req: GenerateRequest): Promise<AIPlatformOpenAPIResponse<GenerateResponseData>>;
|
|
12
|
+
generateImage(req: GenerateImageRequest): Promise<AIPlatformOpenAPIResponse<GenerateImageResponseData>>;
|
|
13
|
+
generateVideo(req: GenerateVideoRequest): Promise<AIPlatformOpenAPIResponse<GenerateVideoResponseData>>;
|
|
14
|
+
queryVideoTask(req: QueryVideoTaskRequest): Promise<AIPlatformOpenAPIResponse<QueryVideoTaskResponseData>>;
|
|
15
|
+
waitVideoTask(req: QueryVideoTaskRequest, options?: {
|
|
16
|
+
intervalMs?: number;
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
}): Promise<AIPlatformOpenAPIResponse<QueryVideoTaskResponseData>>;
|
|
19
|
+
streamGenerate(req: StreamGenerateRequest): AsyncGenerator<AIPlatformEvent>;
|
|
20
|
+
}
|
|
21
|
+
export declare function generate(req: GenerateRequest, options?: AIPlatformCallOptions): Promise<AIPlatformOpenAPIResponse<GenerateResponseData>>;
|
|
22
|
+
export declare function generateImage(req: GenerateImageRequest, options?: AIPlatformCallOptions): Promise<AIPlatformOpenAPIResponse<GenerateImageResponseData>>;
|
|
23
|
+
export declare function generateVideo(req: GenerateVideoRequest, options?: AIPlatformCallOptions): Promise<AIPlatformOpenAPIResponse<GenerateVideoResponseData>>;
|
|
24
|
+
export declare function queryVideoTask(req: QueryVideoTaskRequest, options?: AIPlatformCallOptions): Promise<AIPlatformOpenAPIResponse<QueryVideoTaskResponseData>>;
|
|
25
|
+
export declare function waitVideoTask(req: QueryVideoTaskRequest, waitOptions?: {
|
|
26
|
+
intervalMs?: number;
|
|
27
|
+
timeoutMs?: number;
|
|
28
|
+
}, callOptions?: AIPlatformCallOptions): Promise<AIPlatformOpenAPIResponse<QueryVideoTaskResponseData>>;
|
|
29
|
+
export declare function streamGenerate(req: StreamGenerateRequest, options?: AIPlatformCallOptions): AsyncGenerator<AIPlatformEvent>;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AIPlatformLLMClient = void 0;
|
|
4
|
+
exports.generate = generate;
|
|
5
|
+
exports.generateImage = generateImage;
|
|
6
|
+
exports.generateVideo = generateVideo;
|
|
7
|
+
exports.queryVideoTask = queryVideoTask;
|
|
8
|
+
exports.waitVideoTask = waitVideoTask;
|
|
9
|
+
exports.streamGenerate = streamGenerate;
|
|
10
|
+
const core_1 = require("./core");
|
|
11
|
+
function sleep(ms) {
|
|
12
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
13
|
+
}
|
|
14
|
+
class AIPlatformLLMClient {
|
|
15
|
+
constructor(options = {}) {
|
|
16
|
+
this.appEnv = (0, core_1.getAIPlatformAppEnv)();
|
|
17
|
+
this.accessToken = options.accessToken;
|
|
18
|
+
this.timeoutMs = options.timeoutMs ?? core_1.AI_PLATFORM_DEFAULT_REQUEST_TIMEOUT_MS;
|
|
19
|
+
this.imageTimeoutMs = options.imageTimeoutMs ?? core_1.AI_PLATFORM_DEFAULT_IMAGE_TIMEOUT_MS;
|
|
20
|
+
this.streamTimeoutMs = options.streamTimeoutMs ?? core_1.AI_PLATFORM_DEFAULT_STREAM_TIMEOUT_MS;
|
|
21
|
+
this.defaultHeaders = options.defaultHeaders ?? {};
|
|
22
|
+
this.gatewayOrigin = options.gatewayOrigin ?? (0, core_1.getAIPlatformGatewayOrigin)(this.appEnv);
|
|
23
|
+
}
|
|
24
|
+
async generate(req) {
|
|
25
|
+
return (0, core_1.postAIPlatformJSON)(this.gatewayOrigin, '/dy_open_api/ai_platform/llm_gen/generate/', req, {
|
|
26
|
+
accessToken: this.accessToken,
|
|
27
|
+
defaultHeaders: this.defaultHeaders,
|
|
28
|
+
appEnv: this.appEnv,
|
|
29
|
+
timeoutMs: this.timeoutMs,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
async generateImage(req) {
|
|
33
|
+
return (0, core_1.postAIPlatformJSON)(this.gatewayOrigin, '/dy_open_api/ai_platform/llm_gen/image_generate/', req, {
|
|
34
|
+
accessToken: this.accessToken,
|
|
35
|
+
defaultHeaders: this.defaultHeaders,
|
|
36
|
+
appEnv: this.appEnv,
|
|
37
|
+
timeoutMs: this.imageTimeoutMs,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
async generateVideo(req) {
|
|
41
|
+
return (0, core_1.postAIPlatformJSON)(this.gatewayOrigin, '/dy_open_api/ai_platform/llm_gen/video_generate/', req, {
|
|
42
|
+
accessToken: this.accessToken,
|
|
43
|
+
defaultHeaders: this.defaultHeaders,
|
|
44
|
+
appEnv: this.appEnv,
|
|
45
|
+
timeoutMs: this.timeoutMs,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
async queryVideoTask(req) {
|
|
49
|
+
return (0, core_1.postAIPlatformJSON)(this.gatewayOrigin, '/dy_open_api/ai_platform/llm_gen/query_video_task/', req, {
|
|
50
|
+
accessToken: this.accessToken,
|
|
51
|
+
defaultHeaders: this.defaultHeaders,
|
|
52
|
+
appEnv: this.appEnv,
|
|
53
|
+
timeoutMs: this.timeoutMs,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
async waitVideoTask(req, options) {
|
|
57
|
+
const intervalMs = options?.intervalMs ?? 2000;
|
|
58
|
+
const timeoutMs = options?.timeoutMs ?? 120000;
|
|
59
|
+
const startedAt = Date.now();
|
|
60
|
+
while (true) {
|
|
61
|
+
const result = await this.queryVideoTask(req);
|
|
62
|
+
const status = result?.data?.Status;
|
|
63
|
+
if (status === 'succeeded' || status === 'failed' || status === 'cancelled') {
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
if (Date.now() - startedAt > timeoutMs) {
|
|
67
|
+
throw new Error('video_task_poll_timeout');
|
|
68
|
+
}
|
|
69
|
+
await sleep(intervalMs);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async *streamGenerate(req) {
|
|
73
|
+
for await (const event of (0, core_1.streamAIPlatform)(this.gatewayOrigin, '/dy_open_api/ai_platform/llm_gen/stream_generate/', req, {
|
|
74
|
+
accessToken: this.accessToken,
|
|
75
|
+
defaultHeaders: this.defaultHeaders,
|
|
76
|
+
appEnv: this.appEnv,
|
|
77
|
+
timeoutMs: this.streamTimeoutMs,
|
|
78
|
+
})) {
|
|
79
|
+
yield event;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.AIPlatformLLMClient = AIPlatformLLMClient;
|
|
84
|
+
async function generate(req, options) {
|
|
85
|
+
return new AIPlatformLLMClient((0, core_1.createAIPlatformClientOptions)(options)).generate(req);
|
|
86
|
+
}
|
|
87
|
+
async function generateImage(req, options) {
|
|
88
|
+
const clientOptions = (0, core_1.createAIPlatformClientOptions)(options);
|
|
89
|
+
clientOptions.imageTimeoutMs = options?.timeoutMs;
|
|
90
|
+
return new AIPlatformLLMClient(clientOptions).generateImage(req);
|
|
91
|
+
}
|
|
92
|
+
async function generateVideo(req, options) {
|
|
93
|
+
return new AIPlatformLLMClient((0, core_1.createAIPlatformClientOptions)(options)).generateVideo(req);
|
|
94
|
+
}
|
|
95
|
+
async function queryVideoTask(req, options) {
|
|
96
|
+
return new AIPlatformLLMClient((0, core_1.createAIPlatformClientOptions)(options)).queryVideoTask(req);
|
|
97
|
+
}
|
|
98
|
+
async function waitVideoTask(req, waitOptions, callOptions) {
|
|
99
|
+
return new AIPlatformLLMClient((0, core_1.createAIPlatformClientOptions)(callOptions)).waitVideoTask(req, waitOptions);
|
|
100
|
+
}
|
|
101
|
+
async function* streamGenerate(req, options) {
|
|
102
|
+
const clientOptions = (0, core_1.createAIPlatformClientOptions)(options);
|
|
103
|
+
clientOptions.streamTimeoutMs = options?.timeoutMs;
|
|
104
|
+
for await (const event of new AIPlatformLLMClient(clientOptions).streamGenerate(req)) {
|
|
105
|
+
yield event;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { AIPlatformCallOptions, AIPlatformEvent, AIPlatformLLMClientOptions, AIPlatformOpenAPIResponse } from './types';
|
|
2
|
+
export type AIPlatformAppEnv = 'DEV' | 'PROD';
|
|
3
|
+
export declare function getAIPlatformAppEnv(): AIPlatformAppEnv;
|
|
4
|
+
export declare function getAIPlatformGatewayOrigin(appEnv?: AIPlatformAppEnv): string;
|
|
5
|
+
export declare function createAIPlatformClientOptions(options?: AIPlatformCallOptions): AIPlatformLLMClientOptions;
|
|
6
|
+
export declare function postAIPlatformJSON<T>(gatewayOrigin: string, path: string, body: unknown, options?: {
|
|
7
|
+
accessToken?: string;
|
|
8
|
+
defaultHeaders?: Record<string, string>;
|
|
9
|
+
appEnv?: AIPlatformAppEnv;
|
|
10
|
+
timeoutMs?: number;
|
|
11
|
+
}): Promise<T>;
|
|
12
|
+
export declare function parseSSEStream(stream: ReadableStream<Uint8Array>): AsyncGenerator<AIPlatformEvent>;
|
|
13
|
+
export declare function streamAIPlatform<T extends AIPlatformOpenAPIResponse<unknown> = AIPlatformOpenAPIResponse<unknown>>(gatewayOrigin: string, path: string, body: unknown, options?: {
|
|
14
|
+
accessToken?: string;
|
|
15
|
+
defaultHeaders?: Record<string, string>;
|
|
16
|
+
appEnv?: AIPlatformAppEnv;
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
}): AsyncGenerator<AIPlatformEvent>;
|
|
19
|
+
export declare const AI_PLATFORM_DEFAULT_REQUEST_TIMEOUT_MS = 60000;
|
|
20
|
+
export declare const AI_PLATFORM_DEFAULT_IMAGE_TIMEOUT_MS = 120000;
|
|
21
|
+
export declare const AI_PLATFORM_DEFAULT_STREAM_TIMEOUT_MS = 120000;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AI_PLATFORM_DEFAULT_STREAM_TIMEOUT_MS = exports.AI_PLATFORM_DEFAULT_IMAGE_TIMEOUT_MS = exports.AI_PLATFORM_DEFAULT_REQUEST_TIMEOUT_MS = void 0;
|
|
4
|
+
exports.getAIPlatformAppEnv = getAIPlatformAppEnv;
|
|
5
|
+
exports.getAIPlatformGatewayOrigin = getAIPlatformGatewayOrigin;
|
|
6
|
+
exports.createAIPlatformClientOptions = createAIPlatformClientOptions;
|
|
7
|
+
exports.postAIPlatformJSON = postAIPlatformJSON;
|
|
8
|
+
exports.parseSSEStream = parseSSEStream;
|
|
9
|
+
exports.streamAIPlatform = streamAIPlatform;
|
|
10
|
+
const OPENAPI_ORIGIN = 'open.douyin.com';
|
|
11
|
+
const CLOUD_CALL_DOMAIN_MAP = {
|
|
12
|
+
'open.douyin.com': 'open-douyin-com.openapi.dyc.ivolces.com',
|
|
13
|
+
};
|
|
14
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 60000;
|
|
15
|
+
const DEFAULT_IMAGE_TIMEOUT_MS = 120000;
|
|
16
|
+
const DEFAULT_STREAM_TIMEOUT_MS = 120000;
|
|
17
|
+
function isDevEnvironment() {
|
|
18
|
+
return process.env.APP_ENV === 'DEV';
|
|
19
|
+
}
|
|
20
|
+
function getAIPlatformAppEnv() {
|
|
21
|
+
return isDevEnvironment() ? 'DEV' : 'PROD';
|
|
22
|
+
}
|
|
23
|
+
function getAIPlatformGatewayOrigin(appEnv = getAIPlatformAppEnv()) {
|
|
24
|
+
if (appEnv === 'DEV') {
|
|
25
|
+
return `https://${OPENAPI_ORIGIN}`;
|
|
26
|
+
}
|
|
27
|
+
const mappedDomain = CLOUD_CALL_DOMAIN_MAP[OPENAPI_ORIGIN] ?? OPENAPI_ORIGIN;
|
|
28
|
+
return `http://${mappedDomain}`;
|
|
29
|
+
}
|
|
30
|
+
function createAIPlatformClientOptions(options) {
|
|
31
|
+
const defaultHeaders = {
|
|
32
|
+
...(options?.defaultHeaders ?? {}),
|
|
33
|
+
...(options?.logid ? { 'x-tt-logid': options.logid } : {}),
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
accessToken: options?.accessToken,
|
|
37
|
+
timeoutMs: options?.timeoutMs,
|
|
38
|
+
defaultHeaders,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function buildJSONHeaders(accessToken, defaultHeaders, appEnv) {
|
|
42
|
+
return {
|
|
43
|
+
'content-type': 'application/json',
|
|
44
|
+
...(appEnv === 'DEV' && accessToken ? { 'access-token': accessToken } : {}),
|
|
45
|
+
...defaultHeaders,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function buildStreamHeaders(accessToken, defaultHeaders, appEnv) {
|
|
49
|
+
return {
|
|
50
|
+
accept: 'text/event-stream',
|
|
51
|
+
...buildJSONHeaders(accessToken, defaultHeaders, appEnv),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function maskHeaders(headers) {
|
|
55
|
+
return {
|
|
56
|
+
...headers,
|
|
57
|
+
...(headers['access-token'] ? { 'access-token': '***' } : {}),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
async function postAIPlatformJSON(gatewayOrigin, path, body, options = {}) {
|
|
61
|
+
const controller = new AbortController();
|
|
62
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
63
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
64
|
+
const appEnv = options.appEnv ?? getAIPlatformAppEnv();
|
|
65
|
+
const headers = buildJSONHeaders(options.accessToken, options.defaultHeaders ?? {}, appEnv);
|
|
66
|
+
const url = `${gatewayOrigin}${path}`;
|
|
67
|
+
console.log(`[AI Platform] POST ${url}`);
|
|
68
|
+
console.log('[AI Platform] Headers:', maskHeaders(headers));
|
|
69
|
+
console.log('[AI Platform] Body:', JSON.stringify(body).slice(0, 500));
|
|
70
|
+
try {
|
|
71
|
+
const response = await fetch(url, {
|
|
72
|
+
method: 'POST',
|
|
73
|
+
headers,
|
|
74
|
+
body: JSON.stringify(body),
|
|
75
|
+
signal: controller.signal,
|
|
76
|
+
});
|
|
77
|
+
const text = await response.text();
|
|
78
|
+
if (!response.ok) {
|
|
79
|
+
throw new Error(`HTTP Error: ${response.status} ${response.statusText}, x-tt-logid: ${response.headers.get('x-tt-logid') || 'N/A'}`);
|
|
80
|
+
}
|
|
81
|
+
return (text ? JSON.parse(text) : null);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
console.error('[AI Platform Error]', error);
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
finally {
|
|
88
|
+
clearTimeout(timer);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async function* parseSSEStream(stream) {
|
|
92
|
+
const reader = stream.getReader();
|
|
93
|
+
const decoder = new TextDecoder();
|
|
94
|
+
let buffer = '';
|
|
95
|
+
while (true) {
|
|
96
|
+
const { done, value } = await reader.read();
|
|
97
|
+
if (done) {
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
buffer += decoder.decode(value, { stream: true });
|
|
101
|
+
const frames = splitSSEChunks(buffer);
|
|
102
|
+
buffer = frames.rest;
|
|
103
|
+
for (const frame of frames.items) {
|
|
104
|
+
const event = toSSEEvent(frame);
|
|
105
|
+
if (event) {
|
|
106
|
+
yield event;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (buffer.trim()) {
|
|
111
|
+
const event = toSSEEvent(buffer);
|
|
112
|
+
if (event) {
|
|
113
|
+
yield event;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
async function* streamAIPlatform(gatewayOrigin, path, body, options = {}) {
|
|
118
|
+
const controller = new AbortController();
|
|
119
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_STREAM_TIMEOUT_MS;
|
|
120
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
121
|
+
const appEnv = options.appEnv ?? getAIPlatformAppEnv();
|
|
122
|
+
const headers = buildStreamHeaders(options.accessToken, options.defaultHeaders ?? {}, appEnv);
|
|
123
|
+
const url = `${gatewayOrigin}${path}`;
|
|
124
|
+
console.log(`[AI Platform Stream] POST ${url}`);
|
|
125
|
+
console.log('[AI Platform Stream] Headers:', maskHeaders(headers));
|
|
126
|
+
console.log('[AI Platform Stream] Body:', JSON.stringify(body).slice(0, 500));
|
|
127
|
+
try {
|
|
128
|
+
const response = await fetch(url, {
|
|
129
|
+
method: 'POST',
|
|
130
|
+
headers,
|
|
131
|
+
body: JSON.stringify(body),
|
|
132
|
+
signal: controller.signal,
|
|
133
|
+
});
|
|
134
|
+
if (!response.ok || !response.body) {
|
|
135
|
+
throw new Error(`HTTP Error: ${response.status} ${response.statusText}, x-tt-logid: ${response.headers.get('x-tt-logid') || 'N/A'}`);
|
|
136
|
+
}
|
|
137
|
+
for await (const event of parseSSEStream(response.body)) {
|
|
138
|
+
yield event;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
console.error('[AI Platform Stream Error]', error);
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
finally {
|
|
146
|
+
clearTimeout(timer);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function splitSSEChunks(buffer) {
|
|
150
|
+
const items = [];
|
|
151
|
+
let rest = buffer;
|
|
152
|
+
while (true) {
|
|
153
|
+
const match = rest.match(/\r?\n\r?\n/);
|
|
154
|
+
if (!match || match.index === undefined) {
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
items.push(rest.slice(0, match.index));
|
|
158
|
+
rest = rest.slice(match.index + match[0].length);
|
|
159
|
+
}
|
|
160
|
+
return { items, rest };
|
|
161
|
+
}
|
|
162
|
+
function toSSEEvent(frame) {
|
|
163
|
+
const event = { event: 'message', data: '' };
|
|
164
|
+
for (const line of frame.replace(/\r\n/g, '\n').split('\n')) {
|
|
165
|
+
if (line.startsWith('id:')) {
|
|
166
|
+
event.id = line.slice(3).trim();
|
|
167
|
+
}
|
|
168
|
+
else if (line.startsWith('event:')) {
|
|
169
|
+
event.event = line.slice(6).trim();
|
|
170
|
+
}
|
|
171
|
+
else if (line.startsWith('data:')) {
|
|
172
|
+
event.data += `${line.slice(5).trim()}\n`;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
event.data = event.data.trimEnd();
|
|
176
|
+
if (!event.data && !event.id) {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
if (event.data === '[DONE]') {
|
|
180
|
+
event.payload = null;
|
|
181
|
+
return event;
|
|
182
|
+
}
|
|
183
|
+
try {
|
|
184
|
+
event.payload = JSON.parse(event.data);
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
event.payload = null;
|
|
188
|
+
}
|
|
189
|
+
return event;
|
|
190
|
+
}
|
|
191
|
+
exports.AI_PLATFORM_DEFAULT_REQUEST_TIMEOUT_MS = DEFAULT_REQUEST_TIMEOUT_MS;
|
|
192
|
+
exports.AI_PLATFORM_DEFAULT_IMAGE_TIMEOUT_MS = DEFAULT_IMAGE_TIMEOUT_MS;
|
|
193
|
+
exports.AI_PLATFORM_DEFAULT_STREAM_TIMEOUT_MS = DEFAULT_STREAM_TIMEOUT_MS;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { AIPlatformLLMClient, generate, generateImage, generateVideo, queryVideoTask, streamGenerate, waitVideoTask } from './client';
|
|
2
|
+
export { AI_PLATFORM_DEFAULT_IMAGE_TIMEOUT_MS, AI_PLATFORM_DEFAULT_REQUEST_TIMEOUT_MS, AI_PLATFORM_DEFAULT_STREAM_TIMEOUT_MS, createAIPlatformClientOptions, getAIPlatformAppEnv, getAIPlatformGatewayOrigin, parseSSEStream, postAIPlatformJSON, streamAIPlatform } from './core';
|
|
3
|
+
export type { AIPlatformCallOptions, AIPlatformEvent, AIPlatformLLMClientOptions, AIPlatformMessageEventPayload, AIPlatformOpenAPIResponse, AIPlatformUsage, ContentPart, GenerateImageRequest, GenerateImageResponseData, GenerateMessage, GenerateModelParams, GenerateRequest, GenerateResponseData, GenerateVideoRequest, GenerateVideoResponseData, Message, ModelParams, QueryVideoTaskRequest, QueryVideoTaskResponseData, Role, StreamGenerateRequest, ToolCall } from './types';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.streamAIPlatform = exports.postAIPlatformJSON = exports.parseSSEStream = exports.getAIPlatformGatewayOrigin = exports.getAIPlatformAppEnv = exports.createAIPlatformClientOptions = exports.AI_PLATFORM_DEFAULT_STREAM_TIMEOUT_MS = exports.AI_PLATFORM_DEFAULT_REQUEST_TIMEOUT_MS = exports.AI_PLATFORM_DEFAULT_IMAGE_TIMEOUT_MS = exports.waitVideoTask = exports.streamGenerate = exports.queryVideoTask = exports.generateVideo = exports.generateImage = exports.generate = exports.AIPlatformLLMClient = void 0;
|
|
4
|
+
var client_1 = require("./client");
|
|
5
|
+
Object.defineProperty(exports, "AIPlatformLLMClient", { enumerable: true, get: function () { return client_1.AIPlatformLLMClient; } });
|
|
6
|
+
Object.defineProperty(exports, "generate", { enumerable: true, get: function () { return client_1.generate; } });
|
|
7
|
+
Object.defineProperty(exports, "generateImage", { enumerable: true, get: function () { return client_1.generateImage; } });
|
|
8
|
+
Object.defineProperty(exports, "generateVideo", { enumerable: true, get: function () { return client_1.generateVideo; } });
|
|
9
|
+
Object.defineProperty(exports, "queryVideoTask", { enumerable: true, get: function () { return client_1.queryVideoTask; } });
|
|
10
|
+
Object.defineProperty(exports, "streamGenerate", { enumerable: true, get: function () { return client_1.streamGenerate; } });
|
|
11
|
+
Object.defineProperty(exports, "waitVideoTask", { enumerable: true, get: function () { return client_1.waitVideoTask; } });
|
|
12
|
+
var core_1 = require("./core");
|
|
13
|
+
Object.defineProperty(exports, "AI_PLATFORM_DEFAULT_IMAGE_TIMEOUT_MS", { enumerable: true, get: function () { return core_1.AI_PLATFORM_DEFAULT_IMAGE_TIMEOUT_MS; } });
|
|
14
|
+
Object.defineProperty(exports, "AI_PLATFORM_DEFAULT_REQUEST_TIMEOUT_MS", { enumerable: true, get: function () { return core_1.AI_PLATFORM_DEFAULT_REQUEST_TIMEOUT_MS; } });
|
|
15
|
+
Object.defineProperty(exports, "AI_PLATFORM_DEFAULT_STREAM_TIMEOUT_MS", { enumerable: true, get: function () { return core_1.AI_PLATFORM_DEFAULT_STREAM_TIMEOUT_MS; } });
|
|
16
|
+
Object.defineProperty(exports, "createAIPlatformClientOptions", { enumerable: true, get: function () { return core_1.createAIPlatformClientOptions; } });
|
|
17
|
+
Object.defineProperty(exports, "getAIPlatformAppEnv", { enumerable: true, get: function () { return core_1.getAIPlatformAppEnv; } });
|
|
18
|
+
Object.defineProperty(exports, "getAIPlatformGatewayOrigin", { enumerable: true, get: function () { return core_1.getAIPlatformGatewayOrigin; } });
|
|
19
|
+
Object.defineProperty(exports, "parseSSEStream", { enumerable: true, get: function () { return core_1.parseSSEStream; } });
|
|
20
|
+
Object.defineProperty(exports, "postAIPlatformJSON", { enumerable: true, get: function () { return core_1.postAIPlatformJSON; } });
|
|
21
|
+
Object.defineProperty(exports, "streamAIPlatform", { enumerable: true, get: function () { return core_1.streamAIPlatform; } });
|