fix: transition from removed apiClient to @mariozechner/pi-ai

This commit is contained in:
wassname
2026-04-23 15:20:19 +08:00
parent b9ec541e4f
commit 9c400d0e0a
2 changed files with 20 additions and 10 deletions
+1 -1
View File
@@ -79,7 +79,7 @@ export default function (pi: ExtensionAPI) {
}) })
pi.on("context", async (event, ctx) => { pi.on("context", async (event, ctx) => {
const prunedMessages = await applyPruning(event.messages, state, config, (ctx as any).apiClient) const prunedMessages = await applyPruning(event.messages, state, config, ctx.model)
return { messages: prunedMessages } return { messages: prunedMessages }
}) })
+19 -9
View File
@@ -132,7 +132,7 @@ async function generateSummary(
turns: any[], turns: any[],
previousSummary: string | null, previousSummary: string | null,
focusTopic: string | null, focusTopic: string | null,
apiClient: any, model: any,
): Promise<string | null> { ): Promise<string | null> {
const contentToSummarize = serializeForSummary(turns); const contentToSummarize = serializeForSummary(turns);
@@ -213,13 +213,23 @@ Prioritize preserving all information related to the focus topic.`;
} }
try { try {
const model = (apiClient && apiClient.model) ? apiClient.model : "gemini-2.0-flash"; if (!model) return null;
const response = await apiClient.chat.completions.create({ const piAi = await import("@mariozechner/pi-ai");
model: model, const response = await piAi.complete(model, {
messages: [{ role: "user", content: prompt }], messages: [{ role: "user", content: prompt, timestamp: Date.now() }]
max_tokens: 4000,
}); });
return response.choices[0]?.message?.content?.trim() || null;
let text = "";
if (Array.isArray(response.content)) {
text = response.content
.filter((c: any) => c.type === "text")
.map((c: any) => c.text)
.join("");
} else if (typeof (response as any).content === "string") {
text = (response as any).content;
}
return text.trim() || null;
} catch (e) { } catch (e) {
console.error("Summary generation failed:", e); console.error("Summary generation failed:", e);
return null; return null;
@@ -333,7 +343,7 @@ export async function applyPruning(
messages: any[], messages: any[],
state: DcpState, state: DcpState,
config: DcpConfig, config: DcpConfig,
apiClient: any model: any
): Promise<any[]> { ): Promise<any[]> {
const msgs = messages.map((m: any) => { const msgs = messages.map((m: any) => {
const clone = { ...m }; const clone = { ...m };
@@ -369,7 +379,7 @@ export async function applyPruning(
if (compressStart < compressEnd) { if (compressStart < compressEnd) {
const middle = msgs.slice(compressStart, compressEnd); const middle = msgs.slice(compressStart, compressEnd);
const summary = await generateSummary(middle, state.previousSummary, null, apiClient); const summary = await generateSummary(middle, state.previousSummary, null, model);
if (summary) { if (summary) {
const compressed: any[] = []; const compressed: any[] = [];