@unifan/pi-unifan-zh 1.0.41 → 1.0.43

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.
@@ -67,28 +67,48 @@ export async function generateCommitMessage(
67
67
 
68
68
  // 宿主解析认证、代理地址和环境;统一接口按模型能力转换会话思考档位,避免底层调用默认发送 none。
69
69
  const model = resolved.auth.baseUrl ? { ...ctx.model, baseUrl: resolved.auth.baseUrl } : ctx.model;
70
- const response = await provider.streamSimple(model, {
71
- systemPrompt: COMMIT_SYSTEM_PROMPT,
72
- messages: [{ role: "user", content: [{ type: "text", text: userPrompt }], timestamp: Date.now() }],
73
- }, {
74
- maxTokens: 4096,
75
- signal: ctx.signal,
76
- reasoning: thinkingLevel === "off" ? undefined : thinkingLevel,
77
- apiKey: resolved.auth.apiKey,
78
- headers: resolved.auth.headers,
79
- env: resolved.env,
80
- }).result();
81
- if (response.stopReason !== "stop")
82
- throw new Error(`模型 ${modelLabel} 未正常完成生成(${response.stopReason}):${response.errorMessage || "请检查模型状态后重试。"}`);
83
-
84
- const text = response.content.map((c) => c.type === "text" ? c.text : "").join("").trim()
85
- .replace(/^```[a-zA-Z]*\r?\n?/, "").replace(/\r?\n?```$/, "").trim();
86
- if (!text) throw new Error("模型返回了空的提交信息。");
87
- if (!/^(feat|fix|perf|refactor|ci|build|chore|docs|test|style|revert)(\([^()\r\n]+\))?: [^\r\n]+$/.test(text.split(/\r?\n/)[0]))
88
- throw new Error("模型返回的提交标题不符合约定格式,请重试或补充修改说明。");
89
- if (text.includes("更新代码与相关配置"))
90
- throw new Error("模型返回了无具体业务含义的通用提交信息,请补充修改说明后重试。");
91
- return text;
70
+ let formatFeedback = "";
71
+ for (let attempt = 0; attempt < 2; attempt++) {
72
+ const response = await provider.streamSimple(model, {
73
+ systemPrompt: COMMIT_SYSTEM_PROMPT,
74
+ messages: [{ role: "user", content: [{ type: "text", text: userPrompt + formatFeedback }], timestamp: Date.now() }],
75
+ }, {
76
+ maxTokens: 4096,
77
+ signal: ctx.signal,
78
+ reasoning: thinkingLevel === "off" ? undefined : thinkingLevel,
79
+ apiKey: resolved.auth.apiKey,
80
+ headers: resolved.auth.headers,
81
+ env: resolved.env,
82
+ }).result();
83
+ if (response.stopReason !== "stop")
84
+ throw new Error(`模型 ${modelLabel} 未正常完成生成(${response.stopReason}):${response.errorMessage || "请检查模型状态后重试。"}`);
85
+
86
+ const text = response.content.map((c) => c.type === "text" ? c.text : "").join("").trim()
87
+ .replace(/^```[a-zA-Z]*\r?\n?/, "").replace(/\r?\n?```$/, "").trim();
88
+ if (!text) throw new Error("模型返回了空的提交信息。");
89
+ const lines = text.split(/\r?\n/);
90
+ // 只剥离完整包裹首行的展示标记,不从说明或多个候选中猜测提交标题。
91
+ lines[0] = lines[0].replace(/^\*\*([^\r\n]+)\*\*$/, "$1").replace(/^`([^`\r\n]+)`$/, "$1");
92
+ const normalizedTitle = lines[0].replace(
93
+ /^(feat|fix|perf|refactor|ci|build|chore|docs|test|style|revert)(?:\(([^()\r\n]+)\)|(([^()\r\n]+)))?\s*[::]\s*(.+)$/,
94
+ (_match, type: string, scope: string | undefined, fullWidthScope: string | undefined, description: string) =>
95
+ `${type}${scope || fullWidthScope ? `(${scope || fullWidthScope})` : ""}: ${description.trim()}`,
96
+ );
97
+ lines[0] = normalizedTitle;
98
+ const normalizedText = lines.join("\n").trim();
99
+ if (!/^(feat|fix|perf|refactor|ci|build|chore|docs|test|style|revert)(\([^()\r\n]+\))?: [^\r\n]+$/.test(normalizedTitle)) {
100
+ const preview = JSON.stringify(lines[0].slice(0, 160));
101
+ if (attempt === 0) {
102
+ formatFeedback = "\n\n上一次输出的首行不符合提交格式。请根据以上差异重新生成;第一行必须直接是 type(scope): 中文描述 或 type: 中文描述,不要说明文字、标题、列表或展示标记。";
103
+ continue;
104
+ }
105
+ throw new Error(`模型 ${modelLabel} 返回的提交标题不符合约定格式,自动纠正一次后仍失败。实际首行(最多160字符):${preview}。要求格式:fix(模块): 中文描述`);
106
+ }
107
+ if (normalizedText.includes("更新代码与相关配置"))
108
+ throw new Error("模型返回了无具体业务含义的通用提交信息,请补充修改说明后重试。");
109
+ return normalizedText;
110
+ }
111
+ throw new Error("提交信息生成失败,请重试。");
92
112
  }
93
113
 
94
114
  export default function (pi: ExtensionAPI) {
@@ -164,7 +184,7 @@ export default function (pi: ExtensionAPI) {
164
184
  // 生成失败必须停止,禁止用固定文案继续提交或推送。
165
185
  const reason = error instanceof Error ? error.message : String(error);
166
186
  notify(`提交信息生成失败,未执行提交或推送:${reason}`, "error");
167
- pi.sendMessage({ customType: "pi-commit-error", content: `提交信息生成失败,未执行提交或推送。\n\n${reason}\n\n当前暂存内容保留,可修复模型配置后重新执行。`, display: true });
187
+ pi.sendMessage({ customType: "pi-commit-error", content: `提交信息生成失败,未执行提交或推送。\n\n${reason}\n\n当前暂存内容保留,请根据上述错误原因处理后重新执行。`, display: true });
168
188
  return;
169
189
  }
170
190
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unifan/pi-commit-zh",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "Pi 智能 Git 提交助手(规范化 Conventional Commits 纯中文版,支持一键提审与推流)",
5
5
  "type": "module",
6
6
  "main": "index.ts",
@@ -24,6 +24,6 @@ export const COMMIT_SYSTEM_PROMPT = `你是专业的 Git 提交信息生成专
24
24
  - 核心改动点 1
25
25
  - 核心改动点 2
26
26
 
27
- 3. 语言约束:所有标题描述与正文说明必须且只能使用纯正中文,严禁长句英文。
27
+ 3. 语言约束:描述与正文必须使用纯正中文;type、可选 scope 以及格式分隔符必须使用英文半角字符。
28
28
 
29
29
  4. 输出格式:直接输出生成的 Commit 信息纯文本(首行标题,空行后跟正文),严禁附加任何 markdown 代码块标记或多余寒暄。`;
@@ -100,6 +100,15 @@ test('提供方或认证缺失时停止生成', async () => {
100
100
  await assert.rejects(generateCommitMessage(ctx, 'diff', []), /提供方不可用/);
101
101
  });
102
102
 
103
+ test('规范化模型返回的全角提交格式', async () => {
104
+ const result = await generateCommitMessage(
105
+ context(async () => response('fix(character):修复角色Renderer收集遗漏')),
106
+ 'diff',
107
+ ['角色.cs'],
108
+ );
109
+ assert.equal(result, 'fix(character): 修复角色Renderer收集遗漏');
110
+ });
111
+
103
112
  test('拒绝空结果、异常完成、截断、非法格式和通用文案', async () => {
104
113
  for (const result of [response(''), response('fix: 修复残留', 'error'), response('fix: 修复残留', 'length'), response('说明文字'), response('chore(GameClient): 更新代码与相关配置')]) {
105
114
  await assert.rejects(generateCommitMessage(context(async () => result), 'diff', ['角色.cs']));
@@ -108,6 +117,53 @@ test('拒绝空结果、异常完成、截断、非法格式和通用文案', as
108
117
  await assert.rejects(generateCommitMessage({ modelRegistry: {} }, 'diff', []), /未选择模型/);
109
118
  });
110
119
 
120
+ test('仅去除首行完整包装,保留正文', async () => {
121
+ for (const title of ['**fix(角色): 恢复原速播放**', '`fix(角色): 恢复原速播放`']) {
122
+ let calls = 0;
123
+ const result = await generateCommitMessage(context(async () => {
124
+ calls++;
125
+ return response(title + '\n\n保留原始动画节奏');
126
+ }), 'diff', ['角色.cs']);
127
+ assert.equal(result, 'fix(角色): 恢复原速播放\n\n保留原始动画节奏');
128
+ assert.equal(calls, 1);
129
+ }
130
+ });
131
+
132
+ test('首行说明触发一次纠正,不直接猜测后面的候选标题', async () => {
133
+ let calls = 0;
134
+ const ctx = context(async (_model, prompt) => {
135
+ calls++;
136
+ if (calls === 1) return response('建议的提交信息:\nfix: 错误候选');
137
+ assert.match(prompt.messages[0].content[0].text, /上一次输出的首行不符合/);
138
+ return response('fix(角色): 恢复原速播放');
139
+ });
140
+ assert.equal(await generateCommitMessage(ctx, 'diff', ['角色.cs']), 'fix(角色): 恢复原速播放');
141
+ assert.equal(calls, 2);
142
+ });
143
+
144
+ test('纠正失败提供有界首行诊断,绝不无限重试', async () => {
145
+ let calls = 0;
146
+ await assert.rejects(generateCommitMessage(context(async () => {
147
+ calls++;
148
+ return response('说明文字' + '长'.repeat(300));
149
+ }), 'diff', ['角色.cs']), error => {
150
+ assert.match(error.message, /自动纠正一次后仍失败/);
151
+ assert.match(error.message, /实际首行/);
152
+ assert.ok(!error.message.includes('长'.repeat(161)));
153
+ return true;
154
+ });
155
+ assert.equal(calls, 2);
156
+ });
157
+
158
+ test('异常完成不会进入格式纠正重试', async () => {
159
+ let calls = 0;
160
+ await assert.rejects(generateCommitMessage(context(async () => {
161
+ calls++;
162
+ return response('fix: 修复问题', 'error');
163
+ }), 'diff', []), /未正常完成生成/);
164
+ assert.equal(calls, 1);
165
+ });
166
+
111
167
  test('生成失败不提交或推送,文件清单仅包含暂存区', async () => {
112
168
  const repo = join(root, 'repo');
113
169
  await mkdir(repo);
@@ -131,11 +187,11 @@ test('生成失败不提交或推送,文件清单仅包含暂存区', async ()
131
187
  sendMessage: message => messages.push(message),
132
188
  getThinkingLevel: () => 'max',
133
189
  });
134
- const ctx = { ...context(async () => { throw new Error('认证失败'); }), cwd: repo, hasUI: true, ui: { notify() {} } };
190
+ const ctx = { ...context(async () => response('无法生成规范标题')), cwd: repo, hasUI: true, ui: { notify() {} } };
135
191
  await commands.get('commit-push').handler('', ctx);
136
192
  assert.equal(git('rev-parse', 'HEAD'), head);
137
193
  assert.match(messages.at(-1).content, /未执行提交或推送/);
138
- assert.match(messages.at(-1).content, /认证失败/);
194
+ assert.match(messages.at(-1).content, /自动纠正一次后仍失败/);
139
195
  assert.deepEqual(await getStagedFiles(repo), ['角色 文件.cs']);
140
196
  // 兼容尚未提供 ctx.thinkingLevel 的宿主,从公开会话接口获取实际档位。
141
197
  delete ctx.thinkingLevel;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unifan/pi-unifan-zh",
3
- "version": "1.0.41",
3
+ "version": "1.0.43",
4
4
  "description": "Pi Coding Agent 中文扩展全家桶与独立插件集",
5
5
  "type": "module",
6
6
  "main": "extensions/sessions/index.ts",