mirror of
https://github.com/wassname/pi-auto-compressor.git
synced 2026-06-27 16:46:09 +08:00
fix: transition from removed apiClient to @mariozechner/pi-ai
This commit is contained in:
@@ -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 }
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -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[] = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user