pi-codebuddy-sdk 0.1.14 → 0.1.15
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/package.json +1 -1
- package/src/provider.ts +1 -67
package/package.json
CHANGED
package/src/provider.ts
CHANGED
|
@@ -62,31 +62,14 @@ function makeErrorPartial(model: Model<any>, error: unknown): AssistantMessage {
|
|
|
62
62
|
*/
|
|
63
63
|
function extractUserMessage(context: Context): string {
|
|
64
64
|
const msgs = context.messages;
|
|
65
|
-
|
|
66
|
-
// Pi injects context-mode state as the LAST user message (after the real one).
|
|
67
|
-
// Search from the end, skip obvious context-mode injections.
|
|
68
65
|
for (let i = msgs.length - 1; i >= 0; i--) {
|
|
69
66
|
if (msgs[i].role !== "user") continue;
|
|
70
67
|
const raw = contentToText((msgs[i] as any).content);
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
// Skip messages that are entirely context-mode injection
|
|
74
|
-
if (isContextModeInjection(raw)) continue;
|
|
75
|
-
|
|
76
|
-
return stripPiInjection(raw);
|
|
68
|
+
if (raw) return raw.trim();
|
|
77
69
|
}
|
|
78
70
|
return "Continue.";
|
|
79
71
|
}
|
|
80
72
|
|
|
81
|
-
/** Detect if a user message is a Pi context-mode state injection (not a real message). */
|
|
82
|
-
function isContextModeInjection(text: string): boolean {
|
|
83
|
-
const t = text.trim();
|
|
84
|
-
if (t.startsWith("context-mode active.")) return true;
|
|
85
|
-
if (t.includes("<session_state") || t.includes("<session_mode")) return true;
|
|
86
|
-
if (t.startsWith("<summary>") && t.includes("</summary>")) return true;
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
73
|
function contentToText(content: unknown): string {
|
|
91
74
|
if (typeof content === "string") return content;
|
|
92
75
|
if (!Array.isArray(content)) return "";
|
|
@@ -96,55 +79,6 @@ function contentToText(content: unknown): string {
|
|
|
96
79
|
.join("\n");
|
|
97
80
|
}
|
|
98
81
|
|
|
99
|
-
/**
|
|
100
|
-
* Strip context-mode session state that Pi prepends to user messages.
|
|
101
|
-
*
|
|
102
|
-
* Pi injects several formats:
|
|
103
|
-
* context-mode active. Hierarchy: ...
|
|
104
|
-
* <session_state source="compaction"> ... </session_state>
|
|
105
|
-
* <summary>Compacted: ...</summary>
|
|
106
|
-
*
|
|
107
|
-
* Strip all of them, keep only the actual user message.
|
|
108
|
-
*/
|
|
109
|
-
function stripPiInjection(text: string): string {
|
|
110
|
-
// Fast path
|
|
111
|
-
if (!text.includes("context-mode") && !text.includes("<session_state") && !text.includes("<summary>")) {
|
|
112
|
-
return text;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// 1. Remove <summary> blocks (compaction summaries contain context-mode state)
|
|
116
|
-
text = text.replace(/<summary>[\s\S]*?<\/summary>\s*/g, "");
|
|
117
|
-
|
|
118
|
-
// 2. Remove <session_state> block and everything between "context-mode" and </session_state>
|
|
119
|
-
const sessionEnd = text.lastIndexOf("</session_state>");
|
|
120
|
-
if (sessionEnd >= 0) {
|
|
121
|
-
const after = text.slice(sessionEnd + "</session_state>".length).trimStart();
|
|
122
|
-
if (after) {
|
|
123
|
-
text = after;
|
|
124
|
-
} else {
|
|
125
|
-
// All content was in the session_state block
|
|
126
|
-
const before = text.slice(0, text.indexOf("<session_state")).trim();
|
|
127
|
-
if (before) text = before;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// 3. Remove "context-mode active." line and everything after until a blank line
|
|
132
|
-
// Pattern: "context-mode active. ... Hierarchy: ..." followed by blank line, then real message
|
|
133
|
-
const cmRe = /context-mode active\.[\s\S]*?\n\s*\n([\s\S]*)$/;
|
|
134
|
-
const cmMatch = text.match(cmRe);
|
|
135
|
-
if (cmMatch?.[1]?.trim()) {
|
|
136
|
-
text = cmMatch[1].trim();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// 4. If "context-mode" is still the only content, return empty
|
|
140
|
-
if (text.trim() === "context-mode active." || text.trim().startsWith("context-mode active.")) {
|
|
141
|
-
const afterCm = text.replace(/context-mode active\.[\s\S]*/, "").trim();
|
|
142
|
-
if (afterCm) text = afterCm;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return text.trim() || text;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
82
|
// ── thinking config ──
|
|
149
83
|
|
|
150
84
|
const THINKING_BUDGETS: Record<string, number> = {
|