@unifan/pi-unifan-zh 1.0.42 → 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,36 +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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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("提交信息生成失败,请重试。");
|
|
100
112
|
}
|
|
101
113
|
|
|
102
114
|
export default function (pi: ExtensionAPI) {
|
|
@@ -172,7 +184,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
172
184
|
// 生成失败必须停止,禁止用固定文案继续提交或推送。
|
|
173
185
|
const reason = error instanceof Error ? error.message : String(error);
|
|
174
186
|
notify(`提交信息生成失败,未执行提交或推送:${reason}`, "error");
|
|
175
|
-
pi.sendMessage({ customType: "pi-commit-error", content: `提交信息生成失败,未执行提交或推送。\n\n${reason}\n\n
|
|
187
|
+
pi.sendMessage({ customType: "pi-commit-error", content: `提交信息生成失败,未执行提交或推送。\n\n${reason}\n\n当前暂存内容保留,请根据上述错误原因处理后重新执行。`, display: true });
|
|
176
188
|
return;
|
|
177
189
|
}
|
|
178
190
|
|
|
@@ -117,6 +117,53 @@ test('拒绝空结果、异常完成、截断、非法格式和通用文案', as
|
|
|
117
117
|
await assert.rejects(generateCommitMessage({ modelRegistry: {} }, 'diff', []), /未选择模型/);
|
|
118
118
|
});
|
|
119
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
|
+
|
|
120
167
|
test('生成失败不提交或推送,文件清单仅包含暂存区', async () => {
|
|
121
168
|
const repo = join(root, 'repo');
|
|
122
169
|
await mkdir(repo);
|
|
@@ -140,11 +187,11 @@ test('生成失败不提交或推送,文件清单仅包含暂存区', async ()
|
|
|
140
187
|
sendMessage: message => messages.push(message),
|
|
141
188
|
getThinkingLevel: () => 'max',
|
|
142
189
|
});
|
|
143
|
-
const ctx = { ...context(async () =>
|
|
190
|
+
const ctx = { ...context(async () => response('无法生成规范标题')), cwd: repo, hasUI: true, ui: { notify() {} } };
|
|
144
191
|
await commands.get('commit-push').handler('', ctx);
|
|
145
192
|
assert.equal(git('rev-parse', 'HEAD'), head);
|
|
146
193
|
assert.match(messages.at(-1).content, /未执行提交或推送/);
|
|
147
|
-
assert.match(messages.at(-1).content,
|
|
194
|
+
assert.match(messages.at(-1).content, /自动纠正一次后仍失败/);
|
|
148
195
|
assert.deepEqual(await getStagedFiles(repo), ['角色 文件.cs']);
|
|
149
196
|
// 兼容尚未提供 ctx.thinkingLevel 的宿主,从公开会话接口获取实际档位。
|
|
150
197
|
delete ctx.thinkingLevel;
|