Skip to content

armgit / pibot

Public Imported from GitHub
0

Open-source, local-first AI agent team for your desktop: multi-agent group chats, computer use, browser, MCP, routines and approvals. macOS, Windows, Linux + Android. Built on pi.

12.4 KB
Raw Blame
diff --git a/dist/sdk.d.ts b/dist/sdk.d.ts
new file mode 100644
index 0000000000000000000000000000000000000000..027f00e43fb7a2a4460db58e598c11285b51166d
--- /dev/null
+++ b/dist/sdk.d.ts
@@ -0,0 +1,3 @@
+// Pi Bot add-only patch: types for the headless SDK entry (re-exported from the root entry).
+export { createAgentSession, buildSessionProjection, SessionManager, SettingsManager, ModelRuntime, ModelRegistry, formatSkillsForPrompt, DefaultResourceLoader, createExtensionRuntime, estimateTokens, } from "./index.js";
+export type { AgentSession, ContextEvent, ExtensionAPI, ExtensionFactory, SessionBeforeCompactEvent, SessionEntry, Skill, } from "./index.js";
diff --git a/dist/sdk.js b/dist/sdk.js
new file mode 100644
index 0000000000000000000000000000000000000000..927b6ac325d90e7ad3a8c1d204d05b3a221cc61b
--- /dev/null
+++ b/dist/sdk.js
@@ -0,0 +1,11 @@
+// Pi Bot add-only patch: headless SDK entry. Same modules as the root entry, without
+// re-exporting modes/* (interactive TUI, highlight.js themes), so library hosts load less.
+export { createAgentSession } from "./core/sdk.js";
+export { buildSessionProjection, SessionManager } from "./core/session-manager.js";
+export { SettingsManager } from "./core/settings-manager.js";
+export { ModelRuntime } from "./core/model-runtime.js";
+export { ModelRegistry } from "./core/model-registry.js";
+export { formatSkillsForPrompt } from "./core/skills.js";
+export { DefaultResourceLoader } from "./core/resource-loader.js";
+export { createExtensionRuntime } from "./core/extensions/loader.js";
+export { estimateTokens } from "./core/compaction/index.js";
diff --git a/dist/truncate.d.ts b/dist/truncate.d.ts
new file mode 100644
index 0000000000000000000000000000000000000000..6724e939ffc934cf495efdfe7cb7f70e546add5d
--- /dev/null
+++ b/dist/truncate.d.ts
@@ -0,0 +1 @@
+export { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, truncateHead, type TruncationResult } from "./core/tools/truncate.js";
diff --git a/dist/truncate.js b/dist/truncate.js
new file mode 100644
index 0000000000000000000000000000000000000000..f68ca61b2650dfb43bc9d32e959e8b83820a5657
--- /dev/null
+++ b/dist/truncate.js
@@ -0,0 +1,2 @@
+// Pi Bot add-only patch: output truncation helpers only (no imports), for hosts that must not load the SDK.
+export { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, truncateHead } from "./core/tools/truncate.js";
diff --git a/dist/version.d.ts b/dist/version.d.ts
new file mode 100644
index 0000000000000000000000000000000000000000..a7d7ecd716aa6112e35849656cb51a663f85814b
--- /dev/null
+++ b/dist/version.d.ts
@@ -0,0 +1 @@
+export declare const VERSION: string;
diff --git a/dist/version.js b/dist/version.js
new file mode 100644
index 0000000000000000000000000000000000000000..e81f4d2f6ba14c4fc0c13b246fc28161473fd56d
--- /dev/null
+++ b/dist/version.js
@@ -0,0 +1,2 @@
+// Pi Bot add-only patch: version only, without loading the SDK.
+export const VERSION = "1.4.0";
diff --git a/dist/context-helpers.js b/dist/context-helpers.js
new file mode 100644
--- /dev/null
+++ b/dist/context-helpers.js
@@ -0,0 +1,244 @@
+// Add-only Pi Bot entry: exact pure function bodies from pi-coding-agent 1.4.0 (MIT).
+// Generated by packages/agent/scripts/pi-context-helpers.ts; keep the upstream package license.
+function buildEntryIndex(entries, byId) {
+    if (byId)
+        return byId;
+    const index = new Map();
+    for (const entry of entries) {
+        index.set(entry.id, entry);
+    }
+    return index;
+}
+function buildSessionPath(entries, leafId, byId) {
+    const index = buildEntryIndex(entries, byId);
+    let leaf;
+    if (leafId === null) {
+        return [];
+    }
+    if (leafId) {
+        leaf = index.get(leafId);
+    }
+    leaf ??= entries[entries.length - 1];
+    if (!leaf) {
+        return [];
+    }
+    const path = [];
+    let current = leaf;
+    while (current) {
+        path.push(current);
+        current = current.parentId ? index.get(current.parentId) : undefined;
+    }
+    path.reverse();
+    return path;
+}
+function getSessionContextSettings(path) {
+    let thinkingLevel = "off";
+    let model = null;
+    for (const entry of path) {
+        if (entry.type === "thinking_level_change") {
+            thinkingLevel = entry.thinkingLevel;
+        }
+        else if (entry.type === "model_change") {
+            model = { provider: entry.provider, modelId: entry.modelId };
+        }
+        else if (entry.type === "message" && entry.message.role === "assistant") {
+            model = { provider: entry.message.provider, modelId: entry.message.model };
+        }
+    }
+    return { thinkingLevel, model };
+}
+function sessionEntryToContextMessages(entry) {
+    if (entry.type === "message") {
+        const message = entry.message;
+        // Session files are parsed without validation; old versions, forks, or
+        // hand-edited files can contain messages with null/missing content.
+        if (message.role === "system" && message.content == null)
+            return [{ ...message, content: "" }];
+        if ((message.role === "user" || message.role === "assistant" || message.role === "toolResult") &&
+            message.content == null) {
+            return [{ ...message, content: [] }];
+        }
+        return [message];
+    }
+    if (entry.type === "custom_message") {
+        return [
+            createCustomMessage(entry.customType, entry.content ?? [], entry.display, entry.details, entry.timestamp),
+        ];
+    }
+    if (entry.type === "branch_summary" && entry.summary) {
+        return [createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp)];
+    }
+    if (entry.type === "compaction") {
+        const summary = createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp);
+        return entry.systemMessage ? [entry.systemMessage, summary] : [summary];
+    }
+    return [];
+}
+function buildContextEntries(entries, leafId, byId) {
+    const path = buildSessionPath(entries, leafId, byId);
+    let compaction = null;
+    for (const entry of path) {
+        if (entry.type === "compaction") {
+            compaction = entry;
+        }
+    }
+    if (!compaction) {
+        return path;
+    }
+    const compactionIdx = path.findIndex((entry) => entry.id === compaction.id);
+    if (compactionIdx < 0) {
+        return path;
+    }
+    const contextEntries = [compaction];
+    let foundFirstKept = false;
+    for (let i = 0; i < compactionIdx; i++) {
+        const entry = path[i];
+        if (entry.id === compaction.firstKeptEntryId) {
+            foundFirstKept = true;
+        }
+        if (foundFirstKept && !(entry.type === "message" && entry.message.role === "system")) {
+            contextEntries.push(entry);
+        }
+    }
+    contextEntries.push(...path.slice(compactionIdx + 1));
+    return contextEntries;
+}
+function projectContextEntry(entry, edit) {
+    const messages = sessionEntryToContextMessages(entry);
+    if (!edit)
+        return messages;
+    const replacement = edit.replacement;
+    if (replacement === null)
+        return [];
+    return messages.map((message) => {
+        if (message.role !== "user" &&
+            message.role !== "assistant" &&
+            message.role !== "toolResult" &&
+            message.role !== "custom") {
+            return message;
+        }
+        const content = (message.role === "assistant" || message.role === "toolResult") && typeof replacement.content === "string"
+            ? [{ type: "text", text: replacement.content }]
+            : replacement.content;
+        return { ...message, content };
+    });
+}
+export function buildSessionProjection(entries, leafId, byId) {
+    const path = buildSessionPath(entries, leafId, byId);
+    const { thinkingLevel, model } = getSessionContextSettings(path);
+    const contextEntries = buildContextEntries(entries, leafId, byId);
+    const edits = new Map();
+    for (const entry of contextEntries) {
+        if (entry.type === "context_edit")
+            edits.set(entry.targetId, entry);
+    }
+    const projectedEntries = contextEntries.map((sourceEntry, index) => ({
+        sourceEntry,
+        // buildContextEntries() may retain an older compaction entry because its
+        // raw ID lies inside the newest retained range. Only the newest compaction
+        // at index zero contributes a checkpoint and summary.
+        messages: sourceEntry.type === "compaction" && index > 0
+            ? []
+            : projectContextEntry(sourceEntry, edits.get(sourceEntry.id)),
+    }));
+    return {
+        entries: projectedEntries,
+        messages: projectedEntries.flatMap((entry) => entry.messages),
+        thinkingLevel,
+        model,
+    };
+}
+function createBranchSummaryMessage(summary, fromId, timestamp) {
+    return {
+        role: "branchSummary",
+        summary,
+        fromId,
+        timestamp: new Date(timestamp).getTime(),
+    };
+}
+function createCompactionSummaryMessage(summary, tokensBefore, timestamp) {
+    return {
+        role: "compactionSummary",
+        summary: summary,
+        tokensBefore,
+        timestamp: new Date(timestamp).getTime(),
+    };
+}
+function createCustomMessage(customType, content, display, details, timestamp) {
+    return {
+        role: "custom",
+        customType,
+        content,
+        display,
+        details,
+        timestamp: new Date(timestamp).getTime(),
+    };
+}
+const ESTIMATED_IMAGE_CHARS = 4800;
+function estimateTextAndImageContentChars(content) {
+    if (typeof content === "string") {
+        return content.length;
+    }
+    let chars = 0;
+    for (const block of content) {
+        if (block.type === "text" && block.text) {
+            chars += block.text.length;
+        }
+        else if (block.type === "image") {
+            chars += ESTIMATED_IMAGE_CHARS;
+        }
+    }
+    return chars;
+}
+export function estimateTokens(message) {
+    let chars = 0;
+    switch (message.role) {
+        case "system": {
+            const system = message;
+            chars = estimateTextAndImageContentChars(system.content);
+            if (system.sections) {
+                for (const section of Object.values(system.sections)) {
+                    if (section)
+                        chars += section.length;
+                }
+            }
+            if (system.toolsAdded)
+                chars += JSON.stringify(system.toolsAdded).length;
+            return Math.ceil(chars / 4);
+        }
+        case "user": {
+            chars = estimateTextAndImageContentChars(message.content);
+            return Math.ceil(chars / 4);
+        }
+        case "assistant": {
+            const assistant = message;
+            for (const block of assistant.content) {
+                if (block.type === "text") {
+                    chars += block.text.length;
+                }
+                else if (block.type === "thinking") {
+                    chars += block.thinking.length;
+                }
+                else if (block.type === "toolCall") {
+                    chars += block.name.length + JSON.stringify(block.arguments).length;
+                }
+            }
+            return Math.ceil(chars / 4);
+        }
+        case "custom":
+        case "toolResult": {
+            chars = estimateTextAndImageContentChars(message.content);
+            return Math.ceil(chars / 4);
+        }
+        case "bashExecution": {
+            chars = message.command.length + message.output.length;
+            return Math.ceil(chars / 4);
+        }
+        case "branchSummary":
+        case "compactionSummary": {
+            chars = message.summary.length;
+            return Math.ceil(chars / 4);
+        }
+    }
+    return 0;
+}
diff --git a/dist/context-helpers.d.ts b/dist/context-helpers.d.ts
new file mode 100644
--- /dev/null
+++ b/dist/context-helpers.d.ts
@@ -0,0 +1,1 @@
+export { buildSessionProjection, estimateTokens } from "./index.js";
diff --git a/package.json b/package.json
index e0955b0e7827d5c119a7dc96bbac70bc8a11e9c5..674f8a26c992c1e0f293dc09726cbfb9c3a6bc9d 100644
--- a/package.json
+++ b/package.json
@@ -18,6 +18,22 @@
 		},
 		"./rpc-entry": {
 			"import": "./dist/bundle/rpc-entry.js"
+		},
+		"./sdk": {
+			"types": "./dist/sdk.d.ts",
+			"import": "./dist/sdk.js"
+		},
+		"./version": {
+			"types": "./dist/version.d.ts",
+			"import": "./dist/version.js"
+		},
+		"./truncate": {
+			"types": "./dist/truncate.d.ts",
+			"import": "./dist/truncate.js"
+		},
+		"./context-helpers": {
+			"types": "./dist/context-helpers.d.ts",
+			"import": "./dist/context-helpers.js"
 		}
 	},
 	"files": [