@unifan/pi-unifan-zh 1.0.42 → 1.0.44
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,53 @@ 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
|
+
// pi-ai 1.x 的提供方接口只接受已归一化的消息,context.systemPrompt 会被静默丢弃;
|
|
73
|
+
// 缺少格式约束时思考型模型会耗尽 maxTokens 却不出结果(停止原因 length),因此约束并入用户消息。
|
|
74
|
+
const response = await provider.streamSimple(model, {
|
|
75
|
+
messages: [{
|
|
76
|
+
role: "user",
|
|
77
|
+
content: [{ type: "text", text: `${COMMIT_SYSTEM_PROMPT}\n\n${userPrompt}${formatFeedback}` }],
|
|
78
|
+
timestamp: Date.now(),
|
|
79
|
+
}],
|
|
80
|
+
}, {
|
|
81
|
+
maxTokens: 4096,
|
|
82
|
+
signal: ctx.signal,
|
|
83
|
+
reasoning: thinkingLevel === "off" ? undefined : thinkingLevel,
|
|
84
|
+
apiKey: resolved.auth.apiKey,
|
|
85
|
+
headers: resolved.auth.headers,
|
|
86
|
+
env: resolved.env,
|
|
87
|
+
}).result();
|
|
88
|
+
if (response.stopReason !== "stop")
|
|
89
|
+
throw new Error(`模型 ${modelLabel} 未正常完成生成(${response.stopReason}):${response.errorMessage || "请检查模型状态后重试。"}`);
|
|
90
|
+
|
|
91
|
+
const text = response.content.map((c) => c.type === "text" ? c.text : "").join("").trim()
|
|
92
|
+
.replace(/^```[a-zA-Z]*\r?\n?/, "").replace(/\r?\n?```$/, "").trim();
|
|
93
|
+
if (!text) throw new Error("模型返回了空的提交信息。");
|
|
94
|
+
const lines = text.split(/\r?\n/);
|
|
95
|
+
// 只剥离完整包裹首行的展示标记,不从说明或多个候选中猜测提交标题。
|
|
96
|
+
lines[0] = lines[0].replace(/^\*\*([^\r\n]+)\*\*$/, "$1").replace(/^`([^`\r\n]+)`$/, "$1");
|
|
97
|
+
const normalizedTitle = lines[0].replace(
|
|
98
|
+
/^(feat|fix|perf|refactor|ci|build|chore|docs|test|style|revert)(?:\(([^()\r\n]+)\)|(([^()\r\n]+)))?\s*[::]\s*(.+)$/,
|
|
99
|
+
(_match, type: string, scope: string | undefined, fullWidthScope: string | undefined, description: string) =>
|
|
100
|
+
`${type}${scope || fullWidthScope ? `(${scope || fullWidthScope})` : ""}: ${description.trim()}`,
|
|
101
|
+
);
|
|
102
|
+
lines[0] = normalizedTitle;
|
|
103
|
+
const normalizedText = lines.join("\n").trim();
|
|
104
|
+
if (!/^(feat|fix|perf|refactor|ci|build|chore|docs|test|style|revert)(\([^()\r\n]+\))?: [^\r\n]+$/.test(normalizedTitle)) {
|
|
105
|
+
const preview = JSON.stringify(lines[0].slice(0, 160));
|
|
106
|
+
if (attempt === 0) {
|
|
107
|
+
formatFeedback = "\n\n上一次输出的首行不符合提交格式。请根据以上差异重新生成;第一行必须直接是 type(scope): 中文描述 或 type: 中文描述,不要说明文字、标题、列表或展示标记。";
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
throw new Error(`模型 ${modelLabel} 返回的提交标题不符合约定格式,自动纠正一次后仍失败。实际首行(最多160字符):${preview}。要求格式:fix(模块): 中文描述`);
|
|
111
|
+
}
|
|
112
|
+
if (normalizedText.includes("更新代码与相关配置"))
|
|
113
|
+
throw new Error("模型返回了无具体业务含义的通用提交信息,请补充修改说明后重试。");
|
|
114
|
+
return normalizedText;
|
|
115
|
+
}
|
|
116
|
+
throw new Error("提交信息生成失败,请重试。");
|
|
100
117
|
}
|
|
101
118
|
|
|
102
119
|
export default function (pi: ExtensionAPI) {
|
|
@@ -172,7 +189,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
172
189
|
// 生成失败必须停止,禁止用固定文案继续提交或推送。
|
|
173
190
|
const reason = error instanceof Error ? error.message : String(error);
|
|
174
191
|
notify(`提交信息生成失败,未执行提交或推送:${reason}`, "error");
|
|
175
|
-
pi.sendMessage({ customType: "pi-commit-error", content: `提交信息生成失败,未执行提交或推送。\n\n${reason}\n\n
|
|
192
|
+
pi.sendMessage({ customType: "pi-commit-error", content: `提交信息生成失败,未执行提交或推送。\n\n${reason}\n\n当前暂存内容保留,请根据上述错误原因处理后重新执行。`, display: true });
|
|
176
193
|
return;
|
|
177
194
|
}
|
|
178
195
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unifan/pi-commit-zh",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.19",
|
|
4
4
|
"description": "Pi 智能 Git 提交助手(规范化 Conventional Commits 纯中文版,支持一键提审与推流)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.ts",
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"chinese"
|
|
20
20
|
],
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@earendil-works/pi-ai": ">=0.74.0 <
|
|
23
|
-
"@earendil-works/pi-coding-agent": ">=0.74.0 <
|
|
22
|
+
"@earendil-works/pi-ai": ">=0.74.0 <2.0.0",
|
|
23
|
+
"@earendil-works/pi-coding-agent": ">=0.74.0 <2.0.0"
|
|
24
24
|
},
|
|
25
25
|
"pi": {
|
|
26
26
|
"extensions": [
|
|
@@ -38,6 +38,9 @@ test('通过宿主统一接口继承当前模型并返回业务日志', async ()
|
|
|
38
38
|
const ctx = context(async (actualModel, prompt, options) => {
|
|
39
39
|
assert.equal(actualModel, model);
|
|
40
40
|
assert.match(prompt.messages[0].content[0].text, /修复冲线残留/);
|
|
41
|
+
// 提交规范必须随消息发送,不能依赖会被新版 pi-ai 静默丢弃的 context.systemPrompt。
|
|
42
|
+
assert.match(prompt.messages[0].content[0].text, /Conventional Commits/);
|
|
43
|
+
assert.equal(prompt.systemPrompt, undefined);
|
|
41
44
|
assert.equal(options.maxTokens, 4096);
|
|
42
45
|
assert.equal(options.reasoning, 'high');
|
|
43
46
|
return response('```text\nfix(racing): 清理冲线特效残留\n```');
|
|
@@ -117,6 +120,53 @@ test('拒绝空结果、异常完成、截断、非法格式和通用文案', as
|
|
|
117
120
|
await assert.rejects(generateCommitMessage({ modelRegistry: {} }, 'diff', []), /未选择模型/);
|
|
118
121
|
});
|
|
119
122
|
|
|
123
|
+
test('仅去除首行完整包装,保留正文', async () => {
|
|
124
|
+
for (const title of ['**fix(角色): 恢复原速播放**', '`fix(角色): 恢复原速播放`']) {
|
|
125
|
+
let calls = 0;
|
|
126
|
+
const result = await generateCommitMessage(context(async () => {
|
|
127
|
+
calls++;
|
|
128
|
+
return response(title + '\n\n保留原始动画节奏');
|
|
129
|
+
}), 'diff', ['角色.cs']);
|
|
130
|
+
assert.equal(result, 'fix(角色): 恢复原速播放\n\n保留原始动画节奏');
|
|
131
|
+
assert.equal(calls, 1);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('首行说明触发一次纠正,不直接猜测后面的候选标题', async () => {
|
|
136
|
+
let calls = 0;
|
|
137
|
+
const ctx = context(async (_model, prompt) => {
|
|
138
|
+
calls++;
|
|
139
|
+
if (calls === 1) return response('建议的提交信息:\nfix: 错误候选');
|
|
140
|
+
assert.match(prompt.messages[0].content[0].text, /上一次输出的首行不符合/);
|
|
141
|
+
return response('fix(角色): 恢复原速播放');
|
|
142
|
+
});
|
|
143
|
+
assert.equal(await generateCommitMessage(ctx, 'diff', ['角色.cs']), 'fix(角色): 恢复原速播放');
|
|
144
|
+
assert.equal(calls, 2);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test('纠正失败提供有界首行诊断,绝不无限重试', async () => {
|
|
148
|
+
let calls = 0;
|
|
149
|
+
await assert.rejects(generateCommitMessage(context(async () => {
|
|
150
|
+
calls++;
|
|
151
|
+
return response('说明文字' + '长'.repeat(300));
|
|
152
|
+
}), 'diff', ['角色.cs']), error => {
|
|
153
|
+
assert.match(error.message, /自动纠正一次后仍失败/);
|
|
154
|
+
assert.match(error.message, /实际首行/);
|
|
155
|
+
assert.ok(!error.message.includes('长'.repeat(161)));
|
|
156
|
+
return true;
|
|
157
|
+
});
|
|
158
|
+
assert.equal(calls, 2);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('异常完成不会进入格式纠正重试', async () => {
|
|
162
|
+
let calls = 0;
|
|
163
|
+
await assert.rejects(generateCommitMessage(context(async () => {
|
|
164
|
+
calls++;
|
|
165
|
+
return response('fix: 修复问题', 'error');
|
|
166
|
+
}), 'diff', []), /未正常完成生成/);
|
|
167
|
+
assert.equal(calls, 1);
|
|
168
|
+
});
|
|
169
|
+
|
|
120
170
|
test('生成失败不提交或推送,文件清单仅包含暂存区', async () => {
|
|
121
171
|
const repo = join(root, 'repo');
|
|
122
172
|
await mkdir(repo);
|
|
@@ -140,11 +190,11 @@ test('生成失败不提交或推送,文件清单仅包含暂存区', async ()
|
|
|
140
190
|
sendMessage: message => messages.push(message),
|
|
141
191
|
getThinkingLevel: () => 'max',
|
|
142
192
|
});
|
|
143
|
-
const ctx = { ...context(async () =>
|
|
193
|
+
const ctx = { ...context(async () => response('无法生成规范标题')), cwd: repo, hasUI: true, ui: { notify() {} } };
|
|
144
194
|
await commands.get('commit-push').handler('', ctx);
|
|
145
195
|
assert.equal(git('rev-parse', 'HEAD'), head);
|
|
146
196
|
assert.match(messages.at(-1).content, /未执行提交或推送/);
|
|
147
|
-
assert.match(messages.at(-1).content,
|
|
197
|
+
assert.match(messages.at(-1).content, /自动纠正一次后仍失败/);
|
|
148
198
|
assert.deepEqual(await getStagedFiles(repo), ['角色 文件.cs']);
|
|
149
199
|
// 兼容尚未提供 ctx.thinkingLevel 的宿主,从公开会话接口获取实际档位。
|
|
150
200
|
delete ctx.thinkingLevel;
|