qing-client 0.0.10 → 0.0.12

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.
@@ -4,13 +4,18 @@ export declare abstract class BaseClient {
4
4
  protected config: ClientConfig;
5
5
  protected serviceName: string;
6
6
  protected axiosInstance: AxiosInstance;
7
- protected userContext?: UserContext;
8
7
  protected isFrontendMode: boolean;
9
8
  protected serviceBasePath: string;
10
9
  protected tokenStorage: TokenStorage;
10
+ protected projectId?: string;
11
+ protected appId?: string;
12
+ protected userContext?: UserContext;
11
13
  constructor(config: ClientConfig, serviceName: string);
12
14
  initUserContext(context: UserContext): RawAxiosRequestHeaders;
13
15
  setUserContext(context: UserContext): this;
16
+ setProjectId(projectId: string): this;
17
+ setAppId(appId: string): this;
18
+ setProjectAndApp(projectId: string, appId: string): this;
14
19
  setTokenStorage(storage: TokenStorage): this;
15
20
  private getBaseUrl;
16
21
  setToken(token: string): Promise<void>;
@@ -24,6 +24,16 @@ class BaseClient {
24
24
  this.serviceName = serviceName;
25
25
  this.isFrontendMode = !!config.gatewayUrl;
26
26
  this.serviceBasePath = `/api/${serviceName}`;
27
+ // 根据模式设置不同的配置
28
+ if (this.isFrontendMode) {
29
+ // 前端模式:使用项目和应用ID
30
+ this.projectId = config.projectId;
31
+ this.appId = config.appId;
32
+ }
33
+ else {
34
+ // 后端模式:使用用户上下文
35
+ this.userContext = config.userContext;
36
+ }
27
37
  // 设置令牌存储
28
38
  this.tokenStorage = config.tokenStorage || memoryTokenStorage;
29
39
  // 创建Axios实例
@@ -35,16 +45,10 @@ class BaseClient {
35
45
  'Accept': 'application/json'
36
46
  }
37
47
  });
38
- // 添加请求拦截器
48
+ // 添加请求拦截器 - 只处理认证令牌
39
49
  this.axiosInstance.interceptors.request.use(async (config) => {
40
50
  let headers = config.headers || {};
41
- // 1. 注入用户上下文(后端模式)
42
- if (!this.isFrontendMode && this.userContext)
43
- headers = {
44
- ...headers,
45
- ...this.initUserContext(this.userContext)
46
- };
47
- // 2. 注入认证令牌
51
+ // 只注入认证令牌(两种模式都需要)
48
52
  const token = await this.tokenStorage.getToken();
49
53
  if (token) {
50
54
  headers['Authorization'] = `Bearer ${token}`;
@@ -59,6 +63,7 @@ class BaseClient {
59
63
  };
60
64
  });
61
65
  }
66
+ // 后端模式:用户上下文相关方法
62
67
  initUserContext(context) {
63
68
  return {
64
69
  'v-user-id': context.userId,
@@ -66,9 +71,42 @@ class BaseClient {
66
71
  'v-project-id': context.projectId
67
72
  };
68
73
  }
69
- // 设置用户上下文(后端模式使用)
70
74
  setUserContext(context) {
71
- this.userContext = context;
75
+ if (!this.isFrontendMode) {
76
+ this.userContext = context;
77
+ }
78
+ else {
79
+ console.warn('setUserContext() 仅在后端模式下有效');
80
+ }
81
+ return this;
82
+ }
83
+ // 前端模式:项目和应用相关方法
84
+ setProjectId(projectId) {
85
+ if (this.isFrontendMode) {
86
+ this.projectId = projectId;
87
+ }
88
+ else {
89
+ console.warn('setProjectId() 仅在前端模式下有效');
90
+ }
91
+ return this;
92
+ }
93
+ setAppId(appId) {
94
+ if (this.isFrontendMode) {
95
+ this.appId = appId;
96
+ }
97
+ else {
98
+ console.warn('setAppId() 仅在前端模式下有效');
99
+ }
100
+ return this;
101
+ }
102
+ setProjectAndApp(projectId, appId) {
103
+ if (this.isFrontendMode) {
104
+ this.projectId = projectId;
105
+ this.appId = appId;
106
+ }
107
+ else {
108
+ console.warn('setProjectAndApp() 仅在前端模式下有效');
109
+ }
72
110
  return this;
73
111
  }
74
112
  // 设置令牌存储(可在运行时更换存储策略)
@@ -101,16 +139,31 @@ class BaseClient {
101
139
  async clearToken() {
102
140
  await this.tokenStorage.clearToken();
103
141
  }
104
- // 统一请求方法 - 处理标准API响应
142
+ // 统一请求方法
105
143
  async request(path, options = { method: 'GET' }) {
106
144
  try {
107
145
  const fullPath = this.isFrontendMode
108
146
  ? `${this.serviceBasePath}${path}`
109
147
  : path;
110
148
  let headers = options.headers || {};
111
- const context = options.userContext || this.userContext;
112
- if (context) {
113
- headers = { ...headers, ...this.initUserContext(context) };
149
+ // 根据模式添加不同的头信息
150
+ if (this.isFrontendMode) {
151
+ // 前端模式:添加项目和应用ID头
152
+ const projectId = options.projectId || this.projectId;
153
+ const appId = options.appId || this.appId;
154
+ if (projectId) {
155
+ headers['x-project-id'] = projectId;
156
+ }
157
+ if (appId) {
158
+ headers['x-app-id'] = appId;
159
+ }
160
+ }
161
+ else {
162
+ // 后端模式:添加用户上下文头
163
+ const context = options.userContext || this.userContext;
164
+ if (context) {
165
+ headers = { ...headers, ...this.initUserContext(context) };
166
+ }
114
167
  }
115
168
  const response = await this.axiosInstance.request({
116
169
  url: fullPath,
@@ -133,16 +186,36 @@ class BaseClient {
133
186
  return this.handleApiError(error, path);
134
187
  }
135
188
  }
136
- // 分页请求方法 - 处理分页API响应
189
+ // 分页请求方法
137
190
  async paginatedRequest(path, options = { method: 'GET' }) {
138
191
  try {
139
192
  const fullPath = this.isFrontendMode
140
193
  ? `${this.serviceBasePath}${path}`
141
194
  : path;
195
+ let headers = options.headers || {};
196
+ // 根据模式添加不同的头信息
197
+ if (this.isFrontendMode) {
198
+ // 前端模式:添加项目和应用ID头
199
+ const projectId = options.projectId || this.projectId;
200
+ const appId = options.appId || this.appId;
201
+ if (projectId) {
202
+ headers['x-project-id'] = projectId;
203
+ }
204
+ if (appId) {
205
+ headers['x-app-id'] = appId;
206
+ }
207
+ }
208
+ else {
209
+ // 后端模式:添加用户上下文头
210
+ const context = options.userContext || this.userContext;
211
+ if (context) {
212
+ headers = { ...headers, ...this.initUserContext(context) };
213
+ }
214
+ }
142
215
  const response = await this.axiosInstance.request({
143
216
  url: fullPath,
144
217
  method: options.method,
145
- headers: options.headers,
218
+ headers,
146
219
  params: options.params,
147
220
  data: options.body
148
221
  });
@@ -150,7 +223,7 @@ class BaseClient {
150
223
  if (!response.data.success) {
151
224
  throw new Error(response.data.message || `业务请求失败`);
152
225
  }
153
- // 返回分页数据 - 直接返回整个data部分
226
+ // 返回分页数据
154
227
  return response.data;
155
228
  }
156
229
  catch (error) {
@@ -11,6 +11,12 @@ export declare class Client {
11
11
  readonly user: UserService;
12
12
  readonly token: TokenService;
13
13
  readonly aigc: AigcService;
14
+ protected isFrontendMode: boolean;
14
15
  constructor(config: ClientConfig);
15
16
  setUserContext(context: UserContext): this;
17
+ setProjectId(projectId: string): this;
18
+ setAppId(appId: string): this;
19
+ setProjectAndApp(projectId: string, appId: string): this;
20
+ setToken(token: string): Promise<void>;
21
+ clearToken(): Promise<void>;
16
22
  }
@@ -9,19 +9,84 @@ const UserService_1 = require("../srvice/UserService");
9
9
  class Client {
10
10
  constructor(config) {
11
11
  this.config = config;
12
+ this.isFrontendMode = !!config.gatewayUrl;
12
13
  this.auth = new AuthService_1.AuthService(config);
13
14
  this.msg = new MsgService_1.MsgService(config);
14
15
  this.user = new UserService_1.UserService(config);
15
16
  this.token = new TokenService_1.TokenService(config);
16
17
  this.aigc = new AigcService_1.AigcService(config);
17
18
  }
19
+ // 后端模式:设置用户上下文
18
20
  setUserContext(context) {
19
- this.auth.setUserContext(context);
20
- this.msg.setUserContext(context);
21
- this.user.setUserContext(context);
22
- this.token.setUserContext(context);
23
- this.aigc.setUserContext(context);
21
+ if (!this.isFrontendMode) {
22
+ this.auth.setUserContext(context);
23
+ this.msg.setUserContext(context);
24
+ this.user.setUserContext(context);
25
+ this.token.setUserContext(context);
26
+ this.aigc.setUserContext(context);
27
+ }
28
+ else {
29
+ console.warn('setUserContext() 仅在后端模式下有效');
30
+ }
24
31
  return this;
25
32
  }
33
+ // 前端模式:设置项目ID
34
+ setProjectId(projectId) {
35
+ if (this.isFrontendMode) {
36
+ this.auth.setProjectId(projectId);
37
+ this.msg.setProjectId(projectId);
38
+ this.user.setProjectId(projectId);
39
+ this.token.setProjectId(projectId);
40
+ this.aigc.setProjectId(projectId);
41
+ }
42
+ else {
43
+ console.warn('setProjectId() 仅在前端模式下有效');
44
+ }
45
+ return this;
46
+ }
47
+ // 前端模式:设置应用ID
48
+ setAppId(appId) {
49
+ if (this.isFrontendMode) {
50
+ this.auth.setAppId(appId);
51
+ this.msg.setAppId(appId);
52
+ this.user.setAppId(appId);
53
+ this.token.setAppId(appId);
54
+ this.aigc.setAppId(appId);
55
+ }
56
+ else {
57
+ console.warn('setAppId() 仅在前端模式下有效');
58
+ }
59
+ return this;
60
+ }
61
+ // 前端模式:同时设置项目和应用ID
62
+ setProjectAndApp(projectId, appId) {
63
+ if (this.isFrontendMode) {
64
+ this.auth.setProjectAndApp(projectId, appId);
65
+ this.msg.setProjectAndApp(projectId, appId);
66
+ this.user.setProjectAndApp(projectId, appId);
67
+ this.token.setProjectAndApp(projectId, appId);
68
+ this.aigc.setProjectAndApp(projectId, appId);
69
+ }
70
+ else {
71
+ console.warn('setProjectAndApp() 仅在前端模式下有效');
72
+ }
73
+ return this;
74
+ }
75
+ // 设置认证令牌(两种模式都需要)
76
+ async setToken(token) {
77
+ await this.auth.setToken(token);
78
+ await this.msg.setToken(token);
79
+ await this.user.setToken(token);
80
+ await this.token.setToken(token);
81
+ await this.aigc.setToken(token);
82
+ }
83
+ // 清除认证令牌(两种模式都需要)
84
+ async clearToken() {
85
+ await this.auth.clearToken();
86
+ await this.msg.clearToken();
87
+ await this.user.clearToken();
88
+ await this.token.clearToken();
89
+ await this.aigc.clearToken();
90
+ }
26
91
  }
27
92
  exports.Client = Client;
@@ -0,0 +1,73 @@
1
+ import { BaseClient } from "../client/BaseClient";
2
+ import { ClientConfig, RequestOptions } from "../types";
3
+ import { ChatCompletionRequest, ChatCompletionResponse, ProviderListItem, ProviderDetail, ModelListItem, ModelDetail, CreateSessionRequest, SessionResponse, SessionListResponse, CreateAssistantRequest, AssistantResponse, AssistantListResponse, UpdateAssistantRequest, AddMessageRequest, BatchDeleteSessionsResponse, SessionStatistics, AssistantModelsResponse, PaginatedSessionMessageResponse } from "../types/ai";
4
+ export declare class AiService extends BaseClient {
5
+ constructor(config: ClientConfig);
6
+ chatCompletion(request: ChatCompletionRequest, options?: RequestOptions): Promise<ChatCompletionResponse>;
7
+ getChatModels(options?: RequestOptions): Promise<{
8
+ models: ModelListItem[];
9
+ }>;
10
+ createSession(request: CreateSessionRequest, options?: RequestOptions): Promise<SessionResponse>;
11
+ getSessions(params?: {
12
+ page?: number;
13
+ limit?: number;
14
+ status?: 'active' | 'ended';
15
+ }, options?: RequestOptions): Promise<SessionListResponse>;
16
+ getSession(sessionId: string, options?: RequestOptions): Promise<SessionResponse>;
17
+ updateSession(sessionId: string, request: any, options?: RequestOptions): Promise<SessionResponse>;
18
+ endSession(sessionId: string, options?: RequestOptions): Promise<SessionResponse>;
19
+ deleteSession(sessionId: string, options?: RequestOptions): Promise<void>;
20
+ getSessionModel(sessionId: string, options?: RequestOptions): Promise<{
21
+ currentModel: string;
22
+ availableModels: string[];
23
+ providerName: string;
24
+ }>;
25
+ getActiveSessions(options?: RequestOptions): Promise<{
26
+ sessions: SessionResponse[];
27
+ }>;
28
+ createAssistant(request: CreateAssistantRequest, options?: RequestOptions): Promise<AssistantResponse>;
29
+ getAssistants(params?: {
30
+ page?: number;
31
+ limit?: number;
32
+ }, options?: RequestOptions): Promise<AssistantListResponse>;
33
+ getAssistant(assistantId: string, options?: RequestOptions): Promise<AssistantResponse>;
34
+ updateAssistant(assistantId: string, request: UpdateAssistantRequest, options?: RequestOptions): Promise<AssistantResponse>;
35
+ deleteAssistant(assistantId: string, options?: RequestOptions): Promise<void>;
36
+ getAccessibleAssistants(options?: RequestOptions): Promise<{
37
+ assistants: AssistantResponse[];
38
+ }>;
39
+ getPopularAssistants(limit?: number, options?: RequestOptions): Promise<{
40
+ assistants: AssistantResponse[];
41
+ }>;
42
+ incrementAssistantUsage(assistantId: string, options?: RequestOptions): Promise<AssistantResponse>;
43
+ duplicateAssistant(assistantId: string, newName: string, options?: RequestOptions): Promise<AssistantResponse>;
44
+ getProviders(options?: RequestOptions): Promise<{
45
+ providers: ProviderListItem[];
46
+ }>;
47
+ getProvider(providerKey: string, options?: RequestOptions): Promise<ProviderDetail>;
48
+ getAccessibleProviders(options?: RequestOptions): Promise<{
49
+ providers: ProviderListItem[];
50
+ }>;
51
+ getModels(params?: {
52
+ provider?: string;
53
+ capability?: string;
54
+ }, options?: RequestOptions): Promise<{
55
+ models: ModelListItem[];
56
+ }>;
57
+ getModel(modelId: string, options?: RequestOptions): Promise<ModelDetail>;
58
+ getModelsByProvider(providerKey: string, options?: RequestOptions): Promise<{
59
+ models: ModelListItem[];
60
+ }>;
61
+ getAccessibleModels(options?: RequestOptions): Promise<{
62
+ models: ModelListItem[];
63
+ }>;
64
+ getSessionMessages(sessionId: string, params?: {
65
+ page?: number;
66
+ limit?: number;
67
+ }, options?: RequestOptions): Promise<PaginatedSessionMessageResponse>;
68
+ addMessageToSession(sessionId: string, request: AddMessageRequest, options?: RequestOptions): Promise<SessionResponse>;
69
+ deleteSessionsBatch(sessionIds: string[], options?: RequestOptions): Promise<BatchDeleteSessionsResponse>;
70
+ switchSessionModel(sessionId: string, modelName: string, options?: RequestOptions): Promise<SessionResponse>;
71
+ getSessionStatistics(timeRange?: 'today' | 'week' | 'month' | 'year', options?: RequestOptions): Promise<SessionStatistics>;
72
+ getAssistantModels(assistantId: string, options?: RequestOptions): Promise<AssistantModelsResponse>;
73
+ }
@@ -0,0 +1,256 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AiService = void 0;
4
+ // src/srvice/AiService.ts
5
+ const BaseClient_1 = require("../client/BaseClient");
6
+ class AiService extends BaseClient_1.BaseClient {
7
+ constructor(config) {
8
+ super(config, 'ai');
9
+ }
10
+ // ==================== Chat对话接口 ====================
11
+ // 聊天补全(非流式)
12
+ async chatCompletion(request, options) {
13
+ return this.request('/chat/completion', {
14
+ ...options,
15
+ method: 'POST',
16
+ body: request
17
+ });
18
+ }
19
+ // 获取聊天可用模型
20
+ async getChatModels(options) {
21
+ return this.request('/chat/models', {
22
+ ...options,
23
+ method: 'GET'
24
+ });
25
+ }
26
+ // ==================== Session管理接口 ====================
27
+ // 创建会话
28
+ async createSession(request, options) {
29
+ return this.request('/session', {
30
+ ...options,
31
+ method: 'POST',
32
+ body: request
33
+ });
34
+ }
35
+ // 获取用户会话列表
36
+ async getSessions(params, options) {
37
+ return this.request('/session', {
38
+ ...options,
39
+ method: 'GET',
40
+ params
41
+ });
42
+ }
43
+ // 获取会话详情
44
+ async getSession(sessionId, options) {
45
+ return this.request(`/session/${sessionId}`, {
46
+ ...options,
47
+ method: 'GET'
48
+ });
49
+ }
50
+ // 更新会话
51
+ async updateSession(sessionId, request, options) {
52
+ return this.request(`/session/${sessionId}`, {
53
+ ...options,
54
+ method: 'PUT',
55
+ body: request
56
+ });
57
+ }
58
+ // 结束会话
59
+ async endSession(sessionId, options) {
60
+ return this.request(`/session/${sessionId}/end`, {
61
+ ...options,
62
+ method: 'POST'
63
+ });
64
+ }
65
+ // 删除会话
66
+ async deleteSession(sessionId, options) {
67
+ return this.request(`/session/${sessionId}`, {
68
+ ...options,
69
+ method: 'DELETE'
70
+ });
71
+ }
72
+ // 获取会话模型信息
73
+ async getSessionModel(sessionId, options) {
74
+ return this.request(`/session/${sessionId}/model`, {
75
+ ...options,
76
+ method: 'GET'
77
+ });
78
+ }
79
+ // 获取用户活跃会话
80
+ async getActiveSessions(options) {
81
+ return this.request('/session/active', {
82
+ ...options,
83
+ method: 'GET'
84
+ });
85
+ }
86
+ // ==================== Assistant管理接口 ====================
87
+ // 创建助手
88
+ async createAssistant(request, options) {
89
+ return this.request('/assistant', {
90
+ ...options,
91
+ method: 'POST',
92
+ body: request
93
+ });
94
+ }
95
+ // 获取助手列表
96
+ async getAssistants(params, options) {
97
+ return this.request('/assistant', {
98
+ ...options,
99
+ method: 'GET',
100
+ params
101
+ });
102
+ }
103
+ // 获取助手详情
104
+ async getAssistant(assistantId, options) {
105
+ return this.request(`/assistant/${assistantId}`, {
106
+ ...options,
107
+ method: 'GET'
108
+ });
109
+ }
110
+ // 更新助手
111
+ async updateAssistant(assistantId, request, options) {
112
+ return this.request(`/assistant/${assistantId}`, {
113
+ ...options,
114
+ method: 'PUT',
115
+ body: request
116
+ });
117
+ }
118
+ // 删除助手
119
+ async deleteAssistant(assistantId, options) {
120
+ return this.request(`/assistant/${assistantId}`, {
121
+ ...options,
122
+ method: 'DELETE'
123
+ });
124
+ }
125
+ // 获取可访问的助手
126
+ async getAccessibleAssistants(options) {
127
+ return this.request('/assistant/accessible', {
128
+ ...options,
129
+ method: 'GET'
130
+ });
131
+ }
132
+ // 获取热门助手
133
+ async getPopularAssistants(limit, options) {
134
+ return this.request('/assistant/popular', {
135
+ ...options,
136
+ method: 'GET',
137
+ params: limit ? { limit } : undefined
138
+ });
139
+ }
140
+ // 增加助手使用次数
141
+ async incrementAssistantUsage(assistantId, options) {
142
+ return this.request(`/assistant/${assistantId}/usage`, {
143
+ ...options,
144
+ method: 'POST'
145
+ });
146
+ }
147
+ // 复制助手
148
+ async duplicateAssistant(assistantId, newName, options) {
149
+ return this.request(`/assistant/${assistantId}/duplicate`, {
150
+ ...options,
151
+ method: 'POST',
152
+ body: { newName }
153
+ });
154
+ }
155
+ // ==================== Provider管理接口 ====================
156
+ // 获取服务商列表
157
+ async getProviders(options) {
158
+ return this.request('/provider', {
159
+ ...options,
160
+ method: 'GET'
161
+ });
162
+ }
163
+ // 获取服务商详情
164
+ async getProvider(providerKey, options) {
165
+ return this.request(`/provider/${providerKey}`, {
166
+ ...options,
167
+ method: 'GET'
168
+ });
169
+ }
170
+ // 获取当前用户可访问的服务商
171
+ async getAccessibleProviders(options) {
172
+ return this.request('/provider/accessible/me', {
173
+ ...options,
174
+ method: 'GET'
175
+ });
176
+ }
177
+ // ==================== Model管理接口 ====================
178
+ // 获取模型列表
179
+ async getModels(params, options) {
180
+ return this.request('/model', {
181
+ ...options,
182
+ method: 'GET',
183
+ params
184
+ });
185
+ }
186
+ // 获取模型详情
187
+ async getModel(modelId, options) {
188
+ return this.request(`/model/${modelId}`, {
189
+ ...options,
190
+ method: 'GET'
191
+ });
192
+ }
193
+ // 按服务商获取模型列表
194
+ async getModelsByProvider(providerKey, options) {
195
+ return this.request(`/model/by-provider/${providerKey}`, {
196
+ ...options,
197
+ method: 'GET'
198
+ });
199
+ }
200
+ // 获取当前用户可访问的模型
201
+ async getAccessibleModels(options) {
202
+ return this.request('/model/accessible/me', {
203
+ ...options,
204
+ method: 'GET'
205
+ });
206
+ }
207
+ // 获取会话消息历史
208
+ async getSessionMessages(sessionId, params, options) {
209
+ return this.request(`/session/${sessionId}/messages`, {
210
+ ...options,
211
+ method: 'GET',
212
+ params
213
+ });
214
+ }
215
+ // 添加消息到会话
216
+ async addMessageToSession(sessionId, request, options) {
217
+ return this.request(`/session/${sessionId}/message`, {
218
+ ...options,
219
+ method: 'POST',
220
+ body: request
221
+ });
222
+ }
223
+ // 批量删除会话
224
+ async deleteSessionsBatch(sessionIds, options) {
225
+ return this.request('/session/batch', {
226
+ ...options,
227
+ method: 'DELETE',
228
+ body: { sessionIds }
229
+ });
230
+ }
231
+ // 切换会话模型
232
+ async switchSessionModel(sessionId, modelName, options) {
233
+ const request = { modelName };
234
+ return this.request(`/session/${sessionId}/model`, {
235
+ ...options,
236
+ method: 'PUT',
237
+ body: request
238
+ });
239
+ }
240
+ // 获取会话统计信息
241
+ async getSessionStatistics(timeRange, options) {
242
+ return this.request('/session/statistics', {
243
+ ...options,
244
+ method: 'GET',
245
+ params: timeRange ? { timeRange } : undefined
246
+ });
247
+ }
248
+ // 获取助手可用模型
249
+ async getAssistantModels(assistantId, options) {
250
+ return this.request(`/assistant/${assistantId}/models`, {
251
+ ...options,
252
+ method: 'GET'
253
+ });
254
+ }
255
+ }
256
+ exports.AiService = AiService;