@myassis/gateway 1.0.72 → 1.0.74

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.
@@ -100,10 +100,28 @@ router.post('/refresh', async (req, res) => {
100
100
  res.status(400).json({ success: false, error: 'Missing refresh token' });
101
101
  return;
102
102
  }
103
+ // 刷新前先找到持有该 refreshToken 的旧 accessToken,用于定位记录
104
+ const oldToken = Array.from(index_js_2.authStore.getAll()?.values() ?? [])
105
+ .find(x => x.refreshToken === refreshToken)?.accessToken;
103
106
  const response = await index_js_1.authApi.refresh(refreshToken);
104
- if (response.success && response.accessToken) {
105
- index_js_2.authStore.updateToken(response.accessToken, response.refreshToken);
106
- res.json({ success: true });
107
+ // server 可能返回 {accessToken} {data:{accessToken}},两种都兼容
108
+ const accessToken = response.data?.accessToken ?? response.accessToken;
109
+ const newRefreshToken = response.data?.refreshToken ?? response.refreshToken;
110
+ const expiresIn = response.data?.expiresIn ?? response.expiresIn;
111
+ if (response.success && accessToken) {
112
+ index_js_2.authStore.updateToken(oldToken ?? accessToken, accessToken, newRefreshToken, expiresIn);
113
+ // 必须将新 token 回传给客户端,否则客户端无法更新本地 token
114
+ res.json({
115
+ success: true,
116
+ data: {
117
+ accessToken,
118
+ refreshToken: newRefreshToken,
119
+ expiresIn,
120
+ },
121
+ accessToken,
122
+ refreshToken: newRefreshToken,
123
+ expiresIn,
124
+ });
107
125
  }
108
126
  else {
109
127
  res.status(401).json({ success: false, error: response.error || 'Invalid refresh token' });
@@ -75,9 +75,15 @@ exports.authService = {
75
75
  getUser: (token) => index_js_2.authStore.get(token)?.user || null,
76
76
  // 刷新 Token
77
77
  refresh: async (refreshToken) => {
78
+ // 刷新前先定位旧 accessToken
79
+ const oldToken = Array.from(index_js_2.authStore.getAll()?.values() ?? [])
80
+ .find(x => x.refreshToken === refreshToken)?.accessToken;
78
81
  const response = await index_js_1.authApi.refresh(refreshToken);
79
- if (response.success && response.accessToken) {
80
- index_js_2.authStore.updateToken(response.accessToken, response.refreshToken);
82
+ const accessToken = response.data?.accessToken ?? response.accessToken;
83
+ const newRefreshToken = response.data?.refreshToken ?? response.refreshToken;
84
+ const expiresIn = response.data?.expiresIn ?? response.expiresIn;
85
+ if (response.success && accessToken) {
86
+ index_js_2.authStore.updateToken(oldToken ?? accessToken, accessToken, newRefreshToken, expiresIn);
81
87
  }
82
88
  return response;
83
89
  }
@@ -86,30 +92,9 @@ exports.authService = {
86
92
  exports.skillsService = {
87
93
  list: async (token) => await index_js_1.skillsApi.list(token),
88
94
  get: (skillId, token) => index_js_1.skillsApi.get(skillId, token),
89
- install: async (skillHubId, token) => {
90
- // 1. 从技能库获取技能详情
91
- const hubResult = await index_js_1.skillHubApi.get(skillHubId, token);
92
- if (!hubResult.success || !hubResult.data) {
93
- return { success: false, error: '获取技能详情失败' };
94
- }
95
- const hub = hubResult.data;
96
- // 2. 创建用户技能记录
97
- const skillData = {
98
- name: hub.name,
99
- description: hub.description || '',
100
- content: hub.content || '',
101
- icon: hub.icon,
102
- templateId: skillHubId,
103
- isActive: true,
104
- apiKeyRequired: hub.apiKeyRequired || false,
105
- };
106
- const response = await index_js_1.skillsApi.create(skillData, token);
107
- if (response.success) {
108
- // 3. 增加安装计数
109
- await index_js_1.skillHubApi.install(skillHubId, token);
110
- }
111
- return response;
112
- },
95
+ // 安装技能:字段由服务端从 skill_hubs 回填,此处不再拼装 body,
96
+ // 防止漏传 scriptsUrl / version 等字段导致入库为空
97
+ install: async (skillHubId, token) => await index_js_1.skillHubApi.install(skillHubId, token),
113
98
  update: (skillId, data, token) => index_js_1.skillsApi.update(skillId, data, token),
114
99
  uninstall: async (skillId, token) => await index_js_1.skillsApi.uninstall(skillId, token),
115
100
  setApiKey: (skillId, apiKey, token) => {
@@ -123,19 +123,38 @@ exports.authStore = {
123
123
  },
124
124
  /**
125
125
  * 更新 Token
126
+ * @param oldToken 刷新前的旧 accessToken,用于定位记录
127
+ * @param newToken 刷新后的新 accessToken
128
+ * @param refreshToken 新的 refreshToken(可选)
129
+ * @param expiresIn 有效期秒数(可选)
130
+ * @returns 是否更新成功
126
131
  */
127
- updateToken(token, expiresIn, refreshToken) {
128
- if (authMap) {
129
- const auth = Array.from(authMap.values()).find(x => x.accessToken === token);
130
- if (auth) {
131
- auth.accessToken = token;
132
- if (refreshToken) {
133
- auth.refreshToken = refreshToken;
134
- }
135
- saveAuthToFile(authMap);
136
- logger.debug('Token updated');
137
- }
132
+ updateToken(oldToken, newToken, refreshToken, expiresIn) {
133
+ if (!authMap) {
134
+ this.load();
135
+ }
136
+ if (!newToken) {
137
+ logger.warn('updateToken called without newToken');
138
+ return false;
138
139
  }
140
+ // 按旧 token 定位记录;若传入的旧 token 已被替换过,则回退用新 token 匹配(幂等)
141
+ const auth = Array.from(authMap.values()).find(x => x.accessToken === oldToken)
142
+ || Array.from(authMap.values()).find(x => x.accessToken === newToken);
143
+ if (!auth) {
144
+ logger.warn('updateToken: no auth record matched the given token');
145
+ return false;
146
+ }
147
+ auth.accessToken = newToken;
148
+ if (refreshToken) {
149
+ auth.refreshToken = refreshToken;
150
+ }
151
+ if (expiresIn) {
152
+ auth.expiresIn = expiresIn;
153
+ auth.expiresAt = Date.now() + expiresIn * 1000;
154
+ }
155
+ saveAuthToFile(authMap);
156
+ logger.debug('Token updated for user:', auth.user?.nickname || auth.user?.account);
157
+ return true;
139
158
  },
140
159
  /**
141
160
  * 获取用户 ID
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@myassis/gateway",
3
- "version": "1.0.72",
3
+ "version": "1.0.74",
4
4
  "description": "我的助手 Gateway Service - 本地 AI 网关服务,支持认证、WebSocket 实时通信和任务调度",
5
5
  "main": "dist/index.js",
6
6
  "bin": {