@jsonstudio/llms 0.6.753 → 0.6.795
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.
- package/dist/conversion/compat/actions/apply-patch-fixer.d.ts +1 -0
- package/dist/conversion/compat/actions/apply-patch-fixer.js +30 -0
- package/dist/conversion/compat/actions/apply-patch-format-fixer.d.ts +1 -0
- package/dist/conversion/compat/actions/apply-patch-format-fixer.js +233 -0
- package/dist/conversion/compat/actions/index.d.ts +2 -0
- package/dist/conversion/compat/actions/index.js +2 -0
- package/dist/conversion/compat/profiles/chat-gemini.json +15 -15
- package/dist/conversion/compat/profiles/chat-glm.json +194 -194
- package/dist/conversion/compat/profiles/chat-iflow.json +199 -199
- package/dist/conversion/compat/profiles/chat-lmstudio.json +43 -43
- package/dist/conversion/compat/profiles/chat-qwen.json +20 -20
- package/dist/conversion/compat/profiles/responses-c4m.json +42 -42
- package/dist/conversion/compat/profiles/responses-output2choices-test.json +10 -9
- package/dist/conversion/hub/pipeline/hub-pipeline.d.ts +6 -0
- package/dist/conversion/hub/pipeline/hub-pipeline.js +35 -0
- package/dist/conversion/shared/bridge-message-utils.d.ts +1 -0
- package/dist/conversion/shared/bridge-message-utils.js +7 -0
- package/dist/conversion/shared/bridge-policies.js +8 -8
- package/dist/conversion/shared/tool-governor.js +18 -23
- package/dist/filters/special/response-tool-arguments-stringify.js +3 -22
- package/dist/router/virtual-router/engine.d.ts +5 -0
- package/dist/router/virtual-router/engine.js +21 -0
- package/dist/tools/apply-patch/regression-capturer.d.ts +12 -0
- package/dist/tools/apply-patch/regression-capturer.js +112 -0
- package/dist/tools/apply-patch/structured.d.ts +20 -0
- package/dist/tools/apply-patch/structured.js +441 -0
- package/dist/tools/apply-patch/validator.d.ts +8 -0
- package/dist/tools/apply-patch/validator.js +466 -0
- package/dist/tools/apply-patch-structured.d.ts +1 -20
- package/dist/tools/apply-patch-structured.js +1 -277
- package/dist/tools/args-json.d.ts +1 -0
- package/dist/tools/args-json.js +175 -0
- package/dist/tools/exec-command/normalize.d.ts +17 -0
- package/dist/tools/exec-command/normalize.js +112 -0
- package/dist/tools/exec-command/regression-capturer.d.ts +11 -0
- package/dist/tools/exec-command/regression-capturer.js +144 -0
- package/dist/tools/exec-command/validator.d.ts +6 -0
- package/dist/tools/exec-command/validator.js +22 -0
- package/dist/tools/patch-args-normalizer.d.ts +15 -0
- package/dist/tools/patch-args-normalizer.js +472 -0
- package/dist/tools/patch-regression-capturer.d.ts +1 -0
- package/dist/tools/patch-regression-capturer.js +1 -0
- package/dist/tools/tool-registry.js +36 -541
- package/package.json +1 -1
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
"id": "responses:output2choices-test",
|
|
3
|
+
"protocol": "openai-responses",
|
|
4
|
+
"response": {
|
|
5
|
+
"mappings": [
|
|
6
|
+
{
|
|
7
|
+
"action": "convert_responses_output_to_choices"
|
|
8
|
+
}
|
|
9
|
+
]
|
|
10
|
+
}
|
|
11
11
|
}
|
|
12
|
+
|
|
@@ -64,7 +64,13 @@ export declare class HubPipeline {
|
|
|
64
64
|
private config;
|
|
65
65
|
private unsubscribeProviderErrors?;
|
|
66
66
|
constructor(config: HubPipelineConfig);
|
|
67
|
+
updateRuntimeDeps(deps: {
|
|
68
|
+
healthStore?: HubPipelineConfig['healthStore'] | null;
|
|
69
|
+
routingStateStore?: HubPipelineConfig['routingStateStore'] | null;
|
|
70
|
+
quotaView?: HubPipelineConfig['quotaView'] | null;
|
|
71
|
+
}): void;
|
|
67
72
|
updateVirtualRouterConfig(nextConfig: VirtualRouterConfig): void;
|
|
73
|
+
dispose(): void;
|
|
68
74
|
private executeRequestStagePipeline;
|
|
69
75
|
execute(request: HubPipelineRequest): Promise<HubPipelineResult>;
|
|
70
76
|
private captureAnthropicAliasMap;
|
|
@@ -51,6 +51,30 @@ export class HubPipeline {
|
|
|
51
51
|
this.unsubscribeProviderErrors = undefined;
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
+
updateRuntimeDeps(deps) {
|
|
55
|
+
if (!deps || typeof deps !== 'object') {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if ('healthStore' in deps) {
|
|
59
|
+
this.config.healthStore = deps.healthStore ?? undefined;
|
|
60
|
+
}
|
|
61
|
+
if ('routingStateStore' in deps) {
|
|
62
|
+
this.config.routingStateStore = (deps.routingStateStore ?? undefined);
|
|
63
|
+
}
|
|
64
|
+
if ('quotaView' in deps) {
|
|
65
|
+
this.config.quotaView = deps.quotaView ?? undefined;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
this.routerEngine.updateDeps({
|
|
69
|
+
healthStore: this.config.healthStore ?? null,
|
|
70
|
+
routingStateStore: (this.config.routingStateStore ?? null),
|
|
71
|
+
quotaView: this.config.quotaView ?? null
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
// best-effort: runtime deps updates must never break routing
|
|
76
|
+
}
|
|
77
|
+
}
|
|
54
78
|
updateVirtualRouterConfig(nextConfig) {
|
|
55
79
|
if (!nextConfig || typeof nextConfig !== 'object') {
|
|
56
80
|
throw new Error('HubPipeline updateVirtualRouterConfig requires VirtualRouterConfig payload');
|
|
@@ -58,6 +82,17 @@ export class HubPipeline {
|
|
|
58
82
|
this.config.virtualRouter = nextConfig;
|
|
59
83
|
this.routerEngine.initialize(nextConfig);
|
|
60
84
|
}
|
|
85
|
+
dispose() {
|
|
86
|
+
if (this.unsubscribeProviderErrors) {
|
|
87
|
+
try {
|
|
88
|
+
this.unsubscribeProviderErrors();
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// ignore dispose failures
|
|
92
|
+
}
|
|
93
|
+
this.unsubscribeProviderErrors = undefined;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
61
96
|
async executeRequestStagePipeline(normalized, hooks) {
|
|
62
97
|
const formatAdapter = hooks.createFormatAdapter();
|
|
63
98
|
const semanticMapper = hooks.createSemanticMapper();
|
|
@@ -20,3 +20,4 @@ export interface BridgeInputToChatOptions {
|
|
|
20
20
|
toolResultFallbackText?: string;
|
|
21
21
|
}
|
|
22
22
|
export declare function convertBridgeInputToChatMessages(options: BridgeInputToChatOptions): Array<Record<string, unknown>>;
|
|
23
|
+
export declare function ensureMessagesArray(state: any): Array<Record<string, unknown>>;
|
|
@@ -644,3 +644,10 @@ export function convertBridgeInputToChatMessages(options) {
|
|
|
644
644
|
}
|
|
645
645
|
return messages;
|
|
646
646
|
}
|
|
647
|
+
export function ensureMessagesArray(state) {
|
|
648
|
+
if (Array.isArray(state.messages))
|
|
649
|
+
return state.messages;
|
|
650
|
+
if (!state.messages)
|
|
651
|
+
state.messages = [];
|
|
652
|
+
return state.messages;
|
|
653
|
+
}
|
|
@@ -171,7 +171,7 @@ const OPENAI_CHAT_POLICY = {
|
|
|
171
171
|
request: {
|
|
172
172
|
inbound: [
|
|
173
173
|
reasoningAction('openai_chat_reasoning'),
|
|
174
|
-
toolCallNormalizationAction('openai_chat_tool_call'),
|
|
174
|
+
toolCallNormalizationAction('openai_chat_tool_call'), { name: 'compat.fix-apply-patch' },
|
|
175
175
|
{ name: 'messages.ensure-system-instruction' },
|
|
176
176
|
{ name: 'metadata.extra-fields', options: { allowedKeys: OPENAI_CHAT_ALLOWED_FIELDS } },
|
|
177
177
|
{ name: 'metadata.provider-field', options: { field: 'metadata', target: 'providerMetadata' } },
|
|
@@ -180,7 +180,7 @@ const OPENAI_CHAT_POLICY = {
|
|
|
180
180
|
outbound: [
|
|
181
181
|
{ name: 'messages.normalize-history' },
|
|
182
182
|
{ name: 'tools.capture-results' },
|
|
183
|
-
toolCallNormalizationAction('openai_chat_tool_call'),
|
|
183
|
+
toolCallNormalizationAction('openai_chat_tool_call'), { name: 'compat.fix-apply-patch' },
|
|
184
184
|
{ name: 'tools.ensure-placeholders' },
|
|
185
185
|
{ name: 'messages.ensure-output-fields', options: { toolFallback: 'Tool call completed (no output).' } },
|
|
186
186
|
{ name: 'messages.ensure-system-instruction' },
|
|
@@ -193,12 +193,12 @@ const OPENAI_CHAT_POLICY = {
|
|
|
193
193
|
response: {
|
|
194
194
|
inbound: [
|
|
195
195
|
reasoningAction('openai_chat_reasoning'),
|
|
196
|
-
toolCallNormalizationAction('openai_chat_tool_call'),
|
|
196
|
+
toolCallNormalizationAction('openai_chat_tool_call'), { name: 'compat.fix-apply-patch' },
|
|
197
197
|
{ name: 'metadata.extra-fields', options: { allowedKeys: OPENAI_CHAT_ALLOWED_FIELDS } }
|
|
198
198
|
],
|
|
199
199
|
outbound: [
|
|
200
200
|
reasoningAction('openai_chat_reasoning'),
|
|
201
|
-
toolCallNormalizationAction('openai_chat_tool_call'),
|
|
201
|
+
toolCallNormalizationAction('openai_chat_tool_call'), { name: 'compat.fix-apply-patch' },
|
|
202
202
|
{ name: 'metadata.extra-fields', options: { allowedKeys: OPENAI_CHAT_ALLOWED_FIELDS } }
|
|
203
203
|
]
|
|
204
204
|
}
|
|
@@ -242,7 +242,7 @@ const GEMINI_POLICY = {
|
|
|
242
242
|
protocol: 'gemini-chat',
|
|
243
243
|
request: {
|
|
244
244
|
inbound: [
|
|
245
|
-
reasoningAction('gemini_reasoning'),
|
|
245
|
+
reasoningAction('gemini_reasoning'), { name: 'compat.fix-apply-patch' },
|
|
246
246
|
{ name: 'messages.ensure-system-instruction' },
|
|
247
247
|
{ name: 'metadata.extra-fields', options: { allowedKeys: GEMINI_ALLOWED_FIELDS } }
|
|
248
248
|
],
|
|
@@ -252,17 +252,17 @@ const GEMINI_POLICY = {
|
|
|
252
252
|
{ name: 'tools.ensure-placeholders' },
|
|
253
253
|
{ name: 'messages.ensure-output-fields', options: { toolFallback: 'Tool call completed (no output).' } },
|
|
254
254
|
{ name: 'messages.ensure-system-instruction' },
|
|
255
|
-
reasoningAction('gemini_reasoning'),
|
|
255
|
+
reasoningAction('gemini_reasoning'), { name: 'compat.fix-apply-patch' },
|
|
256
256
|
{ name: 'metadata.extra-fields', options: { allowedKeys: GEMINI_ALLOWED_FIELDS } }
|
|
257
257
|
]
|
|
258
258
|
},
|
|
259
259
|
response: {
|
|
260
260
|
inbound: [
|
|
261
|
-
reasoningAction('gemini_reasoning'),
|
|
261
|
+
reasoningAction('gemini_reasoning'), { name: 'compat.fix-apply-patch' },
|
|
262
262
|
{ name: 'metadata.extra-fields', options: { allowedKeys: GEMINI_ALLOWED_FIELDS } }
|
|
263
263
|
],
|
|
264
264
|
outbound: [
|
|
265
|
-
reasoningAction('gemini_reasoning'),
|
|
265
|
+
reasoningAction('gemini_reasoning'), { name: 'compat.fix-apply-patch' },
|
|
266
266
|
{ name: 'metadata.extra-fields', options: { allowedKeys: GEMINI_ALLOWED_FIELDS } }
|
|
267
267
|
]
|
|
268
268
|
}
|
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
// enforceChatBudget: 为避免在请求侧引入多余依赖,这里提供最小实现(保留形状,不裁剪)。
|
|
5
5
|
import { augmentOpenAITools } from '../../guidance/index.js';
|
|
6
6
|
import { validateToolCall } from '../../tools/tool-registry.js';
|
|
7
|
-
import {
|
|
7
|
+
import { captureApplyPatchRegression } from '../../tools/patch-regression-capturer.js';
|
|
8
|
+
import { normalizeExecCommandArgs } from '../../tools/exec-command/normalize.js';
|
|
8
9
|
function isObject(v) { return !!v && typeof v === 'object' && !Array.isArray(v); }
|
|
9
10
|
// Note: tool schema strict augmentation removed per alignment
|
|
10
11
|
function enforceChatBudget(chat, _modelId) { return chat; }
|
|
@@ -245,6 +246,13 @@ export function normalizeApplyPatchToolCallsOnResponse(chat) {
|
|
|
245
246
|
else if (validation && !validation.ok) {
|
|
246
247
|
try {
|
|
247
248
|
const reason = validation.reason ?? 'unknown';
|
|
249
|
+
captureApplyPatchRegression({
|
|
250
|
+
errorType: reason,
|
|
251
|
+
originalArgs: rawArgs,
|
|
252
|
+
normalizedArgs: argsStr,
|
|
253
|
+
validationError: reason,
|
|
254
|
+
source: 'tool-governor.response'
|
|
255
|
+
});
|
|
248
256
|
const snippet = typeof argsStr === 'string' && argsStr.trim().length
|
|
249
257
|
? argsStr.trim().slice(0, 200).replace(/\s+/g, ' ')
|
|
250
258
|
: '';
|
|
@@ -312,6 +320,13 @@ function normalizeSpecialToolCallsOnRequest(request) {
|
|
|
312
320
|
else if (validation && !validation.ok) {
|
|
313
321
|
try {
|
|
314
322
|
const reason = validation.reason ?? 'unknown';
|
|
323
|
+
captureApplyPatchRegression({
|
|
324
|
+
errorType: reason,
|
|
325
|
+
originalArgs: rawArgs,
|
|
326
|
+
normalizedArgs: argsStr,
|
|
327
|
+
validationError: reason,
|
|
328
|
+
source: 'tool-governor.request'
|
|
329
|
+
});
|
|
315
330
|
const snippet = typeof argsStr === 'string' && argsStr.trim().length
|
|
316
331
|
? argsStr.trim().slice(0, 200).replace(/\s+/g, ' ')
|
|
317
332
|
: '';
|
|
@@ -336,8 +351,9 @@ function normalizeSpecialToolCallsOnRequest(request) {
|
|
|
336
351
|
}
|
|
337
352
|
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
338
353
|
const normalized = normalizeExecCommandArgs(parsed);
|
|
354
|
+
const next = normalized.ok ? normalized.normalized : parsed;
|
|
339
355
|
try {
|
|
340
|
-
fn.arguments = JSON.stringify(
|
|
356
|
+
fn.arguments = JSON.stringify(next ?? {});
|
|
341
357
|
}
|
|
342
358
|
catch {
|
|
343
359
|
fn.arguments = '{}';
|
|
@@ -356,27 +372,6 @@ function normalizeSpecialToolCallsOnRequest(request) {
|
|
|
356
372
|
return request;
|
|
357
373
|
}
|
|
358
374
|
}
|
|
359
|
-
function normalizeExecCommandArgs(args) {
|
|
360
|
-
try {
|
|
361
|
-
const out = { ...args };
|
|
362
|
-
const rawCmd = typeof out.cmd === 'string' && out.cmd.trim().length
|
|
363
|
-
? String(out.cmd)
|
|
364
|
-
: typeof out.command === 'string' && out.command.trim().length
|
|
365
|
-
? String(out.command)
|
|
366
|
-
: undefined;
|
|
367
|
-
if (rawCmd) {
|
|
368
|
-
const fixed = repairFindMeta(rawCmd);
|
|
369
|
-
out.cmd = fixed;
|
|
370
|
-
if (typeof out.command === 'string') {
|
|
371
|
-
out.command = fixed;
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
return out;
|
|
375
|
-
}
|
|
376
|
-
catch {
|
|
377
|
-
return args;
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
375
|
function enhanceResponseToolArguments(chat) {
|
|
381
376
|
try {
|
|
382
377
|
const enable = String(process?.env?.RCC_TOOL_ENHANCE ?? '1').trim() !== '0';
|
|
@@ -1,28 +1,8 @@
|
|
|
1
1
|
import { repairFindMeta } from '../../conversion/shared/tooling.js';
|
|
2
|
+
import { normalizeExecCommandArgs } from '../../tools/exec-command/normalize.js';
|
|
2
3
|
function isObject(v) {
|
|
3
4
|
return !!v && typeof v === 'object' && !Array.isArray(v);
|
|
4
5
|
}
|
|
5
|
-
function normalizeExecCommandArgs(args) {
|
|
6
|
-
try {
|
|
7
|
-
const out = { ...args };
|
|
8
|
-
const rawCmd = typeof out.cmd === 'string' && out.cmd.trim().length
|
|
9
|
-
? String(out.cmd)
|
|
10
|
-
: typeof out.command === 'string' && out.command.trim().length
|
|
11
|
-
? String(out.command)
|
|
12
|
-
: undefined;
|
|
13
|
-
if (rawCmd) {
|
|
14
|
-
const fixed = repairFindMeta(rawCmd);
|
|
15
|
-
out.cmd = fixed;
|
|
16
|
-
if (typeof out.command === 'string') {
|
|
17
|
-
out.command = fixed;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return out;
|
|
21
|
-
}
|
|
22
|
-
catch {
|
|
23
|
-
return args;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
6
|
function packShellCommand(cmd) {
|
|
27
7
|
// Normalize into ["bash","-lc","<single string>"] to support pipes, parens, -exec, etc.
|
|
28
8
|
const normalizeArray = (argv) => {
|
|
@@ -104,8 +84,9 @@ export class ResponseToolArgumentsStringifyFilter {
|
|
|
104
84
|
}
|
|
105
85
|
else if ((name === 'exec_command' || name === 'shell_command' || name === 'bash') && isObject(parsed)) {
|
|
106
86
|
const normalized = normalizeExecCommandArgs(parsed);
|
|
87
|
+
const next = normalized.ok ? normalized.normalized : parsed;
|
|
107
88
|
try {
|
|
108
|
-
fn.arguments = JSON.stringify(
|
|
89
|
+
fn.arguments = JSON.stringify(next ?? {});
|
|
109
90
|
}
|
|
110
91
|
catch {
|
|
111
92
|
fn.arguments = '{}';
|
|
@@ -29,6 +29,11 @@ export declare class VirtualRouterEngine {
|
|
|
29
29
|
routingStateStore?: RoutingInstructionStateStore;
|
|
30
30
|
quotaView?: ProviderQuotaView;
|
|
31
31
|
});
|
|
32
|
+
updateDeps(deps: {
|
|
33
|
+
healthStore?: VirtualRouterHealthStore | null;
|
|
34
|
+
routingStateStore?: RoutingInstructionStateStore | null;
|
|
35
|
+
quotaView?: ProviderQuotaView | null;
|
|
36
|
+
}): void;
|
|
32
37
|
private parseDirectProviderModel;
|
|
33
38
|
initialize(config: VirtualRouterConfig): void;
|
|
34
39
|
route(request: StandardizedRequest | ProcessedRequest, metadata: RouterMetadataInput): {
|
|
@@ -44,6 +44,27 @@ export class VirtualRouterEngine {
|
|
|
44
44
|
this.quotaView = deps.quotaView;
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
+
updateDeps(deps) {
|
|
48
|
+
if (!deps || typeof deps !== 'object') {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if ('healthStore' in deps) {
|
|
52
|
+
this.healthStore = deps.healthStore ?? undefined;
|
|
53
|
+
}
|
|
54
|
+
if ('routingStateStore' in deps) {
|
|
55
|
+
this.routingStateStore =
|
|
56
|
+
deps.routingStateStore ??
|
|
57
|
+
{
|
|
58
|
+
loadSync: loadRoutingInstructionStateSync,
|
|
59
|
+
saveAsync: saveRoutingInstructionStateAsync
|
|
60
|
+
};
|
|
61
|
+
// Routing state store changes require clearing in-memory cache to avoid stale reads.
|
|
62
|
+
this.routingInstructionState.clear();
|
|
63
|
+
}
|
|
64
|
+
if ('quotaView' in deps) {
|
|
65
|
+
this.quotaView = deps.quotaView ?? undefined;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
47
68
|
parseDirectProviderModel(model) {
|
|
48
69
|
const raw = typeof model === 'string' ? model.trim() : '';
|
|
49
70
|
if (!raw) {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface RegressionSample {
|
|
2
|
+
id: string;
|
|
3
|
+
timestamp: string;
|
|
4
|
+
errorType: string;
|
|
5
|
+
originalArgs: string;
|
|
6
|
+
normalizedArgs?: string;
|
|
7
|
+
fixerResult?: string;
|
|
8
|
+
validationError?: string;
|
|
9
|
+
source?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function captureApplyPatchRegression(sample: Omit<RegressionSample, 'id' | 'timestamp'>): void;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import * as os from 'os';
|
|
4
|
+
import { createHash } from 'crypto';
|
|
5
|
+
/**
|
|
6
|
+
* Captures apply_patch payload regressions into a golden samples structure.
|
|
7
|
+
*
|
|
8
|
+
* Trigger: apply_patch validation failure OR fixer failure
|
|
9
|
+
* Condition: non-context errors only (format/json/prefix/etc.)
|
|
10
|
+
*
|
|
11
|
+
* Default destination:
|
|
12
|
+
* ~/.routecodex/golden_samples/ci-regression/apply_patch/<error-type>/
|
|
13
|
+
*
|
|
14
|
+
* Optional repo destination (explicit opt-in):
|
|
15
|
+
* ROUTECODEX_APPLY_PATCH_REGRESSION_TO_REPO=1
|
|
16
|
+
* → <repoRoot>/samples/ci-goldens/_regressions/apply_patch/<error-type>/
|
|
17
|
+
*
|
|
18
|
+
* Explicit override:
|
|
19
|
+
* ROUTECODEX_APPLY_PATCH_REGRESSION_DIR=/abs/path
|
|
20
|
+
*/
|
|
21
|
+
const MAX_SAMPLES_PER_TYPE = 50;
|
|
22
|
+
function detectRepoRootFromCwd() {
|
|
23
|
+
try {
|
|
24
|
+
let dir = process.cwd();
|
|
25
|
+
for (let i = 0; i < 12; i += 1) {
|
|
26
|
+
const marker = path.join(dir, 'samples', 'ci-goldens');
|
|
27
|
+
const pkg = path.join(dir, 'package.json');
|
|
28
|
+
if (fs.existsSync(marker) && fs.existsSync(pkg)) {
|
|
29
|
+
try {
|
|
30
|
+
const raw = fs.readFileSync(pkg, 'utf-8');
|
|
31
|
+
const json = JSON.parse(raw);
|
|
32
|
+
if (json && typeof json === 'object' && String(json.name || '') === 'routecodex') {
|
|
33
|
+
return dir;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
// ignore parse errors
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const parent = path.dirname(dir);
|
|
41
|
+
if (parent === dir)
|
|
42
|
+
break;
|
|
43
|
+
dir = parent;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// ignore
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
function resolveSamplesRoot() {
|
|
52
|
+
const explicit = String(process?.env?.ROUTECODEX_APPLY_PATCH_REGRESSION_DIR || '').trim();
|
|
53
|
+
if (explicit) {
|
|
54
|
+
return path.resolve(explicit);
|
|
55
|
+
}
|
|
56
|
+
const toRepo = String(process?.env?.ROUTECODEX_APPLY_PATCH_REGRESSION_TO_REPO || '').trim() === '1';
|
|
57
|
+
if (toRepo) {
|
|
58
|
+
const repoRoot = detectRepoRootFromCwd();
|
|
59
|
+
if (repoRoot) {
|
|
60
|
+
return path.join(repoRoot, 'samples', 'ci-goldens', '_regressions', 'apply_patch');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return path.join(os.homedir(), '.routecodex', 'golden_samples', 'ci-regression', 'apply_patch');
|
|
64
|
+
}
|
|
65
|
+
function isContextError(error) {
|
|
66
|
+
if (!error)
|
|
67
|
+
return false;
|
|
68
|
+
const lower = error.toLowerCase();
|
|
69
|
+
return (lower.includes('failed to find context') ||
|
|
70
|
+
lower.includes('failed to find expected lines') ||
|
|
71
|
+
lower.includes('file not found') ||
|
|
72
|
+
lower.includes('no such file'));
|
|
73
|
+
}
|
|
74
|
+
function stableSampleId(sample) {
|
|
75
|
+
const key = `${String(sample.errorType || 'unknown')}:${String(sample.originalArgs ?? '')}:` +
|
|
76
|
+
`${String(sample.normalizedArgs ?? '')}:${String(sample.fixerResult ?? '')}:${String(sample.validationError ?? '')}`;
|
|
77
|
+
return createHash('sha1').update(key).digest('hex').slice(0, 16);
|
|
78
|
+
}
|
|
79
|
+
export function captureApplyPatchRegression(sample) {
|
|
80
|
+
try {
|
|
81
|
+
if (isContextError(sample.validationError))
|
|
82
|
+
return;
|
|
83
|
+
const root = resolveSamplesRoot();
|
|
84
|
+
const safeType = String(sample.errorType || 'unknown').replace(/[^a-z0-9-]/gi, '_');
|
|
85
|
+
const typeDir = path.join(root, safeType);
|
|
86
|
+
if (!fs.existsSync(typeDir)) {
|
|
87
|
+
fs.mkdirSync(typeDir, { recursive: true });
|
|
88
|
+
}
|
|
89
|
+
// Check limit (best-effort; keep runtime safe)
|
|
90
|
+
try {
|
|
91
|
+
const existing = fs.readdirSync(typeDir).filter((f) => f.endsWith('.json'));
|
|
92
|
+
if (existing.length >= MAX_SAMPLES_PER_TYPE)
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
// ignore
|
|
97
|
+
}
|
|
98
|
+
const id = `sample_${stableSampleId(sample)}`;
|
|
99
|
+
const file = path.join(typeDir, `${id}.json`);
|
|
100
|
+
if (fs.existsSync(file))
|
|
101
|
+
return;
|
|
102
|
+
const fullSample = {
|
|
103
|
+
id,
|
|
104
|
+
timestamp: new Date().toISOString(),
|
|
105
|
+
...sample
|
|
106
|
+
};
|
|
107
|
+
fs.writeFileSync(file, JSON.stringify(fullSample, null, 2), 'utf-8');
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
// Silently fail to avoid disrupting runtime
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type StructuredApplyPatchKind = 'insert_after' | 'insert_before' | 'replace' | 'delete' | 'create_file' | 'delete_file';
|
|
2
|
+
export interface StructuredApplyPatchChange {
|
|
3
|
+
file?: string;
|
|
4
|
+
kind: StructuredApplyPatchKind | string;
|
|
5
|
+
anchor?: string;
|
|
6
|
+
target?: string;
|
|
7
|
+
lines?: string[] | string;
|
|
8
|
+
use_anchor_indent?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface StructuredApplyPatchPayload extends Record<string, unknown> {
|
|
11
|
+
instructions?: string;
|
|
12
|
+
file?: string;
|
|
13
|
+
changes: StructuredApplyPatchChange[];
|
|
14
|
+
}
|
|
15
|
+
export declare class StructuredApplyPatchError extends Error {
|
|
16
|
+
reason: string;
|
|
17
|
+
constructor(reason: string, message: string);
|
|
18
|
+
}
|
|
19
|
+
export declare function buildStructuredPatch(payload: StructuredApplyPatchPayload): string;
|
|
20
|
+
export declare function isStructuredApplyPatchPayload(candidate: unknown): candidate is StructuredApplyPatchPayload;
|