@michengai/dsh-codex-ui 2.0.12 → 2.0.13
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 +7 -0
- package/CHANGELOG.zh-CN.md +7 -0
- package/README.md +3 -2
- package/README.zh-CN.md +3 -2
- package/lib/client.js +702 -0
- package/lib/composer-plugin.d.mts +2 -6
- package/lib/composer-plugin.mjs +745 -12
- package/lib/desktop-menu-plugin.mjs +1 -1
- package/lib/index.mjs +1 -1
- package/lib/{profile-resolver-B1VNepo_.mjs → profile-resolver-BkTv7lmZ.mjs} +2 -0
- package/lib/session-title-plugin.mjs +6 -0
- package/lib/settings-skin-plugin.mjs +1 -1
- package/lib/sidebar-plugin.mjs +1 -1
- package/lib/theme-plugin.mjs +1 -1
- package/locale/composer/en.json +1 -1
- package/locale/composer/zh.json +1 -1
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -75,6 +75,8 @@ window.__ModuleLoader__.load({
|
|
|
75
75
|
dependencies: `${CODEX_UI_API_BASE}/dependencies`,
|
|
76
76
|
settingsSkin: `${CODEX_UI_API_BASE}/settings-skin`,
|
|
77
77
|
composer: `${CODEX_UI_API_BASE}/composer`,
|
|
78
|
+
composerPrediction: `${CODEX_UI_API_BASE}/composer/prediction`,
|
|
79
|
+
composerPredictionStream: `${CODEX_UI_API_BASE}/composer/prediction/stream`,
|
|
78
80
|
sidebar: `${CODEX_UI_API_BASE}/sidebar`,
|
|
79
81
|
sections: `${CODEX_UI_API_BASE}/sections`,
|
|
80
82
|
theme: `${CODEX_UI_API_BASE}/theme`,
|
|
@@ -3105,6 +3107,9 @@ html[data-dsh-glass] body:not([data-ds-dark-theme]) .dcu-section-card{background
|
|
|
3105
3107
|
"settings.records": "记录与应用",
|
|
3106
3108
|
"settings.noResults": "没有匹配的设置分类",
|
|
3107
3109
|
"input.historyHint": "↑↓ 翻历史",
|
|
3110
|
+
"input.prediction.tab": "Tab 采纳",
|
|
3111
|
+
"input.prediction.more": "Alt+↓ 切换",
|
|
3112
|
+
"input.prediction.dismiss": "Esc 忽略",
|
|
3108
3113
|
"about.nav": "Codex UI",
|
|
3109
3114
|
"sections.pinned": "置顶",
|
|
3110
3115
|
"sections.default": "项目",
|
|
@@ -3291,6 +3296,9 @@ html[data-dsh-glass] body:not([data-ds-dark-theme]) .dcu-section-card{background
|
|
|
3291
3296
|
"settings.records": "Records and app",
|
|
3292
3297
|
"settings.noResults": "No matching settings categories",
|
|
3293
3298
|
"input.historyHint": "↑↓ History",
|
|
3299
|
+
"input.prediction.tab": "Tab accept",
|
|
3300
|
+
"input.prediction.more": "Alt+↓ cycle",
|
|
3301
|
+
"input.prediction.dismiss": "Esc dismiss",
|
|
3294
3302
|
"about.nav": "Codex UI",
|
|
3295
3303
|
"sections.pinned": "Pinned",
|
|
3296
3304
|
"sections.default": "Projects",
|
|
@@ -6416,6 +6424,686 @@ html[data-dsh-glass-float] [data-shortcut-modal="settings"][data-sidebar-collaps
|
|
|
6416
6424
|
});
|
|
6417
6425
|
}
|
|
6418
6426
|
//#endregion
|
|
6427
|
+
//#region src/client/composer/ComposerPredictionOverlay.tsx
|
|
6428
|
+
/**
|
|
6429
|
+
* Browser half of composer predictions (1:1 完整无损移植自 dsh-composer-predictions 1.2.8 原版源码)
|
|
6430
|
+
* 具备经真机验证的局部坐标测量、ResizeObserver / selectionchange 监听、流式 SSE 消费器与 Tab/Alt 按键拦截
|
|
6431
|
+
*/
|
|
6432
|
+
const h = react.default.createElement;
|
|
6433
|
+
const ENDPOINT = CODEX_UI_API_ENDPOINTS.composerPredictionStream;
|
|
6434
|
+
/** Wait for the settled UI before asking, so a fresh turn is fully committed. */
|
|
6435
|
+
const SETTLE_MS = 600;
|
|
6436
|
+
/** Wait for a typing pause before asking for a continuation. */
|
|
6437
|
+
const TYPE_PAUSE_MS = 500;
|
|
6438
|
+
/** A continuation needs enough text to continue from. */
|
|
6439
|
+
const CONTINUE_MIN_CHARS = 3;
|
|
6440
|
+
/** Ignore the first characters of a streaming answer to avoid flicker. */
|
|
6441
|
+
const STREAM_MIN_CHARS = 3;
|
|
6442
|
+
/** Editor text insets from the resident composer styles. */
|
|
6443
|
+
const EDITOR_INSET_TOP = 4;
|
|
6444
|
+
const EDITOR_INSET_LEFT = 14;
|
|
6445
|
+
const EDITOR_INSET_RIGHT = 8;
|
|
6446
|
+
/** The editable scroll container's right margin, outside the text column. */
|
|
6447
|
+
const EDITOR_SCROLL_MARGIN = 4;
|
|
6448
|
+
const FALLBACK_LINE_HEIGHT = 24;
|
|
6449
|
+
const NO_GHOST = {
|
|
6450
|
+
mode: "next",
|
|
6451
|
+
items: [],
|
|
6452
|
+
index: 0,
|
|
6453
|
+
stream: "",
|
|
6454
|
+
active: false
|
|
6455
|
+
};
|
|
6456
|
+
const NEXT_GHOST_STYLE = {
|
|
6457
|
+
position: "absolute",
|
|
6458
|
+
display: "flex",
|
|
6459
|
+
alignItems: "center",
|
|
6460
|
+
gap: "8px",
|
|
6461
|
+
boxSizing: "border-box",
|
|
6462
|
+
fontFamily: "inherit",
|
|
6463
|
+
fontSize: "inherit",
|
|
6464
|
+
lineHeight: "inherit",
|
|
6465
|
+
color: "var(--dsw-alias-label-caption)",
|
|
6466
|
+
background: "var(--dsw-specific-input-major)",
|
|
6467
|
+
overflow: "hidden",
|
|
6468
|
+
whiteSpace: "nowrap",
|
|
6469
|
+
pointerEvents: "none",
|
|
6470
|
+
userSelect: "none",
|
|
6471
|
+
zIndex: 2
|
|
6472
|
+
};
|
|
6473
|
+
/** The continuation ghost hugs its text and sits where the caret is. */
|
|
6474
|
+
const CONTINUE_GHOST_STYLE = {
|
|
6475
|
+
...NEXT_GHOST_STYLE,
|
|
6476
|
+
gap: "6px",
|
|
6477
|
+
padding: "0 4px",
|
|
6478
|
+
borderRadius: "4px",
|
|
6479
|
+
zIndex: 3
|
|
6480
|
+
};
|
|
6481
|
+
const TEXT_STYLE = {
|
|
6482
|
+
flex: "1 1 auto",
|
|
6483
|
+
minWidth: 0,
|
|
6484
|
+
overflow: "hidden",
|
|
6485
|
+
textOverflow: "ellipsis",
|
|
6486
|
+
whiteSpace: "nowrap",
|
|
6487
|
+
opacity: .9
|
|
6488
|
+
};
|
|
6489
|
+
const CONTINUE_TEXT_STYLE = {
|
|
6490
|
+
...TEXT_STYLE,
|
|
6491
|
+
flex: "0 1 auto"
|
|
6492
|
+
};
|
|
6493
|
+
const KEYCAP_STYLE = {
|
|
6494
|
+
flex: "none",
|
|
6495
|
+
fontSize: "11px",
|
|
6496
|
+
lineHeight: "16px",
|
|
6497
|
+
padding: "0 5px",
|
|
6498
|
+
borderRadius: "4px",
|
|
6499
|
+
border: "1px solid var(--dsw-alias-border-l2)",
|
|
6500
|
+
color: "var(--dsw-alias-label-tertiary)",
|
|
6501
|
+
background: "transparent"
|
|
6502
|
+
};
|
|
6503
|
+
const BADGE_STYLE = {
|
|
6504
|
+
flex: "none",
|
|
6505
|
+
fontSize: "10px",
|
|
6506
|
+
lineHeight: "14px",
|
|
6507
|
+
color: "var(--dsw-alias-label-tertiary)",
|
|
6508
|
+
opacity: .8
|
|
6509
|
+
};
|
|
6510
|
+
/**
|
|
6511
|
+
* Locate the composer card this overlay actually belongs to.
|
|
6512
|
+
*
|
|
6513
|
+
* The seat is rendered inside the resident composer card, so the anchor node
|
|
6514
|
+
* is an ancestor-scoped key: a global `querySelector` would pick whichever
|
|
6515
|
+
* card happens to be first in the document and paint that conversation's
|
|
6516
|
+
* ghost over another one.
|
|
6517
|
+
*/
|
|
6518
|
+
function composerElementsNear(anchor) {
|
|
6519
|
+
if (typeof document === "undefined") return null;
|
|
6520
|
+
if (anchor === null || anchor === void 0 || !anchor.isConnected) return null;
|
|
6521
|
+
const card = anchor.closest("[data-composer-card]");
|
|
6522
|
+
if (card === null) return null;
|
|
6523
|
+
const editor = card.querySelector("[contenteditable=\"true\"]") || card.querySelector("[role=\"textbox\"]");
|
|
6524
|
+
return editor === null ? null : {
|
|
6525
|
+
card,
|
|
6526
|
+
editor
|
|
6527
|
+
};
|
|
6528
|
+
}
|
|
6529
|
+
/** The collapsed selection inside the editor, when it is there at all. */
|
|
6530
|
+
function readCaret(editor) {
|
|
6531
|
+
if (typeof window === "undefined" || typeof document === "undefined") return null;
|
|
6532
|
+
const selection = window.getSelection();
|
|
6533
|
+
if (selection === null || selection.rangeCount === 0) return null;
|
|
6534
|
+
const range = selection.getRangeAt(0);
|
|
6535
|
+
if (range === null || !range.collapsed) return null;
|
|
6536
|
+
if (!editor.contains(range.startContainer)) return null;
|
|
6537
|
+
const prefixRange = document.createRange();
|
|
6538
|
+
prefixRange.selectNodeContents(editor);
|
|
6539
|
+
prefixRange.setEnd(range.startContainer, range.startOffset);
|
|
6540
|
+
const prefix = prefixRange.toString();
|
|
6541
|
+
const rect = caretRect(range);
|
|
6542
|
+
if (rect === null) return null;
|
|
6543
|
+
return {
|
|
6544
|
+
prefix,
|
|
6545
|
+
left: rect.left,
|
|
6546
|
+
top: rect.top,
|
|
6547
|
+
height: rect.height
|
|
6548
|
+
};
|
|
6549
|
+
}
|
|
6550
|
+
function caretRect(range) {
|
|
6551
|
+
let rect = range.getBoundingClientRect();
|
|
6552
|
+
if (rect.width === 0 && rect.height === 0) {
|
|
6553
|
+
const rects = range.getClientRects();
|
|
6554
|
+
if (rects.length > 0) rect = rects[0];
|
|
6555
|
+
}
|
|
6556
|
+
if (rect.width === 0 && rect.height === 0 || rect.top === 0 && rect.left === 0) {
|
|
6557
|
+
const fallback = rectBeforeCaret(range);
|
|
6558
|
+
if (fallback !== null) return fallback;
|
|
6559
|
+
}
|
|
6560
|
+
if (rect.width === 0 && rect.height === 0) return null;
|
|
6561
|
+
return {
|
|
6562
|
+
left: rect.left,
|
|
6563
|
+
top: rect.top,
|
|
6564
|
+
height: rect.height
|
|
6565
|
+
};
|
|
6566
|
+
}
|
|
6567
|
+
/** Measure the character before the caret when the collapsed range is empty. */
|
|
6568
|
+
function rectBeforeCaret(range) {
|
|
6569
|
+
const node = range.startContainer;
|
|
6570
|
+
const offset = range.startOffset;
|
|
6571
|
+
if (typeof Node === "undefined" || node === null || node.nodeType !== Node.TEXT_NODE || offset <= 0) return null;
|
|
6572
|
+
const probe = document.createRange();
|
|
6573
|
+
probe.setStart(node, offset - 1);
|
|
6574
|
+
probe.setEnd(node, offset);
|
|
6575
|
+
const rect = probe.getBoundingClientRect();
|
|
6576
|
+
if (rect.width === 0 && rect.height === 0) return null;
|
|
6577
|
+
return {
|
|
6578
|
+
left: rect.right,
|
|
6579
|
+
top: rect.top,
|
|
6580
|
+
height: rect.height
|
|
6581
|
+
};
|
|
6582
|
+
}
|
|
6583
|
+
/** Whether the caret is at the effective end of the draft. */
|
|
6584
|
+
function caretAtEnd(editor, draft) {
|
|
6585
|
+
const caret = readCaret(editor);
|
|
6586
|
+
if (caret === null) return null;
|
|
6587
|
+
const draftText = collapse(draft.trimEnd());
|
|
6588
|
+
const prefix = collapse(caret.prefix.trimEnd());
|
|
6589
|
+
if (prefix === "") return null;
|
|
6590
|
+
return draftText === prefix ? caret : null;
|
|
6591
|
+
}
|
|
6592
|
+
function collapse(text) {
|
|
6593
|
+
return text.replace(/\s+/gu, " ").trim();
|
|
6594
|
+
}
|
|
6595
|
+
/** A half-typed `/command` or `@reference` is not a message continuation. */
|
|
6596
|
+
function isTriggerToken(draft) {
|
|
6597
|
+
return /(?:^|\s)\/[^\s]*$/u.test(draft) || /(?:^|\s)@"?[^\s]*$/u.test(draft);
|
|
6598
|
+
}
|
|
6599
|
+
/** Show the model's first line while it is still streaming. */
|
|
6600
|
+
function streamingCandidate(stream) {
|
|
6601
|
+
if (stream.trim().length < STREAM_MIN_CHARS) return "";
|
|
6602
|
+
for (const raw of stream.split(/\r?\n/u)) {
|
|
6603
|
+
let line = raw.trim();
|
|
6604
|
+
if (line === "") continue;
|
|
6605
|
+
if (/^none$/iu.test(line)) return "";
|
|
6606
|
+
line = cleanLine(line);
|
|
6607
|
+
if (line === "") continue;
|
|
6608
|
+
return line.length > 240 ? line.slice(0, 240) : line;
|
|
6609
|
+
}
|
|
6610
|
+
return "";
|
|
6611
|
+
}
|
|
6612
|
+
function cleanLine(line) {
|
|
6613
|
+
return line.replace(/^(?:[-*•]|\d+[.)])\s+/u, "").replace(/^["'`\u201c\u201d]+|["'`\u201c\u201d]+$/gu, "").replace(/\s+/gu, " ").trim();
|
|
6614
|
+
}
|
|
6615
|
+
/**
|
|
6616
|
+
* Join a continuation onto the draft without gluing words together.
|
|
6617
|
+
* Avoid repeating a separator already present in the exact draft.
|
|
6618
|
+
*/
|
|
6619
|
+
function joinContinuation(draft, value) {
|
|
6620
|
+
if (draft === "" || value === "") return value;
|
|
6621
|
+
if (/\s$/u.test(draft)) return value.replace(/^\s+/u, "");
|
|
6622
|
+
if (/^\s/u.test(value)) return value;
|
|
6623
|
+
const last = draft.slice(-1);
|
|
6624
|
+
const first = value.slice(0, 1);
|
|
6625
|
+
if (isCjk(last) || isCjk(first)) return value;
|
|
6626
|
+
if (/[([{/\-—"'`]$/u.test(last)) return value;
|
|
6627
|
+
if (/^[,.;:!?)\]}%]/u.test(first)) return value;
|
|
6628
|
+
return ` ${value}`;
|
|
6629
|
+
}
|
|
6630
|
+
function isCjk(character) {
|
|
6631
|
+
return /[\u3040-\u30ff\u3400-\u9fff\uac00-\ud7af]/u.test(character);
|
|
6632
|
+
}
|
|
6633
|
+
function sameBox(left, right) {
|
|
6634
|
+
return left !== null && left !== void 0 && left.top === right.top && left.left === right.left && left.height === right.height && left.right === right.right && left.maxWidth === right.maxWidth;
|
|
6635
|
+
}
|
|
6636
|
+
/** Parse a fully buffered SSE body, for shells that cannot stream fetch bodies. */
|
|
6637
|
+
function parseSseText(text) {
|
|
6638
|
+
const events = [];
|
|
6639
|
+
for (const frame of text.split("\n\n")) for (const line of frame.split("\n")) {
|
|
6640
|
+
if (!line.startsWith("data:")) continue;
|
|
6641
|
+
const payload = line.slice(5).trim();
|
|
6642
|
+
if (payload === "") continue;
|
|
6643
|
+
try {
|
|
6644
|
+
events.push(JSON.parse(payload));
|
|
6645
|
+
} catch {}
|
|
6646
|
+
}
|
|
6647
|
+
return events;
|
|
6648
|
+
}
|
|
6649
|
+
/** Read one SSE body and hand every `data:` payload to the callback. */
|
|
6650
|
+
async function consumeStream(body, signal, onEvent) {
|
|
6651
|
+
const reader = body.getReader();
|
|
6652
|
+
const decoder = new TextDecoder();
|
|
6653
|
+
let buffer = "";
|
|
6654
|
+
let terminal = false;
|
|
6655
|
+
try {
|
|
6656
|
+
for (;;) {
|
|
6657
|
+
const { done, value } = await reader.read();
|
|
6658
|
+
if (done) break;
|
|
6659
|
+
if (signal.aborted) break;
|
|
6660
|
+
buffer += decoder.decode(value, { stream: true });
|
|
6661
|
+
let boundary = buffer.indexOf("\n\n");
|
|
6662
|
+
while (boundary >= 0) {
|
|
6663
|
+
const frame = buffer.slice(0, boundary);
|
|
6664
|
+
buffer = buffer.slice(boundary + 2);
|
|
6665
|
+
for (const line of frame.split("\n")) {
|
|
6666
|
+
if (!line.startsWith("data:")) continue;
|
|
6667
|
+
const payload = line.slice(5).trim();
|
|
6668
|
+
if (payload === "") continue;
|
|
6669
|
+
try {
|
|
6670
|
+
const event = JSON.parse(payload);
|
|
6671
|
+
if (event.type === "done" || event.type === "error") terminal = true;
|
|
6672
|
+
onEvent(event);
|
|
6673
|
+
} catch {}
|
|
6674
|
+
}
|
|
6675
|
+
boundary = buffer.indexOf("\n\n");
|
|
6676
|
+
}
|
|
6677
|
+
}
|
|
6678
|
+
return terminal;
|
|
6679
|
+
} finally {
|
|
6680
|
+
if (signal.aborted) try {
|
|
6681
|
+
await reader.cancel();
|
|
6682
|
+
} catch {}
|
|
6683
|
+
reader.releaseLock();
|
|
6684
|
+
}
|
|
6685
|
+
}
|
|
6686
|
+
function ComposerPredictionOverlay(props) {
|
|
6687
|
+
const { sessionId, useSession, useInput, inputActions, enabled = true, t } = props;
|
|
6688
|
+
const predictionsEnabled = enabled !== false;
|
|
6689
|
+
const draft = useInput ? useInput((state) => state === void 0 ? "" : state.draft || "") : "";
|
|
6690
|
+
const running = useSession ? useSession((state) => state?.running === true) : false;
|
|
6691
|
+
const [ghost, setGhost] = react.default.useState(NO_GHOST);
|
|
6692
|
+
const [box, setBox] = react.default.useState(null);
|
|
6693
|
+
const [compositionEnd, setCompositionEnd] = react.default.useState(0);
|
|
6694
|
+
const anchorRef = react.default.useRef(null);
|
|
6695
|
+
const elements = () => composerElementsNear(anchorRef.current);
|
|
6696
|
+
const abortRef = react.default.useRef(null);
|
|
6697
|
+
const composingRef = react.default.useRef(false);
|
|
6698
|
+
const dismissedRef = react.default.useRef(null);
|
|
6699
|
+
const acceptRef = react.default.useRef(null);
|
|
6700
|
+
const cycleRef = react.default.useRef(null);
|
|
6701
|
+
const currentRef = react.default.useRef("");
|
|
6702
|
+
const request = react.default.useCallback(async (mode, draftText) => {
|
|
6703
|
+
if (sessionId === void 0 || sessionId === null) return;
|
|
6704
|
+
if (typeof document !== "undefined" && document.hidden) return;
|
|
6705
|
+
abortRef.current?.abort();
|
|
6706
|
+
const controller = new AbortController();
|
|
6707
|
+
abortRef.current = controller;
|
|
6708
|
+
setGhost({
|
|
6709
|
+
mode,
|
|
6710
|
+
items: [],
|
|
6711
|
+
index: 0,
|
|
6712
|
+
stream: "",
|
|
6713
|
+
active: true
|
|
6714
|
+
});
|
|
6715
|
+
const settle = (items) => {
|
|
6716
|
+
if (controller.signal.aborted) return;
|
|
6717
|
+
setGhost((previous) => {
|
|
6718
|
+
if (previous.mode !== mode) return previous;
|
|
6719
|
+
if (items.length === 0) return NO_GHOST;
|
|
6720
|
+
return {
|
|
6721
|
+
mode,
|
|
6722
|
+
items,
|
|
6723
|
+
index: 0,
|
|
6724
|
+
stream: "",
|
|
6725
|
+
active: false
|
|
6726
|
+
};
|
|
6727
|
+
});
|
|
6728
|
+
};
|
|
6729
|
+
try {
|
|
6730
|
+
const response = await fetch(ENDPOINT, {
|
|
6731
|
+
method: "POST",
|
|
6732
|
+
headers: {
|
|
6733
|
+
"content-type": "application/json",
|
|
6734
|
+
accept: "text/event-stream"
|
|
6735
|
+
},
|
|
6736
|
+
body: JSON.stringify({
|
|
6737
|
+
sessionId,
|
|
6738
|
+
mode,
|
|
6739
|
+
draft: draftText
|
|
6740
|
+
}),
|
|
6741
|
+
signal: controller.signal
|
|
6742
|
+
});
|
|
6743
|
+
if (!response.ok || controller.signal.aborted) {
|
|
6744
|
+
settle([]);
|
|
6745
|
+
return;
|
|
6746
|
+
}
|
|
6747
|
+
if (!(response.headers.get("content-type") || "").includes("text/event-stream")) {
|
|
6748
|
+
const payload = await response.json().catch(() => null);
|
|
6749
|
+
settle(Array.isArray(payload?.suggestions) ? payload.suggestions.filter((item) => typeof item === "string" && item !== "") : typeof payload?.suggestion === "string" && payload.suggestion !== "" ? [payload.suggestion] : []);
|
|
6750
|
+
return;
|
|
6751
|
+
}
|
|
6752
|
+
if (response.body === null) {
|
|
6753
|
+
const done = parseSseText(await response.text().catch(() => "")).find((event) => event.type === "done");
|
|
6754
|
+
settle(Array.isArray(done?.items) ? done.items.filter((item) => typeof item === "string" && item !== "") : []);
|
|
6755
|
+
return;
|
|
6756
|
+
}
|
|
6757
|
+
if (!await consumeStream(response.body, controller.signal, (event) => {
|
|
6758
|
+
if (controller.signal.aborted) return;
|
|
6759
|
+
if (event.type === "delta" && typeof event.text === "string") setGhost((previous) => previous.active && previous.mode === mode ? {
|
|
6760
|
+
...previous,
|
|
6761
|
+
stream: previous.stream + event.text
|
|
6762
|
+
} : previous);
|
|
6763
|
+
else if (event.type === "done") {
|
|
6764
|
+
const items = Array.isArray(event.items) ? event.items.filter((item) => typeof item === "string" && item !== "") : [];
|
|
6765
|
+
settle(items);
|
|
6766
|
+
} else if (event.type === "error") settle([]);
|
|
6767
|
+
})) settle([]);
|
|
6768
|
+
} catch {
|
|
6769
|
+
if (!controller.signal.aborted) settle([]);
|
|
6770
|
+
}
|
|
6771
|
+
}, [sessionId]);
|
|
6772
|
+
react.default.useEffect(() => {
|
|
6773
|
+
if (predictionsEnabled) return;
|
|
6774
|
+
abortRef.current?.abort();
|
|
6775
|
+
setGhost(NO_GHOST);
|
|
6776
|
+
setBox(null);
|
|
6777
|
+
}, [predictionsEnabled]);
|
|
6778
|
+
react.default.useEffect(() => {
|
|
6779
|
+
const onCompositionStart = () => {
|
|
6780
|
+
composingRef.current = true;
|
|
6781
|
+
abortRef.current?.abort();
|
|
6782
|
+
setGhost(NO_GHOST);
|
|
6783
|
+
setBox(null);
|
|
6784
|
+
};
|
|
6785
|
+
const onCompositionEnd = () => {
|
|
6786
|
+
composingRef.current = false;
|
|
6787
|
+
setCompositionEnd((value) => value + 1);
|
|
6788
|
+
};
|
|
6789
|
+
document.addEventListener("compositionstart", onCompositionStart, true);
|
|
6790
|
+
document.addEventListener("compositionend", onCompositionEnd, true);
|
|
6791
|
+
return () => {
|
|
6792
|
+
document.removeEventListener("compositionstart", onCompositionStart, true);
|
|
6793
|
+
document.removeEventListener("compositionend", onCompositionEnd, true);
|
|
6794
|
+
};
|
|
6795
|
+
}, []);
|
|
6796
|
+
react.default.useEffect(() => {
|
|
6797
|
+
dismissedRef.current = null;
|
|
6798
|
+
abortRef.current?.abort();
|
|
6799
|
+
setGhost(NO_GHOST);
|
|
6800
|
+
setBox(null);
|
|
6801
|
+
return () => abortRef.current?.abort();
|
|
6802
|
+
}, [sessionId]);
|
|
6803
|
+
react.default.useEffect(() => {
|
|
6804
|
+
abortRef.current?.abort();
|
|
6805
|
+
setGhost(NO_GHOST);
|
|
6806
|
+
if (dismissedRef.current !== null && dismissedRef.current.draft !== draft) dismissedRef.current = null;
|
|
6807
|
+
}, [draft]);
|
|
6808
|
+
react.default.useEffect(() => {
|
|
6809
|
+
if (!running) return;
|
|
6810
|
+
abortRef.current?.abort();
|
|
6811
|
+
setGhost(NO_GHOST);
|
|
6812
|
+
dismissedRef.current = null;
|
|
6813
|
+
}, [running]);
|
|
6814
|
+
react.default.useEffect(() => {
|
|
6815
|
+
if (sessionId === void 0 || sessionId === null) return void 0;
|
|
6816
|
+
if (!predictionsEnabled || running || draft !== "") return void 0;
|
|
6817
|
+
if (dismissedRef.current !== null && dismissedRef.current.draft === draft) return void 0;
|
|
6818
|
+
const timer = setTimeout(() => {
|
|
6819
|
+
if (composingRef.current) return;
|
|
6820
|
+
request("next", "");
|
|
6821
|
+
}, SETTLE_MS);
|
|
6822
|
+
return () => clearTimeout(timer);
|
|
6823
|
+
}, [
|
|
6824
|
+
sessionId,
|
|
6825
|
+
running,
|
|
6826
|
+
draft,
|
|
6827
|
+
request,
|
|
6828
|
+
predictionsEnabled,
|
|
6829
|
+
compositionEnd
|
|
6830
|
+
]);
|
|
6831
|
+
react.default.useEffect(() => {
|
|
6832
|
+
if (sessionId === void 0 || sessionId === null) return void 0;
|
|
6833
|
+
if (!predictionsEnabled || draft.trim() === "" || draft.length < CONTINUE_MIN_CHARS) return void 0;
|
|
6834
|
+
if (isTriggerToken(draft)) return void 0;
|
|
6835
|
+
if (dismissedRef.current !== null && dismissedRef.current.draft === draft) return void 0;
|
|
6836
|
+
const timer = setTimeout(() => {
|
|
6837
|
+
if (composingRef.current) return;
|
|
6838
|
+
const found = elements();
|
|
6839
|
+
if (found === null) return;
|
|
6840
|
+
if (caretAtEnd(found.editor, draft) === null) return;
|
|
6841
|
+
request("continue", draft.slice(-4e3));
|
|
6842
|
+
}, TYPE_PAUSE_MS);
|
|
6843
|
+
return () => clearTimeout(timer);
|
|
6844
|
+
}, [
|
|
6845
|
+
sessionId,
|
|
6846
|
+
draft,
|
|
6847
|
+
request,
|
|
6848
|
+
predictionsEnabled,
|
|
6849
|
+
compositionEnd
|
|
6850
|
+
]);
|
|
6851
|
+
react.default.useEffect(() => {
|
|
6852
|
+
const onVisibility = () => {
|
|
6853
|
+
if (!predictionsEnabled || document.hidden) return;
|
|
6854
|
+
if (draft !== "" || running) return;
|
|
6855
|
+
if (currentRef.current !== "" || dismissedRef.current !== null) return;
|
|
6856
|
+
if (sessionId === void 0 || sessionId === null) return;
|
|
6857
|
+
request("next", "");
|
|
6858
|
+
};
|
|
6859
|
+
document.addEventListener("visibilitychange", onVisibility);
|
|
6860
|
+
return () => document.removeEventListener("visibilitychange", onVisibility);
|
|
6861
|
+
}, [
|
|
6862
|
+
draft,
|
|
6863
|
+
running,
|
|
6864
|
+
sessionId,
|
|
6865
|
+
request,
|
|
6866
|
+
predictionsEnabled
|
|
6867
|
+
]);
|
|
6868
|
+
const rawCandidate = ghost.items.length > 0 ? ghost.items[Math.min(ghost.index, ghost.items.length - 1)] ?? "" : streamingCandidate(ghost.stream);
|
|
6869
|
+
const current = ghost.mode === "continue" ? joinContinuation(draft, rawCandidate) : rawCandidate;
|
|
6870
|
+
currentRef.current = current;
|
|
6871
|
+
const hasCandidate = current !== "";
|
|
6872
|
+
const visible = predictionsEnabled && current !== "" && box !== null && (ghost.mode === "next" ? draft === "" && !running : draft.trim() !== "" && !isTriggerToken(draft));
|
|
6873
|
+
react.default.useLayoutEffect(() => {
|
|
6874
|
+
if (ghost.mode !== "next") return void 0;
|
|
6875
|
+
const measure = () => {
|
|
6876
|
+
const found = elements();
|
|
6877
|
+
if (found === null) {
|
|
6878
|
+
setBox((previous) => previous === null ? previous : null);
|
|
6879
|
+
return;
|
|
6880
|
+
}
|
|
6881
|
+
const cardRect = found.card.getBoundingClientRect();
|
|
6882
|
+
const editorRect = found.editor.getBoundingClientRect();
|
|
6883
|
+
if (editorRect.width <= 0 || editorRect.height <= 0) {
|
|
6884
|
+
setBox((previous) => previous === null ? previous : null);
|
|
6885
|
+
return;
|
|
6886
|
+
}
|
|
6887
|
+
const computed = window.getComputedStyle(found.editor);
|
|
6888
|
+
const lineHeight = Number.parseFloat(computed.lineHeight);
|
|
6889
|
+
const paddingTop = Number.parseFloat(computed.paddingTop);
|
|
6890
|
+
const paddingLeft = Number.parseFloat(computed.paddingLeft);
|
|
6891
|
+
const topInset = Number.isFinite(paddingTop) ? paddingTop : EDITOR_INSET_TOP;
|
|
6892
|
+
const leftInset = Number.isFinite(paddingLeft) ? paddingLeft : EDITOR_INSET_LEFT;
|
|
6893
|
+
const next = {
|
|
6894
|
+
top: Math.max(0, Math.round(editorRect.top - cardRect.top + topInset)),
|
|
6895
|
+
left: Math.max(0, Math.round(editorRect.left - cardRect.left + leftInset)),
|
|
6896
|
+
right: Math.max(EDITOR_INSET_RIGHT, Math.round(cardRect.right - editorRect.right + EDITOR_SCROLL_MARGIN)),
|
|
6897
|
+
height: Number.isFinite(lineHeight) && lineHeight > 0 ? Math.round(lineHeight) : FALLBACK_LINE_HEIGHT
|
|
6898
|
+
};
|
|
6899
|
+
setBox((previous) => sameBox(previous, next) ? previous : next);
|
|
6900
|
+
};
|
|
6901
|
+
measure();
|
|
6902
|
+
const found = elements();
|
|
6903
|
+
let observer;
|
|
6904
|
+
if (found !== null && typeof ResizeObserver === "function") {
|
|
6905
|
+
observer = new ResizeObserver(measure);
|
|
6906
|
+
observer.observe(found.card);
|
|
6907
|
+
observer.observe(found.editor);
|
|
6908
|
+
}
|
|
6909
|
+
window.addEventListener("resize", measure);
|
|
6910
|
+
return () => {
|
|
6911
|
+
observer?.disconnect();
|
|
6912
|
+
window.removeEventListener("resize", measure);
|
|
6913
|
+
};
|
|
6914
|
+
}, [
|
|
6915
|
+
ghost.mode,
|
|
6916
|
+
draft,
|
|
6917
|
+
sessionId,
|
|
6918
|
+
running,
|
|
6919
|
+
hasCandidate
|
|
6920
|
+
]);
|
|
6921
|
+
react.default.useLayoutEffect(() => {
|
|
6922
|
+
if (ghost.mode !== "continue") return void 0;
|
|
6923
|
+
let frame = 0;
|
|
6924
|
+
const measure = () => {
|
|
6925
|
+
frame = 0;
|
|
6926
|
+
const found = elements();
|
|
6927
|
+
if (found === null) {
|
|
6928
|
+
setBox((previous) => previous === null ? previous : null);
|
|
6929
|
+
return;
|
|
6930
|
+
}
|
|
6931
|
+
const caret = caretAtEnd(found.editor, draft);
|
|
6932
|
+
if (caret === null) {
|
|
6933
|
+
setBox((previous) => previous === null ? previous : null);
|
|
6934
|
+
return;
|
|
6935
|
+
}
|
|
6936
|
+
const editorRect = found.editor.getBoundingClientRect();
|
|
6937
|
+
if (editorRect.width <= 0 || editorRect.height <= 0) {
|
|
6938
|
+
setBox((previous) => previous === null ? previous : null);
|
|
6939
|
+
return;
|
|
6940
|
+
}
|
|
6941
|
+
const cardRect = found.card.getBoundingClientRect();
|
|
6942
|
+
const computed = window.getComputedStyle(found.editor);
|
|
6943
|
+
const lineHeight = Number.parseFloat(computed.lineHeight);
|
|
6944
|
+
const height = Number.isFinite(caret.height) && caret.height > 0 ? Math.round(caret.height) : Number.isFinite(lineHeight) && lineHeight > 0 ? Math.round(lineHeight) : FALLBACK_LINE_HEIGHT;
|
|
6945
|
+
const desired = caret.left - cardRect.left;
|
|
6946
|
+
const left = Math.max(0, desired);
|
|
6947
|
+
const maxWidth = cardRect.right - (cardRect.left + left) - 16;
|
|
6948
|
+
if (maxWidth < 48) {
|
|
6949
|
+
setBox((previous) => previous === null ? previous : null);
|
|
6950
|
+
return;
|
|
6951
|
+
}
|
|
6952
|
+
const top = caret.top - cardRect.top;
|
|
6953
|
+
if (top < 0 || top > cardRect.height - 8) {
|
|
6954
|
+
setBox((previous) => previous === null ? previous : null);
|
|
6955
|
+
return;
|
|
6956
|
+
}
|
|
6957
|
+
const next = {
|
|
6958
|
+
top: Math.round(top),
|
|
6959
|
+
left: Math.round(left),
|
|
6960
|
+
maxWidth: Math.round(maxWidth),
|
|
6961
|
+
height
|
|
6962
|
+
};
|
|
6963
|
+
setBox((previous) => sameBox(previous, next) ? previous : next);
|
|
6964
|
+
};
|
|
6965
|
+
const schedule = () => {
|
|
6966
|
+
if (frame === 0) frame = requestAnimationFrame(measure);
|
|
6967
|
+
};
|
|
6968
|
+
measure();
|
|
6969
|
+
document.addEventListener("selectionchange", schedule);
|
|
6970
|
+
document.addEventListener("scroll", schedule, true);
|
|
6971
|
+
const found = elements();
|
|
6972
|
+
window.addEventListener("resize", schedule);
|
|
6973
|
+
let observer;
|
|
6974
|
+
if (found !== null && typeof ResizeObserver === "function") {
|
|
6975
|
+
observer = new ResizeObserver(schedule);
|
|
6976
|
+
observer.observe(found.card);
|
|
6977
|
+
observer.observe(found.editor);
|
|
6978
|
+
}
|
|
6979
|
+
return () => {
|
|
6980
|
+
if (frame !== 0) cancelAnimationFrame(frame);
|
|
6981
|
+
document.removeEventListener("selectionchange", schedule);
|
|
6982
|
+
document.removeEventListener("scroll", schedule, true);
|
|
6983
|
+
window.removeEventListener("resize", schedule);
|
|
6984
|
+
observer?.disconnect();
|
|
6985
|
+
};
|
|
6986
|
+
}, [
|
|
6987
|
+
ghost.mode,
|
|
6988
|
+
draft,
|
|
6989
|
+
sessionId,
|
|
6990
|
+
running,
|
|
6991
|
+
hasCandidate
|
|
6992
|
+
]);
|
|
6993
|
+
const accept = () => {
|
|
6994
|
+
const value = currentRef.current;
|
|
6995
|
+
if (value === "" || ghost.active) return;
|
|
6996
|
+
const expected = ghost.mode === "continue" ? draft + value : value;
|
|
6997
|
+
dismissedRef.current = { draft: expected };
|
|
6998
|
+
abortRef.current?.abort();
|
|
6999
|
+
setGhost(NO_GHOST);
|
|
7000
|
+
setBox(null);
|
|
7001
|
+
try {
|
|
7002
|
+
if (inputActions.insertText(value, inputActions.captureInsertion()) !== true) return;
|
|
7003
|
+
} catch {
|
|
7004
|
+
return;
|
|
7005
|
+
}
|
|
7006
|
+
try {
|
|
7007
|
+
const found = elements();
|
|
7008
|
+
if (found !== null && !found.card.contains(document.activeElement)) found.editor.focus();
|
|
7009
|
+
} catch {}
|
|
7010
|
+
};
|
|
7011
|
+
acceptRef.current = accept;
|
|
7012
|
+
const cycle = (delta) => {
|
|
7013
|
+
setGhost((previous) => {
|
|
7014
|
+
if (previous.items.length <= 1) return previous;
|
|
7015
|
+
const index = (previous.index + delta + previous.items.length) % previous.items.length;
|
|
7016
|
+
return {
|
|
7017
|
+
...previous,
|
|
7018
|
+
index
|
|
7019
|
+
};
|
|
7020
|
+
});
|
|
7021
|
+
};
|
|
7022
|
+
cycleRef.current = cycle;
|
|
7023
|
+
react.default.useEffect(() => {
|
|
7024
|
+
if (!visible) return void 0;
|
|
7025
|
+
const onKeyDown = (event) => {
|
|
7026
|
+
if (event.defaultPrevented) return;
|
|
7027
|
+
if (event.isComposing === true || event.keyCode === 229) return;
|
|
7028
|
+
const found = elements();
|
|
7029
|
+
if (found === null) return;
|
|
7030
|
+
const target = typeof Node !== "undefined" && event.target instanceof Node ? event.target : null;
|
|
7031
|
+
const inEditor = found.editor.contains(document.activeElement);
|
|
7032
|
+
if (!(found.card.contains(document.activeElement) || target !== null && found.card.contains(target))) return;
|
|
7033
|
+
if (event.key === "Escape") {
|
|
7034
|
+
event.preventDefault();
|
|
7035
|
+
event.stopPropagation();
|
|
7036
|
+
dismissedRef.current = { draft };
|
|
7037
|
+
abortRef.current?.abort();
|
|
7038
|
+
setGhost(NO_GHOST);
|
|
7039
|
+
setBox(null);
|
|
7040
|
+
return;
|
|
7041
|
+
}
|
|
7042
|
+
if (event.altKey && (event.key === "ArrowDown" || event.key === "ArrowUp")) {
|
|
7043
|
+
if (ghost.items.length <= 1) return;
|
|
7044
|
+
event.preventDefault();
|
|
7045
|
+
event.stopPropagation();
|
|
7046
|
+
cycleRef.current?.(event.key === "ArrowDown" ? 1 : -1);
|
|
7047
|
+
return;
|
|
7048
|
+
}
|
|
7049
|
+
if (event.key !== "Tab" || event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) return;
|
|
7050
|
+
if (!inEditor || ghost.active) return;
|
|
7051
|
+
if (ghost.mode === "continue" && caretAtEnd(found.editor, draft) === null) return;
|
|
7052
|
+
event.preventDefault();
|
|
7053
|
+
event.stopPropagation();
|
|
7054
|
+
acceptRef.current?.();
|
|
7055
|
+
};
|
|
7056
|
+
document.addEventListener("keydown", onKeyDown, true);
|
|
7057
|
+
return () => document.removeEventListener("keydown", onKeyDown, true);
|
|
7058
|
+
}, [
|
|
7059
|
+
visible,
|
|
7060
|
+
draft,
|
|
7061
|
+
ghost.items.length,
|
|
7062
|
+
ghost.active,
|
|
7063
|
+
ghost.mode
|
|
7064
|
+
]);
|
|
7065
|
+
const anchor = h("span", {
|
|
7066
|
+
ref: anchorRef,
|
|
7067
|
+
"data-composer-prediction-anchor": "",
|
|
7068
|
+
"aria-hidden": "true",
|
|
7069
|
+
style: { display: "none" }
|
|
7070
|
+
});
|
|
7071
|
+
if (!visible || box === null) return anchor;
|
|
7072
|
+
const continuation = ghost.mode === "continue";
|
|
7073
|
+
const style = continuation ? CONTINUE_GHOST_STYLE : NEXT_GHOST_STYLE;
|
|
7074
|
+
const showKeycap = !ghost.active && (!continuation || (box.maxWidth ?? 0) >= 96);
|
|
7075
|
+
const showBadge = ghost.items.length > 1;
|
|
7076
|
+
return h(react.default.Fragment, null, h("span", {
|
|
7077
|
+
role: "status",
|
|
7078
|
+
"aria-live": "polite",
|
|
7079
|
+
"aria-atomic": "true",
|
|
7080
|
+
style: {
|
|
7081
|
+
position: "absolute",
|
|
7082
|
+
width: 1,
|
|
7083
|
+
height: 1,
|
|
7084
|
+
padding: 0,
|
|
7085
|
+
margin: -1,
|
|
7086
|
+
overflow: "hidden",
|
|
7087
|
+
clipPath: "inset(50%)",
|
|
7088
|
+
whiteSpace: "nowrap"
|
|
7089
|
+
}
|
|
7090
|
+
}, ghost.active ? "" : t("input.prediction.dismiss") || current), h("div", {
|
|
7091
|
+
style: {
|
|
7092
|
+
...style,
|
|
7093
|
+
...box
|
|
7094
|
+
},
|
|
7095
|
+
"aria-hidden": "true",
|
|
7096
|
+
"data-composer-prediction": ghost.mode,
|
|
7097
|
+
title: current
|
|
7098
|
+
}, h("span", { style: continuation ? CONTINUE_TEXT_STYLE : TEXT_STYLE }, current), showBadge ? h("span", {
|
|
7099
|
+
style: BADGE_STYLE,
|
|
7100
|
+
title: t("input.prediction.more")
|
|
7101
|
+
}, `${ghost.index + 1}/${ghost.items.length}`) : null, showKeycap ? h("kbd", {
|
|
7102
|
+
style: KEYCAP_STYLE,
|
|
7103
|
+
title: t("input.prediction.tab")
|
|
7104
|
+
}, "Tab") : null), anchor);
|
|
7105
|
+
}
|
|
7106
|
+
//#endregion
|
|
6419
7107
|
//#region src/client/composer/new-conversation-style.ts
|
|
6420
7108
|
/** 新建页底部输入布局;保留宿主节点、插槽与 active 会话底部信息。 */
|
|
6421
7109
|
const NEW_CONVERSATION_STYLE = `
|
|
@@ -6551,6 +7239,20 @@ html[data-dsh-glass-float] [data-shortcut-modal="settings"][data-sidebar-collaps
|
|
|
6551
7239
|
locale: NS
|
|
6552
7240
|
}, SuggestionsDock));
|
|
6553
7241
|
cleaners.push(unregisterSuggestions);
|
|
7242
|
+
function PredictionSlot(props) {
|
|
7243
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ComposerPredictionOverlay, {
|
|
7244
|
+
...props,
|
|
7245
|
+
ctx,
|
|
7246
|
+
t
|
|
7247
|
+
});
|
|
7248
|
+
}
|
|
7249
|
+
const unregisterPrediction = ctx.slots.inject("conversation.input.overlay", () => ctx.slots.register({
|
|
7250
|
+
name: "conversation.input.overlay",
|
|
7251
|
+
id: "codex-ui-composer-prediction",
|
|
7252
|
+
order: 50,
|
|
7253
|
+
locale: NS
|
|
7254
|
+
}, PredictionSlot));
|
|
7255
|
+
cleaners.push(unregisterPrediction);
|
|
6554
7256
|
return () => {
|
|
6555
7257
|
for (const clean of cleaners.reverse()) try {
|
|
6556
7258
|
clean();
|