@hienlh/ppm 0.20.5 → 0.20.6
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/CHANGELOG.md +10 -0
- package/assets/skills/ppm/SKILL.md +1 -1
- package/assets/skills/ppm/references/cli-reference.md +4 -4
- package/assets/skills/ppm/references/http-api.md +1 -1
- package/dist/web/assets/{accounts-settings-section-Cmmozwer.js → accounts-settings-section-B-F7U-gq.js} +1 -1
- package/dist/web/assets/{ai-resource-editor-BlrWV3xU.js → ai-resource-editor-qRHwpktf.js} +1 -1
- package/dist/web/assets/{ai-settings-section-dDsgddSt.js → ai-settings-section-BePrVceb.js} +1 -1
- package/dist/web/assets/ai-settings-section-DSlsTrpf.js +1 -0
- package/dist/web/assets/{appearance-settings-section-DRxziHFt.js → appearance-settings-section-BMlkj9U2.js} +1 -1
- package/dist/web/assets/chat-tab-DmqV3MlC.js +15 -0
- package/dist/web/assets/{code-editor-DYkmDRRF.js → code-editor-C4CriX9c.js} +2 -2
- package/dist/web/assets/{conflict-editor-D9_sr59D.js → conflict-editor-LfQoRcK-.js} +1 -1
- package/dist/web/assets/{csv-preview-BMC8jnpF.js → csv-preview-BEYKn68B.js} +1 -1
- package/dist/web/assets/{database-viewer-Box3z5-Q.js → database-viewer-BL9R-ciJ.js} +1 -1
- package/dist/web/assets/{diff-viewer-B4PlZ1Dt.js → diff-viewer-D91SI_zb.js} +1 -1
- package/dist/web/assets/{explorer-body-SvWDS84f.js → explorer-body-ColVUjgk.js} +1 -1
- package/dist/web/assets/{explorer-window-content-DBHBTA7B.js → explorer-window-content-VSMJfZC2.js} +1 -1
- package/dist/web/assets/{extension-manager-section-BSt5I_H8.js → extension-manager-section-B-BRHGu1.js} +1 -1
- package/dist/web/assets/{extension-webview-DBGMUU97.js → extension-webview-DufzanN9.js} +1 -1
- package/dist/web/assets/{files-settings-section-0dwYLZou.js → files-settings-section-Do5YbTUK.js} +1 -1
- package/dist/web/assets/{general-settings-section-BgLy_6yj.js → general-settings-section-B-n_PYzs.js} +1 -1
- package/dist/web/assets/{git-log-panel-BkgmcWk1.js → git-log-panel-BQzOHgxG.js} +1 -1
- package/dist/web/assets/{glide-data-grid-CZVu-zCw.js → glide-data-grid-BUS44oLM.js} +1 -1
- package/dist/web/assets/{group-chat-tab-Cy9zIynj.js → group-chat-tab-CkHXqorZ.js} +1 -1
- package/dist/web/assets/{index-CuYtJ5xF.css → index-B9Wbjl0o.css} +1 -1
- package/dist/web/assets/{index-DYGLdCpe.js → index-Cx1meAwb.js} +4 -4
- package/dist/web/assets/{jira-watcher-section-DRQx632s.js → jira-watcher-section-BQ4hVqWo.js} +1 -1
- package/dist/web/assets/{keybindings-store-C2HZ1mT4.js → keybindings-store-yYx88hvw.js} +1 -1
- package/dist/web/assets/{keyboard-shortcuts-section-WiZSZ-Wr.js → keyboard-shortcuts-section-BZUYdXGx.js} +1 -1
- package/dist/web/assets/{markdown-renderer-BFYBVcZP.js → markdown-renderer-BA3C2L-a.js} +1 -1
- package/dist/web/assets/{markdown-renderer-CklcWnd6.js → markdown-renderer-CMFkajd7.js} +1 -1
- package/dist/web/assets/{mobile-explorer-sheet-D1OXw7op.js → mobile-explorer-sheet-gvO7a80O.js} +1 -1
- package/dist/web/assets/{notification-store-DyKshGRs.js → notification-store-CCh6a8RD.js} +1 -1
- package/dist/web/assets/{notifications-settings-section-BEQEjvan.js → notifications-settings-section-JQ3l-Qp-.js} +1 -1
- package/dist/web/assets/{pdf-preview-DmQazeeV.js → pdf-preview-B-JAU8JV.js} +1 -1
- package/dist/web/assets/{postgres-viewer-Cd_wXjkT.js → postgres-viewer-B--Y59n5.js} +1 -1
- package/dist/web/assets/{ppmbot-settings-section-PzCLPm8a.js → ppmbot-settings-section-D1SCOc0_.js} +1 -1
- package/dist/web/assets/{process-table-C2TdkJck.js → process-table-D9z7cEmb.js} +1 -1
- package/dist/web/assets/{proxy-settings-section-DsZ6KcHD.js → proxy-settings-section-y7VwH0Hf.js} +1 -1
- package/dist/web/assets/{query-audit-section-2eX9-6mO.js → query-audit-section-Ca_gRQ_s.js} +1 -1
- package/dist/web/assets/{remote-desktop-mobile-sheet-CMXSUcAN.js → remote-desktop-mobile-sheet-CdO2aMXl.js} +2 -2
- package/dist/web/assets/{remote-desktop-mobile-view-Bw6p5ikf.js → remote-desktop-mobile-view-CGQ8vSf1.js} +1 -1
- package/dist/web/assets/{remote-desktop-readiness-gate-W0lAhHq9.js → remote-desktop-readiness-gate-BmC3b05e.js} +1 -1
- package/dist/web/assets/{remote-desktop-window-content-P888YEqm.js → remote-desktop-window-content-Cdaoltdz.js} +1 -1
- package/dist/web/assets/{schedules-settings-section-C8YDAxlo.js → schedules-settings-section--0s10CfA.js} +1 -1
- package/dist/web/assets/{settings-body-B5v3_Tnb.js → settings-body-DEMqFm5z.js} +2 -2
- package/dist/web/assets/{settings-tab-CauShulf.js → settings-tab-Dh1FpBny.js} +1 -1
- package/dist/web/assets/{settings-window-content-BIosR7RY.js → settings-window-content-3KrQ6imZ.js} +1 -1
- package/dist/web/assets/{sql-query-editor-DMihDPnw.js → sql-query-editor-Clo90z8-.js} +1 -1
- package/dist/web/assets/{sqlite-viewer-jCIGt2GP.js → sqlite-viewer-C08QQR_L.js} +1 -1
- package/dist/web/assets/{system-monitor-body-IUamfS5w.js → system-monitor-body-30-TFH3a.js} +2 -2
- package/dist/web/assets/{system-monitor-tab-DaWeif4k.js → system-monitor-tab-ChIXfOWg.js} +1 -1
- package/dist/web/assets/{system-monitor-window-content-Bq3BS7Mg.js → system-monitor-window-content-C1Cd_pT7.js} +1 -1
- package/dist/web/assets/{tab-host-window-content-vfYLLIKU.js → tab-host-window-content-C6eG_Rf1.js} +1 -1
- package/dist/web/assets/{team-member-sheet-Be7OSGMT.js → team-member-sheet-BhatQbL1.js} +2 -2
- package/dist/web/assets/{team-member-window-content-D5cYZzBq.js → team-member-window-content-BN3luct-.js} +1 -1
- package/dist/web/assets/{terminal-tab-C89DQllY.js → terminal-tab-afylLiLY.js} +1 -1
- package/dist/web/assets/{tool-cards-CWotDH_2.js → tool-cards-Ct1QdT8c.js} +2 -2
- package/dist/web/assets/{use-accounts-data-lmmmG4ep.js → use-accounts-data-C0bvcMhi.js} +1 -1
- package/dist/web/assets/{use-monaco-theme-0ziS4xlp.js → use-monaco-theme-cgPbRDUm.js} +1 -1
- package/dist/web/assets/{use-remote-desktop-display-choice-qBZ8ej7J.js → use-remote-desktop-display-choice-DSqF1Rgx.js} +1 -1
- package/dist/web/assets/use-websocket-BQssVSIR.js +1 -0
- package/dist/web/assets/{video-preview-B8xALVkh.js → video-preview-BTzstVhM.js} +1 -1
- package/dist/web/index.html +2 -2
- package/dist/web/sw.js +1 -1
- package/docs/architecture/ai-chat-and-providers.md +88 -0
- package/package.json +1 -1
- package/packages/vscode-compat/node_modules/.bin/tsc.bunx +0 -0
- package/packages/vscode-compat/node_modules/.bin/tsc.exe +0 -0
- package/packages/vscode-compat/node_modules/.bin/tsserver.bunx +0 -0
- package/packages/vscode-compat/node_modules/.bin/tsserver.exe +0 -0
- package/schemas/ppm-config.schema.json +18 -1
- package/src/providers/claude-agent-sdk.ts +21 -13
- package/src/providers/cli-provider-base.ts +3 -1
- package/src/providers/codex-app-server/codex-history.ts +5 -3
- package/src/providers/codex-app-server/codex-provider.ts +16 -2
- package/src/providers/codex-app-server/codex-rollout-header.ts +2 -1
- package/src/providers/cursor-cli/cursor-history.ts +4 -2
- package/src/server/routes/settings.ts +15 -1
- package/src/server/ws/chat.ts +2 -2
- package/src/services/chat.service.ts +95 -2
- package/src/services/codex-native-memory.ts +66 -0
- package/src/services/edge-forwarder.ts +191 -191
- package/src/services/jsonl-transcript-parser.ts +2 -1
- package/src/services/provider-shared-context.ts +152 -0
- package/src/services/windows-process-tree.ts +351 -351
- package/src/shared/provider-context.ts +13 -0
- package/src/types/chat.ts +6 -0
- package/src/types/config.ts +29 -0
- package/src/web/components/chat/chat-history-bar.tsx +6 -3
- package/src/web/components/chat/chat-tab.tsx +7 -0
- package/src/web/components/settings/ai-settings-section.tsx +34 -5
- package/src/web/components/settings/codex-context-settings.tsx +100 -0
- package/src/web/hooks/use-chat.ts +71 -14
- package/src/web/hooks/use-websocket.ts +14 -7
- package/src/web/lib/api-settings.ts +3 -0
- package/src/web/lib/ws-client.ts +55 -4
- package/.release-bin/zip +0 -15
- package/.release-node-shim.sh +0 -11
- package/dist/web/assets/ai-settings-section-PaEh_o0w.js +0 -1
- package/dist/web/assets/chat-tab-MNuc18Os.js +0 -15
- package/dist/web/assets/use-websocket-DAvfbjxh.js +0 -1
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { providerRegistry } from "../providers/registry.ts";
|
|
2
|
+
import { configService } from "./config.service.ts";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { buildSharedProviderContext } from "./provider-shared-context.ts";
|
|
5
|
+
import { stripSharedContext, withSharedContext } from "../shared/provider-context.ts";
|
|
2
6
|
import type {
|
|
3
7
|
Session,
|
|
4
8
|
SessionConfig,
|
|
@@ -9,6 +13,20 @@ import type {
|
|
|
9
13
|
} from "../providers/provider.interface.ts";
|
|
10
14
|
|
|
11
15
|
class ChatService {
|
|
16
|
+
// Delivery hints only: a restart/eviction safely sends a fresh snapshot.
|
|
17
|
+
private sharedSnapshots = new Map<string, string>();
|
|
18
|
+
|
|
19
|
+
private rememberSharedContext(providerId: string, sessionId: string, context?: string): void {
|
|
20
|
+
if (!context) return;
|
|
21
|
+
const key = `${providerId}:${sessionId}`;
|
|
22
|
+
this.sharedSnapshots.delete(key);
|
|
23
|
+
this.sharedSnapshots.set(key, createHash("sha256").update(context).digest("hex"));
|
|
24
|
+
if (this.sharedSnapshots.size > 512) this.sharedSnapshots.delete(this.sharedSnapshots.keys().next().value!);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
invalidateSharedContext(providerId: string, sessionId: string): void {
|
|
28
|
+
this.sharedSnapshots.delete(`${providerId}:${sessionId}`);
|
|
29
|
+
}
|
|
12
30
|
async createSession(
|
|
13
31
|
providerId?: string,
|
|
14
32
|
config: SessionConfig = {},
|
|
@@ -32,6 +50,7 @@ class ChatService {
|
|
|
32
50
|
): Promise<Session> {
|
|
33
51
|
const provider = providerRegistry.get(providerId);
|
|
34
52
|
if (!provider) throw new Error(`Provider "${providerId}" not found`);
|
|
53
|
+
this.invalidateSharedContext(providerId, sessionId);
|
|
35
54
|
return provider.resumeSession(sessionId);
|
|
36
55
|
}
|
|
37
56
|
|
|
@@ -68,6 +87,7 @@ class ChatService {
|
|
|
68
87
|
): Promise<void> {
|
|
69
88
|
const provider = providerRegistry.get(providerId);
|
|
70
89
|
if (!provider) throw new Error(`Provider "${providerId}" not found`);
|
|
90
|
+
this.invalidateSharedContext(providerId, sessionId);
|
|
71
91
|
return provider.deleteSession(sessionId);
|
|
72
92
|
}
|
|
73
93
|
|
|
@@ -82,7 +102,77 @@ class ChatService {
|
|
|
82
102
|
yield { type: "error", message: `Provider "${providerId}" not found` };
|
|
83
103
|
return;
|
|
84
104
|
}
|
|
85
|
-
|
|
105
|
+
const prepared = await this.prepareSendOptions(providerId, sessionId, message, opts);
|
|
106
|
+
this.rememberSharedContext(providerId, sessionId, prepared.sharedContext);
|
|
107
|
+
let finished = false;
|
|
108
|
+
let activeSessionId = sessionId;
|
|
109
|
+
try {
|
|
110
|
+
for await (const event of provider.sendMessage(sessionId,
|
|
111
|
+
provider.supportsSharedContext ? message : withSharedContext(message, prepared.sharedContext),
|
|
112
|
+
{ ...prepared, sharedContext: provider.supportsSharedContext ? prepared.sharedContext : undefined })) {
|
|
113
|
+
if (event.type === "error" || (event.type === "done" && event.resultSubtype?.startsWith("error")) || (event.type === "system" && event.subtype === "compact_done")) {
|
|
114
|
+
this.invalidateSharedContext(providerId, activeSessionId);
|
|
115
|
+
}
|
|
116
|
+
const migratedId = event.type === "session_migrated" ? event.newSessionId : event.type === "done" ? event.sessionId : undefined;
|
|
117
|
+
if (migratedId && migratedId !== activeSessionId) {
|
|
118
|
+
const snapshot = this.sharedSnapshots.get(`${providerId}:${activeSessionId}`);
|
|
119
|
+
if (snapshot) {
|
|
120
|
+
this.sharedSnapshots.set(`${providerId}:${migratedId}`, snapshot);
|
|
121
|
+
this.invalidateSharedContext(providerId, activeSessionId);
|
|
122
|
+
}
|
|
123
|
+
activeSessionId = migratedId;
|
|
124
|
+
}
|
|
125
|
+
yield event;
|
|
126
|
+
}
|
|
127
|
+
finished = true;
|
|
128
|
+
} finally {
|
|
129
|
+
if (!finished) this.invalidateSharedContext(providerId, activeSessionId);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Prepare context for both a new stream and a live stream's follow-up turn. */
|
|
134
|
+
async prepareSendOptions(
|
|
135
|
+
providerId: string,
|
|
136
|
+
sessionId: string,
|
|
137
|
+
message: string,
|
|
138
|
+
opts?: SendMessageOpts,
|
|
139
|
+
): Promise<SendMessageOpts> {
|
|
140
|
+
if (!providerRegistry.get(providerId)) throw new Error(`Provider "${providerId}" not found`);
|
|
141
|
+
let sharedContext: string | undefined;
|
|
142
|
+
if (configService.get("ai").share_provider_context === false || /^\s*\/(compact|clear|new)(\s|$)/i.test(message)) {
|
|
143
|
+
this.invalidateSharedContext(providerId, sessionId);
|
|
144
|
+
}
|
|
145
|
+
// Slash commands belong to the runtime parser. A context prefix would turn
|
|
146
|
+
// /compact (or a provider skill) into an ordinary prompt.
|
|
147
|
+
if (configService.get("ai").share_provider_context !== false && !message.trimStart().startsWith("/")) {
|
|
148
|
+
const { getSessionProjectPath } = await import("./db.service.ts");
|
|
149
|
+
const projectPath = this.getSession(sessionId)?.projectPath ?? getSessionProjectPath(sessionId);
|
|
150
|
+
if (projectPath) {
|
|
151
|
+
const providers = providerRegistry.listAll().flatMap(({ id }) => {
|
|
152
|
+
const item = providerRegistry.get(id);
|
|
153
|
+
return item ? [item] : [];
|
|
154
|
+
});
|
|
155
|
+
sharedContext = await buildSharedProviderContext(projectPath, providers, { recipientProviderId: providerId });
|
|
156
|
+
const hash = createHash("sha256").update(sharedContext).digest("hex");
|
|
157
|
+
if (this.sharedSnapshots.get(`${providerId}:${sessionId}`) === hash) sharedContext = undefined;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return { ...opts, sharedContext };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Push a live follow-up through the same context policy as sendMessage. */
|
|
164
|
+
async pushMessage(providerId: string, sessionId: string, message: string, opts?: SendMessageOpts): Promise<void> {
|
|
165
|
+
const provider = providerRegistry.get(providerId);
|
|
166
|
+
if (!provider) throw new Error(`Provider "${providerId}" not found`);
|
|
167
|
+
const streaming = provider as typeof provider & {
|
|
168
|
+
pushMessage?: (id: string, content: string, options: SendMessageOpts) => void;
|
|
169
|
+
};
|
|
170
|
+
if (!streaming.pushMessage) return;
|
|
171
|
+
const prepared = await this.prepareSendOptions(providerId, sessionId, message, opts);
|
|
172
|
+
streaming.pushMessage(sessionId,
|
|
173
|
+
provider.supportsSharedContext ? message : withSharedContext(message, prepared.sharedContext),
|
|
174
|
+
{ ...prepared, sharedContext: provider.supportsSharedContext ? prepared.sharedContext : undefined });
|
|
175
|
+
this.rememberSharedContext(providerId, sessionId, prepared.sharedContext);
|
|
86
176
|
}
|
|
87
177
|
|
|
88
178
|
/** Look up a session across all providers (for WS handler) */
|
|
@@ -106,7 +196,10 @@ class ChatService {
|
|
|
106
196
|
async getMessages(providerId: string, sessionId: string): Promise<ChatMessage[]> {
|
|
107
197
|
const provider = providerRegistry.get(providerId);
|
|
108
198
|
if (!provider) return [];
|
|
109
|
-
|
|
199
|
+
const messages = await provider.getMessages?.(sessionId) ?? [];
|
|
200
|
+
return messages.map((message) => message.role === "user"
|
|
201
|
+
? { ...message, content: stripSharedContext(message.content) }
|
|
202
|
+
: message);
|
|
110
203
|
}
|
|
111
204
|
}
|
|
112
205
|
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { readdirSync, realpathSync } from "node:fs";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
|
|
5
|
+
const MAX_MEMORIES = 24;
|
|
6
|
+
const MAX_CHARS = 24_000;
|
|
7
|
+
|
|
8
|
+
function latestDatabase(home: string, name: string): string | undefined {
|
|
9
|
+
try {
|
|
10
|
+
const candidates = readdirSync(home).filter((file) => new RegExp(`^${name}_\\d+\\.sqlite$`).test(file));
|
|
11
|
+
candidates.sort((a, b) => Number(b.match(/_(\d+)/)?.[1]) - Number(a.match(/_(\d+)/)?.[1]));
|
|
12
|
+
return candidates[0] ? join(home, candidates[0]) : undefined;
|
|
13
|
+
} catch { return undefined; }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Read native distilled memories only for threads belonging to this exact project. */
|
|
17
|
+
export function readCodexNativeMemory(projectPath: string, codexHomes: string[]): Array<{ path: string; content: string }> {
|
|
18
|
+
const result: Array<{ path: string; content: string }> = [];
|
|
19
|
+
const windows = process.platform === "win32";
|
|
20
|
+
let canonicalProject = resolve(projectPath);
|
|
21
|
+
try { canonicalProject = realpathSync(canonicalProject); } catch { /* A deleted project can still have native memory. */ }
|
|
22
|
+
const project = canonicalProject.replaceAll("\\", "/").replace(/\/+$/, "");
|
|
23
|
+
let remaining = MAX_CHARS;
|
|
24
|
+
const seen = new Set<string>();
|
|
25
|
+
for (const home of new Set(codexHomes)) {
|
|
26
|
+
if (!remaining || result.length >= MAX_MEMORIES) break;
|
|
27
|
+
const statePath = latestDatabase(home, "state");
|
|
28
|
+
if (!statePath) continue;
|
|
29
|
+
// Older native releases stored stage1_outputs in the state database itself.
|
|
30
|
+
const memoryPath = latestDatabase(home, "memories") ?? statePath;
|
|
31
|
+
let state: Database | undefined;
|
|
32
|
+
let memories: Database | undefined;
|
|
33
|
+
try {
|
|
34
|
+
state = new Database(statePath, { readonly: true });
|
|
35
|
+
const columns = state.query("PRAGMA table_info(threads)").all() as Array<{ name: string }>;
|
|
36
|
+
const cwd = "rtrim(replace(cwd, char(92), '/'), '/')";
|
|
37
|
+
const enabled = columns.some((column) => column.name === "memory_mode") ? " AND memory_mode != 'disabled'" : "";
|
|
38
|
+
const recent = columns.some((column) => column.name === "updated_at") ? " ORDER BY updated_at DESC" : "";
|
|
39
|
+
const threads = state.query(`SELECT id FROM threads WHERE ${windows ? `lower(${cwd})` : cwd} = ?${enabled}${recent} LIMIT 256`)
|
|
40
|
+
.all(windows ? project.toLowerCase() : project) as Array<{ id: string }>;
|
|
41
|
+
if (!threads.length) continue;
|
|
42
|
+
memories = new Database(memoryPath, { readonly: true });
|
|
43
|
+
const placeholders = threads.map(() => "?").join(",");
|
|
44
|
+
const rows = memories.query(`SELECT thread_id, substr(raw_memory, 1, 12000) AS raw_memory, length(raw_memory) AS full_length FROM stage1_outputs WHERE thread_id IN (${placeholders}) ORDER BY generated_at DESC LIMIT ?`)
|
|
45
|
+
.all(...threads.map((thread) => thread.id), MAX_MEMORIES) as Array<{ thread_id: string; raw_memory: string; full_length: number }>;
|
|
46
|
+
for (const row of rows) {
|
|
47
|
+
const raw = row.raw_memory?.trim();
|
|
48
|
+
if (!raw || raw.includes("\0") || seen.has(raw) || remaining < 200) continue;
|
|
49
|
+
const path = `${memoryPath}#stage1_outputs/${row.thread_id}`;
|
|
50
|
+
const excerpt = raw.slice(0, Math.max(0, remaining - 180));
|
|
51
|
+
const truncated = row.full_length > excerpt.length;
|
|
52
|
+
const content = excerpt + (truncated ? "\n[Excerpt truncated. Read the raw_memory column of this source database row for the remaining native memory.]" : "");
|
|
53
|
+
seen.add(raw);
|
|
54
|
+
result.push({ path, content });
|
|
55
|
+
remaining -= content.length;
|
|
56
|
+
if (result.length >= MAX_MEMORIES) break;
|
|
57
|
+
}
|
|
58
|
+
} catch {
|
|
59
|
+
// Missing, locked, or newer incompatible native schemas must not block chat.
|
|
60
|
+
} finally {
|
|
61
|
+
memories?.close();
|
|
62
|
+
state?.close();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
@@ -1,191 +1,191 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Edge forwarder — owns the public port and pipes raw TCP to the server's
|
|
3
|
-
* current loopback port.
|
|
4
|
-
*
|
|
5
|
-
* ─── CRITICAL INVARIANT: THIS PROCESS MUST NEVER SPAWN A CHILD ───
|
|
6
|
-
*
|
|
7
|
-
* On Windows a child spawned with fd stdio inherits every inheritable handle,
|
|
8
|
-
* including a listening socket. When such a descendant is orphaned it keeps the
|
|
9
|
-
* socket open, so the port stays in LISTEN under a dead PID and can never be
|
|
10
|
-
* rebound — a "zombie port". The server hits this constantly because it spawns
|
|
11
|
-
* chat/tool/MCP subprocesses; those inherit its listener and wedge its port.
|
|
12
|
-
*
|
|
13
|
-
* The edge exists precisely because it spawns nothing, so its socket cannot be
|
|
14
|
-
* inherited and its port cannot zombie. cloudflared can therefore stay pinned
|
|
15
|
-
* to it forever and the public URL stops rotating. Adding any child-process
|
|
16
|
-
* call to this file reintroduces the exact bug the edge was built to remove.
|
|
17
|
-
*
|
|
18
|
-
* Raw TCP, never HTTP: WebSocket upgrades and SSE streams pass through
|
|
19
|
-
* untouched, and forwarded requests keep the headers cloudflared set.
|
|
20
|
-
*/
|
|
21
|
-
import net from "node:net";
|
|
22
|
-
import { resolveTargetPort, _resetTargetCache } from "./edge-target-resolver.ts";
|
|
23
|
-
|
|
24
|
-
/** Delay between attempts while waiting for the server to come back. */
|
|
25
|
-
const CONNECT_RETRY_MS = 250;
|
|
26
|
-
/**
|
|
27
|
-
* How long a client connection waits for a reachable server before giving up.
|
|
28
|
-
* During an upgrade the server is briefly absent; refusing instantly would
|
|
29
|
-
* surface as a broken page rather than a slow one.
|
|
30
|
-
*/
|
|
31
|
-
const CONNECT_WINDOW_MS = 5000;
|
|
32
|
-
|
|
33
|
-
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
34
|
-
|
|
35
|
-
/** Resolve once the socket is connected; reject on the first connect error. */
|
|
36
|
-
function connectUpstream(port: number): Promise<net.Socket> {
|
|
37
|
-
return new Promise((resolvePromise, reject) => {
|
|
38
|
-
const upstream = net.connect({
|
|
39
|
-
port,
|
|
40
|
-
host: "127.0.0.1", // never taken from status.json — loopback only
|
|
41
|
-
allowHalfOpen: true,
|
|
42
|
-
});
|
|
43
|
-
const onError = (err: Error) => {
|
|
44
|
-
upstream.destroy();
|
|
45
|
-
reject(err);
|
|
46
|
-
};
|
|
47
|
-
upstream.once("error", onError);
|
|
48
|
-
upstream.once("connect", () => {
|
|
49
|
-
upstream.removeListener("error", onError);
|
|
50
|
-
resolvePromise(upstream);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Largest amount of client data held while waiting for the server. Bounds the
|
|
57
|
-
* memory a slow upload can pin during an upgrade window.
|
|
58
|
-
*/
|
|
59
|
-
const MAX_PENDING_BYTES = 8 * 1024 * 1024;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Pipe one client connection to the server.
|
|
63
|
-
*
|
|
64
|
-
* Resolving the upstream is async, so the first bytes usually arrive before
|
|
65
|
-
* there is anywhere to send them. They must be captured by a `data` listener
|
|
66
|
-
* attached synchronously on this very tick and replayed once the upstream
|
|
67
|
-
* exists.
|
|
68
|
-
*
|
|
69
|
-
* `socket.pause()` is NOT a substitute — verified against Bun 1.3.13: bytes
|
|
70
|
-
* that arrive while paused are lost, and a later `pipe()`/`resume()` does not
|
|
71
|
-
* bring them back. A socket with no `data` listener drops them outright too.
|
|
72
|
-
* Both variants silently swallowed the first request in testing.
|
|
73
|
-
*/
|
|
74
|
-
async function handleConnection(
|
|
75
|
-
client: net.Socket,
|
|
76
|
-
connectWindowMs: number,
|
|
77
|
-
): Promise<void> {
|
|
78
|
-
const pending: Buffer[] = [];
|
|
79
|
-
let pendingBytes = 0;
|
|
80
|
-
let clientEnded = false;
|
|
81
|
-
const onEarlyData = (chunk: Buffer) => {
|
|
82
|
-
pendingBytes += chunk.length;
|
|
83
|
-
if (pendingBytes > MAX_PENDING_BYTES) {
|
|
84
|
-
client.destroy();
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
pending.push(chunk);
|
|
88
|
-
};
|
|
89
|
-
client.on("data", onEarlyData);
|
|
90
|
-
client.once("end", () => { clientEnded = true; });
|
|
91
|
-
|
|
92
|
-
let upstream: net.Socket | null = null;
|
|
93
|
-
let clientGone = false;
|
|
94
|
-
|
|
95
|
-
// A socket error with no listener is an unhandled 'error' event, which would
|
|
96
|
-
// take the whole edge process — and the public port — down with it.
|
|
97
|
-
const teardown = () => {
|
|
98
|
-
upstream?.destroy();
|
|
99
|
-
client.destroy();
|
|
100
|
-
};
|
|
101
|
-
client.on("error", teardown);
|
|
102
|
-
client.once("close", () => { clientGone = true; });
|
|
103
|
-
|
|
104
|
-
const deadline = Date.now() + connectWindowMs;
|
|
105
|
-
while (!clientGone && Date.now() < deadline) {
|
|
106
|
-
const port = resolveTargetPort();
|
|
107
|
-
if (port !== null) {
|
|
108
|
-
try {
|
|
109
|
-
upstream = await connectUpstream(port);
|
|
110
|
-
break;
|
|
111
|
-
} catch {
|
|
112
|
-
// Server not up yet (or moved mid-restart) — re-resolve and retry.
|
|
113
|
-
_resetTargetCache();
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
await sleep(CONNECT_RETRY_MS);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (clientGone) return;
|
|
120
|
-
if (!upstream) {
|
|
121
|
-
client.destroy();
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
upstream.on("error", teardown);
|
|
126
|
-
|
|
127
|
-
// Replay what arrived during the connect, then hand both directions to the
|
|
128
|
-
// stream machinery. Removing the listener and piping happen in the same tick,
|
|
129
|
-
// so no chunk can slip through the gap.
|
|
130
|
-
client.off("data", onEarlyData);
|
|
131
|
-
for (const chunk of pending) upstream.write(chunk);
|
|
132
|
-
pending.length = 0;
|
|
133
|
-
|
|
134
|
-
client.pipe(upstream);
|
|
135
|
-
upstream.pipe(client);
|
|
136
|
-
|
|
137
|
-
// The client may have half-closed while we were still connecting.
|
|
138
|
-
if (clientEnded) upstream.end();
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export interface EdgeForwarderOptions {
|
|
142
|
-
publicPort: number;
|
|
143
|
-
host?: string;
|
|
144
|
-
/** How long a client waits for a reachable server. Defaults to 5s. */
|
|
145
|
-
connectWindowMs?: number;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Bind the public port and start forwarding. Resolves once listening so the
|
|
150
|
-
* caller can fail fast when the port is unavailable.
|
|
151
|
-
*/
|
|
152
|
-
export function startEdgeForwarder(
|
|
153
|
-
opts: EdgeForwarderOptions,
|
|
154
|
-
): Promise<net.Server> {
|
|
155
|
-
const host = opts.host ?? "0.0.0.0";
|
|
156
|
-
const connectWindowMs = opts.connectWindowMs ?? CONNECT_WINDOW_MS;
|
|
157
|
-
return new Promise((resolvePromise, reject) => {
|
|
158
|
-
// allowHalfOpen keeps the other direction alive when one side sends FIN —
|
|
159
|
-
// long-lived chat WS and SSE streams depend on it.
|
|
160
|
-
const server = net.createServer({ allowHalfOpen: true }, (client) => {
|
|
161
|
-
void handleConnection(client, connectWindowMs);
|
|
162
|
-
});
|
|
163
|
-
server.once("error", reject);
|
|
164
|
-
server.listen(opts.publicPort, host, () => {
|
|
165
|
-
server.removeListener("error", reject);
|
|
166
|
-
resolvePromise(server);
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// ─── Process entry ─────────────────────────────────────────────────────
|
|
172
|
-
// Mirrors the `__serve__` guard in src/server/index.ts: the supervisor
|
|
173
|
-
// re-invokes the binary as `<bin> __edge__ <publicPort> <host>`.
|
|
174
|
-
if (process.argv.includes("__edge__")) {
|
|
175
|
-
const idx = process.argv.indexOf("__edge__");
|
|
176
|
-
const publicPort = parseInt(process.argv[idx + 1] ?? "", 10);
|
|
177
|
-
const host = process.argv[idx + 2] ?? "0.0.0.0";
|
|
178
|
-
|
|
179
|
-
if (!Number.isInteger(publicPort) || publicPort <= 0 || publicPort > 65535) {
|
|
180
|
-
process.stderr.write(`[edge] Invalid public port: ${process.argv[idx + 1]}\n`);
|
|
181
|
-
process.exit(2);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
try {
|
|
185
|
-
await startEdgeForwarder({ publicPort, host });
|
|
186
|
-
process.stderr.write(`[edge] Listening on ${host}:${publicPort}\n`);
|
|
187
|
-
} catch (err) {
|
|
188
|
-
process.stderr.write(`[edge] Failed to bind ${host}:${publicPort}: ${err}\n`);
|
|
189
|
-
process.exit(1);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Edge forwarder — owns the public port and pipes raw TCP to the server's
|
|
3
|
+
* current loopback port.
|
|
4
|
+
*
|
|
5
|
+
* ─── CRITICAL INVARIANT: THIS PROCESS MUST NEVER SPAWN A CHILD ───
|
|
6
|
+
*
|
|
7
|
+
* On Windows a child spawned with fd stdio inherits every inheritable handle,
|
|
8
|
+
* including a listening socket. When such a descendant is orphaned it keeps the
|
|
9
|
+
* socket open, so the port stays in LISTEN under a dead PID and can never be
|
|
10
|
+
* rebound — a "zombie port". The server hits this constantly because it spawns
|
|
11
|
+
* chat/tool/MCP subprocesses; those inherit its listener and wedge its port.
|
|
12
|
+
*
|
|
13
|
+
* The edge exists precisely because it spawns nothing, so its socket cannot be
|
|
14
|
+
* inherited and its port cannot zombie. cloudflared can therefore stay pinned
|
|
15
|
+
* to it forever and the public URL stops rotating. Adding any child-process
|
|
16
|
+
* call to this file reintroduces the exact bug the edge was built to remove.
|
|
17
|
+
*
|
|
18
|
+
* Raw TCP, never HTTP: WebSocket upgrades and SSE streams pass through
|
|
19
|
+
* untouched, and forwarded requests keep the headers cloudflared set.
|
|
20
|
+
*/
|
|
21
|
+
import net from "node:net";
|
|
22
|
+
import { resolveTargetPort, _resetTargetCache } from "./edge-target-resolver.ts";
|
|
23
|
+
|
|
24
|
+
/** Delay between attempts while waiting for the server to come back. */
|
|
25
|
+
const CONNECT_RETRY_MS = 250;
|
|
26
|
+
/**
|
|
27
|
+
* How long a client connection waits for a reachable server before giving up.
|
|
28
|
+
* During an upgrade the server is briefly absent; refusing instantly would
|
|
29
|
+
* surface as a broken page rather than a slow one.
|
|
30
|
+
*/
|
|
31
|
+
const CONNECT_WINDOW_MS = 5000;
|
|
32
|
+
|
|
33
|
+
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
34
|
+
|
|
35
|
+
/** Resolve once the socket is connected; reject on the first connect error. */
|
|
36
|
+
function connectUpstream(port: number): Promise<net.Socket> {
|
|
37
|
+
return new Promise((resolvePromise, reject) => {
|
|
38
|
+
const upstream = net.connect({
|
|
39
|
+
port,
|
|
40
|
+
host: "127.0.0.1", // never taken from status.json — loopback only
|
|
41
|
+
allowHalfOpen: true,
|
|
42
|
+
});
|
|
43
|
+
const onError = (err: Error) => {
|
|
44
|
+
upstream.destroy();
|
|
45
|
+
reject(err);
|
|
46
|
+
};
|
|
47
|
+
upstream.once("error", onError);
|
|
48
|
+
upstream.once("connect", () => {
|
|
49
|
+
upstream.removeListener("error", onError);
|
|
50
|
+
resolvePromise(upstream);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Largest amount of client data held while waiting for the server. Bounds the
|
|
57
|
+
* memory a slow upload can pin during an upgrade window.
|
|
58
|
+
*/
|
|
59
|
+
const MAX_PENDING_BYTES = 8 * 1024 * 1024;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Pipe one client connection to the server.
|
|
63
|
+
*
|
|
64
|
+
* Resolving the upstream is async, so the first bytes usually arrive before
|
|
65
|
+
* there is anywhere to send them. They must be captured by a `data` listener
|
|
66
|
+
* attached synchronously on this very tick and replayed once the upstream
|
|
67
|
+
* exists.
|
|
68
|
+
*
|
|
69
|
+
* `socket.pause()` is NOT a substitute — verified against Bun 1.3.13: bytes
|
|
70
|
+
* that arrive while paused are lost, and a later `pipe()`/`resume()` does not
|
|
71
|
+
* bring them back. A socket with no `data` listener drops them outright too.
|
|
72
|
+
* Both variants silently swallowed the first request in testing.
|
|
73
|
+
*/
|
|
74
|
+
async function handleConnection(
|
|
75
|
+
client: net.Socket,
|
|
76
|
+
connectWindowMs: number,
|
|
77
|
+
): Promise<void> {
|
|
78
|
+
const pending: Buffer[] = [];
|
|
79
|
+
let pendingBytes = 0;
|
|
80
|
+
let clientEnded = false;
|
|
81
|
+
const onEarlyData = (chunk: Buffer) => {
|
|
82
|
+
pendingBytes += chunk.length;
|
|
83
|
+
if (pendingBytes > MAX_PENDING_BYTES) {
|
|
84
|
+
client.destroy();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
pending.push(chunk);
|
|
88
|
+
};
|
|
89
|
+
client.on("data", onEarlyData);
|
|
90
|
+
client.once("end", () => { clientEnded = true; });
|
|
91
|
+
|
|
92
|
+
let upstream: net.Socket | null = null;
|
|
93
|
+
let clientGone = false;
|
|
94
|
+
|
|
95
|
+
// A socket error with no listener is an unhandled 'error' event, which would
|
|
96
|
+
// take the whole edge process — and the public port — down with it.
|
|
97
|
+
const teardown = () => {
|
|
98
|
+
upstream?.destroy();
|
|
99
|
+
client.destroy();
|
|
100
|
+
};
|
|
101
|
+
client.on("error", teardown);
|
|
102
|
+
client.once("close", () => { clientGone = true; });
|
|
103
|
+
|
|
104
|
+
const deadline = Date.now() + connectWindowMs;
|
|
105
|
+
while (!clientGone && Date.now() < deadline) {
|
|
106
|
+
const port = resolveTargetPort();
|
|
107
|
+
if (port !== null) {
|
|
108
|
+
try {
|
|
109
|
+
upstream = await connectUpstream(port);
|
|
110
|
+
break;
|
|
111
|
+
} catch {
|
|
112
|
+
// Server not up yet (or moved mid-restart) — re-resolve and retry.
|
|
113
|
+
_resetTargetCache();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
await sleep(CONNECT_RETRY_MS);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (clientGone) return;
|
|
120
|
+
if (!upstream) {
|
|
121
|
+
client.destroy();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
upstream.on("error", teardown);
|
|
126
|
+
|
|
127
|
+
// Replay what arrived during the connect, then hand both directions to the
|
|
128
|
+
// stream machinery. Removing the listener and piping happen in the same tick,
|
|
129
|
+
// so no chunk can slip through the gap.
|
|
130
|
+
client.off("data", onEarlyData);
|
|
131
|
+
for (const chunk of pending) upstream.write(chunk);
|
|
132
|
+
pending.length = 0;
|
|
133
|
+
|
|
134
|
+
client.pipe(upstream);
|
|
135
|
+
upstream.pipe(client);
|
|
136
|
+
|
|
137
|
+
// The client may have half-closed while we were still connecting.
|
|
138
|
+
if (clientEnded) upstream.end();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface EdgeForwarderOptions {
|
|
142
|
+
publicPort: number;
|
|
143
|
+
host?: string;
|
|
144
|
+
/** How long a client waits for a reachable server. Defaults to 5s. */
|
|
145
|
+
connectWindowMs?: number;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Bind the public port and start forwarding. Resolves once listening so the
|
|
150
|
+
* caller can fail fast when the port is unavailable.
|
|
151
|
+
*/
|
|
152
|
+
export function startEdgeForwarder(
|
|
153
|
+
opts: EdgeForwarderOptions,
|
|
154
|
+
): Promise<net.Server> {
|
|
155
|
+
const host = opts.host ?? "0.0.0.0";
|
|
156
|
+
const connectWindowMs = opts.connectWindowMs ?? CONNECT_WINDOW_MS;
|
|
157
|
+
return new Promise((resolvePromise, reject) => {
|
|
158
|
+
// allowHalfOpen keeps the other direction alive when one side sends FIN —
|
|
159
|
+
// long-lived chat WS and SSE streams depend on it.
|
|
160
|
+
const server = net.createServer({ allowHalfOpen: true }, (client) => {
|
|
161
|
+
void handleConnection(client, connectWindowMs);
|
|
162
|
+
});
|
|
163
|
+
server.once("error", reject);
|
|
164
|
+
server.listen(opts.publicPort, host, () => {
|
|
165
|
+
server.removeListener("error", reject);
|
|
166
|
+
resolvePromise(server);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ─── Process entry ─────────────────────────────────────────────────────
|
|
172
|
+
// Mirrors the `__serve__` guard in src/server/index.ts: the supervisor
|
|
173
|
+
// re-invokes the binary as `<bin> __edge__ <publicPort> <host>`.
|
|
174
|
+
if (process.argv.includes("__edge__")) {
|
|
175
|
+
const idx = process.argv.indexOf("__edge__");
|
|
176
|
+
const publicPort = parseInt(process.argv[idx + 1] ?? "", 10);
|
|
177
|
+
const host = process.argv[idx + 2] ?? "0.0.0.0";
|
|
178
|
+
|
|
179
|
+
if (!Number.isInteger(publicPort) || publicPort <= 0 || publicPort > 65535) {
|
|
180
|
+
process.stderr.write(`[edge] Invalid public port: ${process.argv[idx + 1]}\n`);
|
|
181
|
+
process.exit(2);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
await startEdgeForwarder({ publicPort, host });
|
|
186
|
+
process.stderr.write(`[edge] Listening on ${host}:${publicPort}\n`);
|
|
187
|
+
} catch (err) {
|
|
188
|
+
process.stderr.write(`[edge] Failed to bind ${host}:${publicPort}: ${err}\n`);
|
|
189
|
+
process.exit(1);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -8,6 +8,7 @@ import { resolve } from "node:path";
|
|
|
8
8
|
import { homedir } from "node:os";
|
|
9
9
|
import type { ChatEvent, ChatMessage } from "../types/chat.ts";
|
|
10
10
|
import { stringifyToolResultContent } from "../shared/tool-result-content.ts";
|
|
11
|
+
import { stripSharedContext } from "../shared/provider-context.ts";
|
|
11
12
|
|
|
12
13
|
const MAX_FILE_SIZE = 50 * 1024 * 1024; // 50MB
|
|
13
14
|
const TEAMMATE_MSG_RE = /<teammate-message[^>]*>[\s\S]*?<\/teammate-message>/g;
|
|
@@ -117,7 +118,7 @@ export function parseSessionMessage(
|
|
|
117
118
|
return {
|
|
118
119
|
id: msg.uuid,
|
|
119
120
|
role,
|
|
120
|
-
content: textContent,
|
|
121
|
+
content: role === "user" ? stripSharedContext(textContent) : textContent,
|
|
121
122
|
events: events.length > 0 ? events : undefined,
|
|
122
123
|
timestamp: msg.timestamp ?? new Date().toISOString(),
|
|
123
124
|
sdkUuid: msg.uuid,
|