@vectorx/ai-sdk 0.1.3 → 0.4.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.
Files changed (40) hide show
  1. package/{types → lib}/agent/index.d.ts +2 -2
  2. package/{types → lib}/ai.d.ts +7 -8
  3. package/lib/ai.js +20 -28
  4. package/{types → lib}/index.d.ts +3 -2
  5. package/lib/index.js +4 -3
  6. package/{types/types.d.ts → lib/model-type.d.ts} +62 -14
  7. package/lib/model-type.js +24 -0
  8. package/lib/models/Chat.d.ts +14 -0
  9. package/lib/models/Chat.js +36 -0
  10. package/lib/models/Default/index.d.ts +11 -0
  11. package/lib/models/{default.js → Default/index.js} +28 -11
  12. package/lib/models/QwenDocTurbo/index.d.ts +84 -0
  13. package/lib/models/QwenDocTurbo/index.js +178 -0
  14. package/lib/models/QwenImage/index.d.ts +81 -0
  15. package/lib/models/QwenImage/index.js +208 -0
  16. package/lib/models/QwenSketchToImage/index.d.ts +35 -0
  17. package/lib/models/QwenSketchToImage/index.js +155 -0
  18. package/lib/models/QwenStyleRepaintV1/index.d.ts +114 -0
  19. package/lib/models/QwenStyleRepaintV1/index.js +213 -0
  20. package/lib/models/QwenVlMax/index.d.ts +78 -0
  21. package/lib/models/QwenVlMax/index.js +121 -0
  22. package/lib/models/index.d.ts +50 -0
  23. package/lib/models/index.js +44 -4
  24. package/{types → lib}/models/react.d.ts +3 -2
  25. package/lib/models/react.js +3 -3
  26. package/{types → lib}/stream.d.ts +1 -8
  27. package/lib/tokenManager.d.ts +36 -0
  28. package/lib/tokenManager.js +89 -0
  29. package/lib/utils.js +2 -3
  30. package/package.json +4 -5
  31. package/lib/models/model-types.js +0 -6
  32. package/lib/types.js +0 -11
  33. package/types/models/default.d.ts +0 -13
  34. package/types/models/index.d.ts +0 -23
  35. package/types/models/model-types.d.ts +0 -131
  36. /package/{types → lib}/eventsource_parser/index.d.ts +0 -0
  37. /package/{types → lib}/eventsource_parser/parse.d.ts +0 -0
  38. /package/{types → lib}/eventsource_parser/stream.d.ts +0 -0
  39. /package/{types → lib}/eventsource_parser/types.d.ts +0 -0
  40. /package/{types → lib}/utils.d.ts +0 -0
@@ -1,17 +1,11 @@
1
1
  import { type ParsedEvent } from "./eventsource_parser";
2
- import type { BaseDoGenerateOutput, BaseDoStreamOutputChunk, ChatModelMessage, ModelTool, SimpleChatModel, ToolCallAssistantMessage } from "./types";
2
+ import type { ChatModelMessage, ModelTool, ToolCallAssistantMessage } from "./model-type";
3
3
  export type FunctionTool = {
4
4
  name: string;
5
5
  description: string;
6
6
  fn: CallableFunction;
7
7
  parameters: object;
8
8
  };
9
- type RawResponse = {
10
- rawResponse?: any;
11
- };
12
- export type DoGenerateOutput = BaseDoGenerateOutput & RawResponse;
13
- export type DoStreamOutput = AsyncIterableReadableStream<BaseDoStreamOutputChunk & RawResponse>;
14
- export type ChatModelConstructor = typeof SimpleChatModel;
15
9
  export type AsyncIterableReadableStream<T> = ReadableStream<T> & {
16
10
  [Symbol.asyncIterator]: () => {
17
11
  next(): Promise<IteratorResult<T>>;
@@ -51,4 +45,3 @@ export declare function createPromise<T = unknown>(): {
51
45
  };
52
46
  export declare function isToolCallAssistantMessage(message: ChatModelMessage): message is ToolCallAssistantMessage;
53
47
  export declare function functionToolToModelTool(tool: FunctionTool): ModelTool;
54
- export {};
@@ -0,0 +1,36 @@
1
+ import type { IAbstractRequest } from "@vectorx/ai-types";
2
+ export interface TokenResponse {
3
+ code: number;
4
+ msg: string;
5
+ data: {
6
+ token: string;
7
+ expires_at: number | null;
8
+ };
9
+ success: boolean;
10
+ }
11
+ export interface TokenError {
12
+ code: string;
13
+ message: string;
14
+ request_id: string;
15
+ }
16
+ export declare class TokenManager {
17
+ private currentToken;
18
+ private tokenExpiresAt;
19
+ private readonly baseUrl;
20
+ private readonly subUrl;
21
+ private readonly defaultExpireSeconds;
22
+ private readonly refreshBufferSeconds;
23
+ private req;
24
+ constructor(req: IAbstractRequest, baseUrl: string);
25
+ get fullUrl(): string;
26
+ getValidToken(): Promise<string>;
27
+ private refreshToken;
28
+ isTokenValid(): boolean;
29
+ getTokenRemainingTime(): number;
30
+ clearToken(): void;
31
+ getTokenInfo(): {
32
+ token: string | null;
33
+ expiresAt: number;
34
+ remainingTime: number;
35
+ };
36
+ }
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.TokenManager = void 0;
13
+ class TokenManager {
14
+ constructor(req, baseUrl) {
15
+ this.currentToken = null;
16
+ this.tokenExpiresAt = 0;
17
+ this.subUrl = "agent/token";
18
+ this.defaultExpireSeconds = 600;
19
+ this.refreshBufferSeconds = 300;
20
+ this.req = req;
21
+ this.baseUrl = baseUrl;
22
+ }
23
+ get fullUrl() {
24
+ return `${this.baseUrl}/${this.subUrl}`;
25
+ }
26
+ getValidToken() {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ const now = Math.floor(Date.now() / 1000);
29
+ if (this.currentToken && this.tokenExpiresAt > now + this.refreshBufferSeconds) {
30
+ return this.currentToken;
31
+ }
32
+ yield this.refreshToken();
33
+ return this.currentToken;
34
+ });
35
+ }
36
+ refreshToken(expireInSeconds) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ var _a, _b, _c, _d;
39
+ const expireSeconds = expireInSeconds || this.defaultExpireSeconds;
40
+ const url = `${this.fullUrl}?expire_in_seconds=${expireSeconds}`;
41
+ try {
42
+ const response = yield this.req.post({
43
+ url,
44
+ headers: {
45
+ "Content-Type": "application/json",
46
+ },
47
+ });
48
+ if (response.statusCode !== 200) {
49
+ throw new Error(`获取临时 Token 失败: ${(_a = response.data) === null || _a === void 0 ? void 0 : _a.msg} (${(_b = response.data) === null || _b === void 0 ? void 0 : _b.code})`);
50
+ }
51
+ if (!((_d = (_c = response.data) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.token)) {
52
+ throw new Error("获取临时 Token 失败: 响应数据无效");
53
+ }
54
+ this.currentToken = response.data.data.token;
55
+ this.tokenExpiresAt = response.data.data.expires_at || 0;
56
+ }
57
+ catch (error) {
58
+ console.error("刷新临时 Token 失败:", error);
59
+ throw error;
60
+ }
61
+ });
62
+ }
63
+ isTokenValid() {
64
+ if (!this.currentToken) {
65
+ return false;
66
+ }
67
+ const now = Math.floor(Date.now() / 1000);
68
+ return this.tokenExpiresAt > now + this.refreshBufferSeconds;
69
+ }
70
+ getTokenRemainingTime() {
71
+ if (!this.currentToken) {
72
+ return 0;
73
+ }
74
+ const now = Math.floor(Date.now() / 1000);
75
+ return Math.max(0, this.tokenExpiresAt - now);
76
+ }
77
+ clearToken() {
78
+ this.currentToken = null;
79
+ this.tokenExpiresAt = 0;
80
+ }
81
+ getTokenInfo() {
82
+ return {
83
+ token: this.currentToken,
84
+ expiresAt: this.tokenExpiresAt,
85
+ remainingTime: this.getTokenRemainingTime(),
86
+ };
87
+ }
88
+ }
89
+ exports.TokenManager = TokenManager;
package/lib/utils.js CHANGED
@@ -10,21 +10,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.handleResponseData = handleResponseData;
13
- const GO_TO_AI_TEXT = "请检查调用方式,或前往云开发 AI+ 首页查看文档:https://tcb.cloud.tencent.com/dev#/ai";
14
13
  function handleResponseData(responseData, header) {
15
14
  return __awaiter(this, void 0, void 0, function* () {
16
15
  var _a, _b;
17
16
  if (typeof responseData === "object" && responseData && "then" in responseData) {
18
17
  const json = (yield responseData);
19
18
  if (typeof json === "object" && json && "code" in json && json.code !== 0) {
20
- throw new Error(`小红书 ModelRequest 请求出错,错误码:${json.code},错误信息:${json.message}\n${GO_TO_AI_TEXT}\n${JSON.stringify(json, null, 2)}`);
19
+ throw new Error(`ModelRequest 请求出错,错误码:${json.code},错误信息:${json.message}\n${JSON.stringify(json, null, 2)}`);
21
20
  }
22
21
  return responseData;
23
22
  }
24
23
  if ((_b = (_a = header === null || header === void 0 ? void 0 : header.get) === null || _a === void 0 ? void 0 : _a.call(header, "content-type")) === null || _b === void 0 ? void 0 : _b.includes("application/json")) {
25
24
  const json = yield readableStream2JsonObject(responseData);
26
25
  if (typeof json === "object" && json && "code" in json && json.code !== 0) {
27
- throw new Error(`小红书 ModelRequest 请求出错,错误码:${json.code},错误信息:${json.message}\n${GO_TO_AI_TEXT}\n${JSON.stringify(json, null, 2)}`);
26
+ throw new Error(`ModelRequest 请求出错,错误码:${json.code},错误信息:${json.message}\n${JSON.stringify(json, null, 2)}`);
28
27
  }
29
28
  }
30
29
  return responseData;
package/package.json CHANGED
@@ -1,9 +1,8 @@
1
1
  {
2
2
  "name": "@vectorx/ai-sdk",
3
- "version": "0.1.3",
3
+ "version": "0.4.0",
4
4
  "description": "Cloud AI SDK",
5
5
  "main": "lib/index.js",
6
- "types": "types/index.d.ts",
7
6
  "sideEffects": false,
8
7
  "files": [
9
8
  "lib",
@@ -22,9 +21,10 @@
22
21
  "node": ">=18.0.0"
23
22
  },
24
23
  "dependencies": {
25
- "@vectorx/ai-types": "0.1.3",
26
- "@vectorx/agent-runtime": "0.1.3",
27
24
  "@mattiasbuelens/web-streams-adapter": "^0.1.0",
25
+ "@vectorx/ai-types": "0.4.0",
26
+ "langfuse": "^3.38.4",
27
+ "openai": "^4.103.0",
28
28
  "text-encoding-shim": "^1.0.5",
29
29
  "web-streams-polyfill": "^4.1.0",
30
30
  "zod": "^3.24.2"
@@ -36,7 +36,6 @@
36
36
  "@typescript-eslint/parser": "^7.1.0",
37
37
  "eslint": "^8.57.0",
38
38
  "jest": "^29.7.0",
39
- "openai": "^4.103.0",
40
39
  "ts-node-dev": "^2.0.0",
41
40
  "typescript": "^5.3.3"
42
41
  },
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SimpleChatModel = void 0;
4
- class SimpleChatModel {
5
- }
6
- exports.SimpleChatModel = SimpleChatModel;
package/lib/types.js DELETED
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.IAgentEnv = exports.SimpleChatModel = void 0;
4
- class SimpleChatModel {
5
- }
6
- exports.SimpleChatModel = SimpleChatModel;
7
- var IAgentEnv;
8
- (function (IAgentEnv) {
9
- IAgentEnv["Production"] = "production";
10
- IAgentEnv["Development"] = "development";
11
- })(IAgentEnv || (exports.IAgentEnv = IAgentEnv = {}));
@@ -1,13 +0,0 @@
1
- import type { DoGenerateOutput, DoStreamOutput, ModelReq, ModelRequestOptions, ReqOptions } from "../types";
2
- import type { ModelName } from "./index";
3
- export declare abstract class SimpleChatModel {
4
- abstract doGenerate(data: ModelRequestOptions, options?: ReqOptions): Promise<DoGenerateOutput>;
5
- abstract doStream(data: ModelRequestOptions, options?: ReqOptions): Promise<DoStreamOutput>;
6
- }
7
- export declare class DefaultSimpleModel implements SimpleChatModel {
8
- private req;
9
- modelName: ModelName;
10
- constructor(req: ModelReq, modelName: ModelName);
11
- doGenerate(data: ModelRequestOptions, options?: ReqOptions): Promise<DoGenerateOutput>;
12
- doStream(data: ModelRequestOptions, options?: ReqOptions): Promise<DoStreamOutput>;
13
- }
@@ -1,23 +0,0 @@
1
- import { DefaultSimpleModel } from "./default";
2
- import { ReactModel } from "./react";
3
- export declare enum ModelName {
4
- DeepSeekR1 = "deepseek-r1",
5
- DeepSeekV3 = "deepseek-v3",
6
- QwenMax = "qwen-max",
7
- QwenLong = "qwen-long",
8
- QwenPlus = "qwen-plus",
9
- QwenTurbo = "qwen-turbo",
10
- QwenVlOcr = "qwen-vl-ocr",
11
- Wanx21T2iTurbo = "wanx2.1-t2i-turbo"
12
- }
13
- export declare const modelName: {
14
- "deepseek-r1": string;
15
- "deepseek-v3": string;
16
- "qwen-max": string;
17
- "qwen-long": string;
18
- "qwen-plus": string;
19
- "qwen-turbo": string;
20
- "qwen-vl-ocr": string;
21
- };
22
- declare const toolMap: Map<string, Function>;
23
- export { DefaultSimpleModel, ReactModel, toolMap };
@@ -1,131 +0,0 @@
1
- export declare abstract class SimpleChatModel {
2
- abstract doGenerate(data: ModelRequestOptions, options?: ReqOptions): Promise<DoGenerateOutput>;
3
- abstract doStream(data: ModelRequestOptions, options?: ReqOptions): Promise<DoStreamOutput>;
4
- }
5
- export interface ModelRequestOptions {
6
- model: string;
7
- max_tokens?: number;
8
- temperature?: number;
9
- top_p?: number;
10
- n?: number;
11
- conversation_id?: string;
12
- frequency_penalty?: number;
13
- presence_penalty?: number;
14
- stream?: boolean;
15
- platform_tools?: Array<{
16
- plantform_tool_id: string;
17
- payload: any;
18
- }>;
19
- historys?: Array<{
20
- role: string;
21
- content: string;
22
- }>;
23
- knowledge_base?: Array<{
24
- knowledge_base_id: string;
25
- search_mode?: "vector" | "full_text" | "hybrid";
26
- limit?: number;
27
- score_threshold?: number;
28
- }>;
29
- db_base?: any[];
30
- enable_thinking?: boolean;
31
- enable_search?: boolean;
32
- }
33
- type RawResponse = {
34
- rawResponse?: any;
35
- };
36
- export type DoGenerateOutput = BaseDoGenerateOutput & RawResponse;
37
- export type DoStreamOutput = AsyncIterableReadableStream<BaseDoStreamOutputChunk & RawResponse>;
38
- export type ChatModelConstructor = typeof SimpleChatModel;
39
- export type AsyncIterableReadableStream<T> = ReadableStream<T> & {
40
- [Symbol.asyncIterator]: () => {
41
- next(): Promise<IteratorResult<T>>;
42
- };
43
- };
44
- export interface IModelReqInput {
45
- url: string;
46
- headers?: Record<string, string>;
47
- data?: Object;
48
- stream?: boolean;
49
- timeout?: number;
50
- }
51
- export type ModelReq = <T extends IModelReqInput>(props: T) => T["stream"] extends true ? Promise<ReadableStream<Uint8Array>> : Promise<Object>;
52
- export interface IAgentReqInput {
53
- url: string;
54
- method: string;
55
- headers?: Record<string, string>;
56
- data?: Object;
57
- stream?: boolean;
58
- timeout?: number;
59
- }
60
- export interface ReqOptions {
61
- timeout?: number;
62
- }
63
- export type AgentReq = <T extends IAgentReqInput>(props: T) => T["stream"] extends true ? Promise<ReadableStream<Uint8Array>> : Promise<Object>;
64
- export type UserMessage = {
65
- role: "user";
66
- content: string;
67
- };
68
- export type SystemMessage = {
69
- role: "system";
70
- content: string;
71
- };
72
- export type AssistantMessage = PlainAssistantMessage | ToolCallAssistantMessage;
73
- export type PlainAssistantMessage = {
74
- role: "assistant";
75
- content: string;
76
- };
77
- export type ToolCallAssistantMessage = {
78
- role: "assistant";
79
- tool_calls: Array<ToolCall>;
80
- content?: string;
81
- };
82
- export type ToolMessage = {
83
- role: "tool";
84
- tool_call_id: string;
85
- content: string;
86
- };
87
- export type ChatModelMessage = UserMessage | SystemMessage | AssistantMessage | ToolMessage;
88
- export type FunctionTool = {
89
- name: string;
90
- description: string;
91
- fn: CallableFunction;
92
- parameters: object;
93
- };
94
- export type ModelTool = {
95
- type: string;
96
- function: ModelToolFunction;
97
- };
98
- export type ModelToolFunction = {
99
- name: string;
100
- description: string;
101
- parameters: object;
102
- };
103
- export type ToolCall = {
104
- id: string;
105
- type: string;
106
- function: {
107
- name: string;
108
- arguments: string;
109
- };
110
- };
111
- type FinishReason = "tool_calls" | (string & {});
112
- export type Usage = {
113
- completion_tokens: number;
114
- prompt_tokens: number;
115
- total_tokens: number;
116
- };
117
- export interface BaseDoGenerateOutput {
118
- choices?: Array<{
119
- finish_reason?: FinishReason;
120
- message?: ChatModelMessage;
121
- }>;
122
- usage?: Usage;
123
- }
124
- export interface BaseDoStreamOutputChunk {
125
- choices?: Array<{
126
- finish_reason?: FinishReason;
127
- delta?: ChatModelMessage;
128
- }>;
129
- usage?: Usage;
130
- }
131
- export {};
File without changes
File without changes
File without changes
File without changes
File without changes