-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaskRun.ts
More file actions
149 lines (138 loc) Β· 4.76 KB
/
Copy pathaskRun.ts
File metadata and controls
149 lines (138 loc) Β· 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { logInfo, logWarn } from "../../evlog.js";
import { buildAskSystemPrompt } from "./askPrompt.js";
import { formatAskReply } from "./formatAskReply.js";
import { buildContext7Tools } from "../tools/context7Tools.js";
import {
ASK_FAILURE_MESSAGE,
ASK_META_REFUSAL,
ASK_RETRY_NUDGE,
CONTEXT7_RESPONSE_BYTES,
MAX_ASK_FINALIZE_ROUNDS,
MAX_ASK_TOOL_ROUNDS,
} from "../../settings/index.js";
import { createFeaturePiSession } from "../runtime/createFeatureSession.js";
import { buildAskUserContent } from "./askUserContent.js";
import type { AskRunParams, AskRunResult } from "./askRunTypes.js";
import { mergeExactUsage } from "../providers/usageMetadata.js";
import { classifyAskQuestionIntent } from "./askSafety.js";
import { buildAskRunSetup } from "./askRunSetup.js";
import {
createRateLimitCircuit,
runWithRateLimitCircuit,
wrapExecutorsWithRateLimitCircuit,
} from "../../github/rateLimitCircuit.js";
import {
getSharedRateLimitCircuit,
openSharedRateLimitCircuitBestEffort,
} from "../../github/sharedRateLimitCircuit.js";
export async function runAskRun(params: AskRunParams): Promise<AskRunResult> {
const { cfg, question, replyTarget } = params;
if (classifyAskQuestionIntent(question) === "bot_meta") {
logInfo("ask_meta_refusal", { owner: params.owner, repo: params.repo, pr: params.prNumber });
logInfo("ask_run_completed", {
toolRounds: 0,
rateLimitCircuitOpened: false,
hasAnswer: true,
metaRefusal: true,
});
return {
answer: formatAskReply({
question,
answer: ASK_META_REFUSAL,
replyTarget,
}),
replied: true,
};
}
const installationId = params.durability?.installationId ?? 0;
const durabilityPool = params.durability?.pool;
const circuit = createRateLimitCircuit({
installationId,
onOpened: (kind) => {
openSharedRateLimitCircuitBestEffort(durabilityPool, {
installationId,
lastErrorKind: kind,
});
},
});
if (durabilityPool != null && installationId > 0) {
try {
const sharedCircuit = await getSharedRateLimitCircuit(durabilityPool, installationId);
if (sharedCircuit != null && sharedCircuit.openUntil.getTime() > Date.now()) {
circuit.hydrateOpenFromShared(
sharedCircuit.lastErrorKind === "secondary" ? "secondary" : "primary",
sharedCircuit.openUntil,
);
logInfo("github_shared_rate_limit_circuit_honored", {
installationId,
type: "ask",
});
}
} catch (error) {
// Best-effort shared read: DB blips must not abort the ask run.
logWarn("github_shared_rate_limit_circuit_read_failed", {
installationId,
type: "ask",
message: error instanceof Error ? error.message : String(error),
});
}
}
return runWithRateLimitCircuit(circuit, async () => {
const { bundle } = buildAskRunSetup(params);
const ctx7 = buildContext7Tools({
apiKey: cfg.context7ApiKey,
maxResponseBytes: CONTEXT7_RESPONSE_BYTES,
});
const tools = [...bundle.piTools, ...ctx7.piTools];
const executors = wrapExecutorsWithRateLimitCircuit({
...bundle.executors,
...ctx7.executors,
});
const session = await createFeaturePiSession({
role: "ask",
cfg,
cwd: params.cwd,
systemPrompt: buildAskSystemPrompt(),
tools,
executors,
durability: params.durability,
});
try {
const sendOpts = {
maxToolRounds: MAX_ASK_TOOL_ROUNDS,
phase: "ask" as const,
checkpointId: "ask:ask",
};
let usage: AskRunResult["usage"];
const firstTurn = await session.send(buildAskUserContent(params), sendOpts);
usage = mergeExactUsage(usage, firstTurn.usage);
let lastText = firstTurn.text.trim();
if (!lastText && MAX_ASK_FINALIZE_ROUNDS > 0) {
for (let round = 0; round < MAX_ASK_FINALIZE_ROUNDS && !lastText; round++) {
const finalizeTurn = await session.send(ASK_RETRY_NUDGE, {
phase: "ask",
checkpointId: "ask:ask",
// Keep tool definitions registered for cache prefixes; forbid tool turns.
maxToolRounds: 0,
});
usage = mergeExactUsage(usage, finalizeTurn.usage);
lastText = finalizeTurn.text.trim();
}
}
const answerText = formatAskReply({
question,
answer: lastText.length > 0 ? lastText : ASK_FAILURE_MESSAGE,
replyTarget,
});
logInfo("ask_run_completed", {
provider: cfg.piProvider,
hasAnswer: lastText.length > 0,
metaRefusal: false,
rateLimitCircuitOpened: circuit.isOpen(),
});
return { answer: answerText, replied: true, ...(usage ? { usage } : {}) };
} finally {
await session.dispose();
}
});
}