@pageops/sdk 0.1.33 → 0.1.35

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/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # PageOps SDK source baseline
2
+
3
+ This directory restores the maintainable SDK boundary from the verified public
4
+ `@pageops/sdk@0.1.34` artifacts. Version `0.1.35` adds developer-gated source
5
+ controls and conversation presentation behavior directly to the SDK instead of
6
+ patching host DOM from a business application.
7
+
8
+ `src/index.js` is the readable ESM runtime baseline, `src/styles.css` preserves
9
+ the 0.1.34 styles, and `src/extensions.css` contains the new non-minified style
10
+ rules. `npm run build` synchronizes ESM, UMD, and CSS under `dist/` without
11
+ network access or a bundler. Declarations remain explicit under `src/*.d.ts`
12
+ and `dist/*.d.ts`.
13
+
14
+ The host supplies `developerAccessUrl`. The endpoint result controls only the
15
+ source-package UI. Failed/missing access checks hide those controls. It does not
16
+ replace authorization on source APIs.
package/dist/index.d.ts CHANGED
@@ -23,6 +23,7 @@ declare class PageOpsSDKController {
23
23
  private sourceLoadError;
24
24
  private sourceUploading;
25
25
  private sourceRefreshTimer;
26
+ private developerAccess;
26
27
  private streaming;
27
28
  private asking;
28
29
  private waitingForFirstContent;
@@ -35,6 +36,7 @@ declare class PageOpsSDKController {
35
36
  private deletingThreadId;
36
37
  private streamFailure;
37
38
  private streamDone;
39
+ private activeQuestionStartedAt;
38
40
  private answerAudience;
39
41
  private floatingRatio;
40
42
  private floatingCleanup;
@@ -51,6 +53,7 @@ declare class PageOpsSDKController {
51
53
  private setFloatingPosition;
52
54
  private bindFloatingLauncher;
53
55
  private cleanupFloatingLauncher;
56
+ private loadDeveloperAccess;
54
57
  private ensureContext;
55
58
  private getPageContext;
56
59
  private answerAudienceStorageKey;
@@ -85,6 +88,7 @@ declare class PageOpsSDKController {
85
88
  private renderSourceUploader;
86
89
  private renderSourceItem;
87
90
  private renderSteps;
91
+ private renderThinkingProcess;
88
92
  private renderExecutionSteps;
89
93
  private bindPanelEvents;
90
94
  }
@@ -1 +1,54 @@
1
1
  .pageops-sdk{--po-text: #172033;--po-muted: #64748b;--po-border: #dbe5f5;--po-soft-border: #e2e8f0;--po-surface: #ffffff;--po-soft: #f8fafc;--po-panel: #fbfdff;--po-primary: #2563eb;--po-primary-dark: #1d4ed8;--po-shadow: 0 24px 70px rgb(15 23 42 / 18%);color:var(--po-text);font-family:Inter,ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,sans-serif}.pageops-sdk *,.pageops-sdk *:before,.pageops-sdk *:after{box-sizing:border-box}.pageops-sdk button,.pageops-sdk input,.pageops-sdk textarea,.pageops-sdk select{font:inherit}.pageops-sdk button{cursor:pointer}.po-fab{position:fixed;top:var(--pageops-fab-top,50%);right:24px;bottom:auto;z-index:2147483000;border:0;border-radius:999px;padding:12px 18px;background:linear-gradient(135deg,#2563eb,#14b8a6);color:#fff;font-weight:800;box-shadow:0 18px 42px #2563eb47;transform:translateY(-50%);cursor:grab;touch-action:none;user-select:none;-webkit-user-select:none}.po-fab.po-fab-dragging{cursor:grabbing}.po-panel{position:fixed;inset:40px;z-index:2147483000;display:grid;grid-template-columns:250px minmax(0,1fr) 340px;min-height:520px;overflow:hidden;border:1px solid var(--po-border);border-radius:24px;background:var(--po-surface);box-shadow:var(--po-shadow)}.po-sidebar{display:flex;min-width:0;min-height:0;flex-direction:column;gap:12px;overflow:hidden;border-right:1px solid #dbeafe;background:#eef5ff;padding:16px}.po-brand{display:flex;align-items:center;gap:10px}.po-brand strong,.po-brand span{display:block}.po-brand span{margin-top:2px;color:var(--po-muted);font-size:12px}.po-logo{width:34px;height:34px;flex:0 0 auto;border-radius:12px;background:linear-gradient(135deg,#5eead4,#2563eb)}.po-new,.po-composer button{border:0;border-radius:10px;background:var(--po-primary);color:#fff;font-weight:750}.po-new{width:100%;padding:10px 12px}.po-search,.po-composer textarea{width:100%;border:1px solid #cbd5e1;border-radius:10px;background:#fff;color:var(--po-text);outline:none}.po-search{padding:9px 10px}.po-thread-list{display:grid;align-content:start;gap:8px;min-height:0;overflow:auto}.po-thread-row{position:relative;display:grid;grid-template-columns:minmax(0,1fr) 32px;align-items:center;border:1px solid transparent;border-radius:10px}.po-thread-row.active{border-color:#bfdbfe;background:#fff}.po-thread{display:grid;gap:4px;width:100%;border:0;border-radius:10px;background:transparent;color:#334155;padding:9px 10px;text-align:left}.po-thread-row.active .po-thread{color:var(--po-text)}.po-thread span{color:#94a3b8;font-size:12px}.po-thread-delete{display:grid;width:28px;height:28px;place-items:center;border:0;border-radius:6px;background:transparent;color:#94a3b8;opacity:0}.po-thread-row:hover .po-thread-delete,.po-thread-row:focus-within .po-thread-delete{opacity:1}.po-thread-delete:hover{background:#fee2e2;color:#b91c1c}.po-thread-delete:disabled{cursor:not-allowed;opacity:.35}.po-trash-icon{position:relative;width:11px;height:12px;border:1.5px solid currentcolor;border-top:0;border-radius:0 0 2px 2px}.po-trash-icon:before{position:absolute;top:-4px;left:-2px;width:13px;border-top:1.5px solid currentcolor;content:""}.po-trash-icon:after{position:absolute;top:-6px;left:3px;width:4px;height:2px;border:1.5px solid currentcolor;border-bottom:0;border-radius:2px 2px 0 0;content:""}.po-conversation-error{margin:0;color:#b91c1c;font-size:12px}.po-chat{display:flex;min-width:0;min-height:0;flex-direction:column;background:linear-gradient(180deg,#fff,var(--po-panel));padding:22px 26px}.po-chat header,.po-trace header{display:flex;align-items:center;justify-content:space-between;gap:12px;border-bottom:1px solid var(--po-soft-border);padding-bottom:14px}.po-chat header p,.po-chat header h2,.po-trace header h2{margin:0}.po-chat header p,.po-chat header span{color:var(--po-muted);font-size:12px}.po-chat header h2,.po-trace header h2{font-size:20px;letter-spacing:0}.po-chat-title{min-width:0}.po-chat-title h2{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.po-audience{display:none;flex:0 0 auto;align-items:center;gap:8px}.po-audience>span{white-space:nowrap}.po-audience-options{display:grid;grid-template-columns:44px;height:32px;overflow:hidden;border:1px solid #cbd5e1;border-radius:6px;background:#f8fafc}.po-audience-options button{min-width:0;border:0;border-right:1px solid #e2e8f0;background:transparent;color:#475569;padding:0 8px;font-size:12px;font-weight:650}.po-audience-options button:last-child{border-right:0}.po-audience-options button[aria-pressed=true]{background:var(--po-primary);color:#fff}.po-audience-options button:disabled{cursor:not-allowed;opacity:.55}.po-messages{display:grid;align-content:start;gap:14px;min-height:0;flex:1;overflow:auto;padding:18px 4px 18px 0}.po-message{display:grid;max-width:82%;gap:6px;border:1px solid var(--po-soft-border);border-radius:14px;padding:11px 13px;line-height:1.55;white-space:pre-wrap;word-break:break-word}.po-message b{font-size:12px;opacity:.78}.po-message.user{justify-self:end;border-color:var(--po-primary);background:var(--po-primary);color:#fff}.po-message.assistant{justify-self:start;background:var(--po-soft)}.po-message.run-selected{border-color:#93c5fd;box-shadow:0 0 0 2px #3b82f614}.po-answer-meta{display:flex;min-width:0;align-items:center;flex-wrap:wrap;gap:6px 10px;border-top:1px solid var(--po-soft-border);margin-top:5px;padding-top:8px;color:var(--po-muted);font-size:11px}.po-answer-meta.selected{color:#1d4ed8}.po-run-id{display:flex;min-width:0;max-width:100%;align-items:center;gap:5px}.po-run-id code{display:block;max-width:148px;overflow:hidden;background:transparent;color:inherit;padding:0;text-overflow:ellipsis;white-space:nowrap}.po-answer-meta button{border:0;background:transparent;color:var(--po-primary-dark);padding:2px 0;font-size:11px;font-weight:700}.po-answer-meta button:hover{text-decoration:underline}.po-message.streaming{border-color:#bfdbfe;background:#eff6ff}.po-message.answer-error{border-color:#fecaca;background:#fff7f7;color:#991b1b}.po-message.answer-error>strong{color:#7f1d1d}.po-stream-stopped{justify-self:start;color:var(--po-muted);font-size:12px}.po-ai-waiting{display:inline-flex;align-items:center;gap:9px;min-height:24px;color:#475569}.po-ai-spinner{width:16px;height:16px;flex:0 0 auto;border:2px solid #bfdbfe;border-top-color:var(--po-primary);border-radius:50%;animation:po-ai-spin .7s linear infinite}.po-typing-cursor{display:inline-block;width:2px;height:1em;margin-left:3px;background:var(--po-primary);vertical-align:-.12em;animation:po-typing-cursor .8s steps(1,end) infinite}@keyframes po-ai-spin{to{transform:rotate(360deg)}}@keyframes po-typing-cursor{50%{opacity:0}}.po-message code{border-radius:5px;background:#eaf0ff;color:var(--po-primary-dark);padding:1px 5px}.po-message pre{max-width:100%;margin:8px 0;overflow:auto;border:1px solid var(--po-border);border-radius:6px;background:#f8fafc;padding:10px 12px;white-space:pre}.po-message pre code{border-radius:0;background:transparent;color:#1f2937;padding:0}.po-message h2,.po-message h3,.po-message h4,.po-message p{margin:8px 0}.po-message table{width:100%;margin:12px 0;border-collapse:collapse;border-radius:10px;font-size:14px}.po-message th,.po-message td{border:1px solid var(--po-border);padding:8px 10px;text-align:left;vertical-align:top}.po-message th{background:#eef5ff;color:#334155;font-weight:800}.po-composer{display:grid;grid-template-columns:minmax(0,1fr) auto;align-items:end;gap:10px;border-top:1px solid var(--po-soft-border);padding-top:12px}.po-composer textarea{min-height:88px;resize:vertical;padding:10px}.po-composer button{min-height:44px;padding:0 16px}.po-composer .po-stop{display:grid;width:44px;height:44px;min-height:44px;place-items:center;border:1px solid #fecaca;background:#fff;color:#dc2626;padding:0}.po-stop span{width:12px;height:12px;border-radius:2px;background:currentcolor}.po-trace{display:flex;min-height:0;flex-direction:column;border-left:1px solid var(--po-soft-border);background:#fafbff}.po-trace header{padding:18px}.po-trace header button{width:32px;height:32px;border:1px solid #cbd5e1;border-radius:10px;background:#fff;color:#334155;font-size:20px;line-height:1}.po-trace section{display:grid;align-content:start;gap:12px;min-height:0;overflow:auto;padding:16px}.po-steps{display:grid;gap:12px}.po-source-uploader{display:grid;gap:8px;padding-bottom:4px}.po-source-uploader p{margin:0;color:var(--po-muted);font-size:12px;line-height:1.5}.po-upload-button{display:inline-flex;justify-content:center;border:1px solid #bfdbfe;border-radius:6px;background:#eff6ff;color:var(--po-primary-dark);padding:8px 10px;font-weight:750;cursor:pointer}.po-source-reference{display:grid;grid-template-columns:1fr 1fr;overflow:hidden;border:1px solid #cbd5e1;border-radius:6px;background:#fff}.po-source-reference label{display:flex;align-items:center;justify-content:center;gap:6px;min-height:32px;color:#475569;font-size:12px;font-weight:700;cursor:pointer}.po-source-reference label+label{border-left:1px solid #cbd5e1}.po-source-reference label:has(input:checked){background:#eef2ff;color:var(--po-primary-dark)}.po-source-reference input{accent-color:var(--po-primary)}.po-source-reference-value,.po-source-remark{box-sizing:border-box;width:100%;border:1px solid #cbd5e1;border-radius:6px;background:#fff;color:var(--po-text);padding:8px 9px;font:inherit;font-size:12px;outline:none}.po-source-reference-value:focus,.po-source-remark:focus{border-color:var(--po-primary);box-shadow:0 0 0 2px #4f46e51f}.po-source-remark{min-height:54px;resize:vertical}.po-upload-button input{display:none}.po-upload-status{color:#166534!important}.po-upload-progress{height:7px;overflow:hidden;border-radius:999px;background:#dbeafe}.po-upload-progress span{display:block;height:100%;border-radius:inherit;background:linear-gradient(90deg,#2563eb,#14b8a6);transition:width .2s ease}.po-upload-empty{color:#92400e!important}.po-source-selector{display:grid;gap:5px;color:var(--po-muted);font-size:12px;font-weight:700}.po-source-selector select{width:100%;border:1px solid #cbd5e1;border-radius:6px;background:#fff;color:var(--po-text);padding:7px 8px;font-size:12px;outline:none}.po-source-selector select:focus{border-color:var(--po-primary);box-shadow:0 0 0 2px #4f46e51f}.po-source-selector select:disabled{cursor:not-allowed;opacity:.62}.po-source-list{display:grid;gap:6px}.po-source-item{display:grid;gap:3px;border:1px solid #dbeafe;border-radius:6px;background:#f8fbff;padding:8px}.po-source-item.selected{border-color:#93c5fd;background:#eff6ff;box-shadow:0 0 0 1px #3b82f61f}.po-source-item strong{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.po-source-item span{color:var(--po-muted);font-size:12px}.po-composer textarea:disabled,.po-composer button:disabled{cursor:not-allowed;opacity:.62}.po-upload-error{color:#b91c1c!important}.po-step{border:1px solid var(--po-soft-border);border-radius:8px;background:#fff;padding:12px}.po-step.running{border-color:#bfdbfe;background:#eff6ff}.po-step.completed{border-color:#bbf7d0}.po-step.failed{border-color:#fecaca;background:#fff1f2}.po-step.cancelled{border-color:#cbd5e1;background:#f8fafc}.po-step summary{display:grid;grid-template-columns:9px minmax(0,1fr) auto;align-items:start;gap:9px;cursor:pointer}.po-step summary::marker{display:none;content:""}.po-step[data-static] summary{cursor:default}.po-step-dot{width:8px;height:8px;margin-top:5px;border:2px solid #94a3b8;border-radius:50%;background:#fff}.po-step.running .po-step-dot{border-color:#2563eb;animation:po-step-pulse 1.2s ease-in-out infinite}.po-step.completed .po-step-dot{border-color:#16a34a;background:#16a34a}.po-step.failed .po-step-dot{border-color:#dc2626;background:#dc2626}.po-step-copy{display:grid;min-width:0;gap:4px}.po-step-copy strong{font-size:13px}.po-step-copy small{color:var(--po-muted);font-size:12px;font-weight:500;line-height:1.45}.po-step-status{color:var(--po-muted);font-size:11px;white-space:nowrap}.po-step-metrics{display:flex;flex-wrap:wrap;gap:6px;margin:10px 0 0}.po-step-metrics span{border:1px solid #dbeafe;border-radius:4px;background:#f8fbff;color:#475569;padding:2px 6px;font-size:11px}.po-step-details{display:grid;gap:8px;margin:10px 0 0 18px;border-top:1px solid var(--po-soft-border);padding-top:9px}.po-step-details div{display:grid;gap:3px}.po-step-details span{color:var(--po-muted);font-size:11px}.po-step-details p{overflow-wrap:anywhere;margin:0;color:#334155;font-size:12px;line-height:1.5;white-space:pre-wrap}.po-trace-state{display:flex;align-items:center;gap:8px;border:1px solid var(--po-soft-border);border-radius:8px;background:#fff;color:var(--po-muted);padding:12px;font-size:12px}.po-trace-state.failed{border-color:#fecaca;color:#b91c1c}@keyframes po-step-pulse{50%{opacity:.35}}@media(max-width:900px){.po-panel{inset:12px;grid-template-columns:1fr;overflow:auto}.po-sidebar,.po-trace{min-height:220px;border:0}.po-chat{min-height:520px}.po-chat header{align-items:flex-start;flex-wrap:wrap}.po-audience{width:100%;justify-content:space-between}.po-message{max-width:94%}}@media(max-width:600px){.po-fab{right:12px}}@media(prefers-reduced-motion:reduce){.po-ai-spinner,.po-step-dot,.po-typing-cursor{animation:none}}
2
+ .po-thinking-process {
3
+ margin: 4px 0 2px;
4
+ border: 1px solid #d8e0ec;
5
+ border-radius: 6px;
6
+ background: #f7f9fc;
7
+ color: #334155;
8
+ }
9
+
10
+ .po-thinking-process > summary {
11
+ display: flex;
12
+ align-items: center;
13
+ justify-content: space-between;
14
+ gap: 12px;
15
+ min-height: 34px;
16
+ padding: 0 10px;
17
+ cursor: pointer;
18
+ font-size: 12px;
19
+ }
20
+
21
+ .po-thinking-process > summary strong {
22
+ font-size: 12px;
23
+ font-weight: 700;
24
+ }
25
+
26
+ .po-thinking-process > summary span {
27
+ color: #7b8798;
28
+ font-size: 11px;
29
+ }
30
+
31
+ .po-thinking-content {
32
+ display: grid;
33
+ gap: 8px;
34
+ padding: 9px;
35
+ border-top: 1px solid #e2e8f0;
36
+ }
37
+
38
+ .po-thinking-content .po-step {
39
+ margin: 0;
40
+ border-radius: 6px;
41
+ background: #fff;
42
+ }
43
+
44
+ .po-message-time {
45
+ color: inherit;
46
+ font-size: 11px;
47
+ font-variant-numeric: tabular-nums;
48
+ line-height: 1.3;
49
+ opacity: .62;
50
+ }
51
+
52
+ .po-message.user > div {
53
+ margin-top: 1px;
54
+ }
@@ -57,7 +57,7 @@ class K {
57
57
  streamQuestion(e, t, s, r) {
58
58
  return fetch(`${this.base()}/question/stream`, {
59
59
  method: "POST",
60
- credentials: "include",
60
+ credentials: "omit",
61
61
  headers: { "content-type": "application/json", ...this.ownerHeaders() },
62
62
  body: JSON.stringify({ app_id: this.options.appId, thread_id: e, question: t, user_role: s }),
63
63
  signal: r
@@ -65,7 +65,7 @@ class K {
65
65
  }
66
66
  async request(e, t) {
67
67
  var c, p;
68
- const s = (t == null ? void 0 : t.body) instanceof FormData ? t.headers : { "content-type": "application/json", ...(c = t == null ? void 0 : t.headers) != null ? c : {} }, r = await fetch(`${this.base()}${e}`, { ...t, credentials: "include", headers: s });
68
+ const s = (t == null ? void 0 : t.body) instanceof FormData ? t.headers : { "content-type": "application/json", ...(c = t == null ? void 0 : t.headers) != null ? c : {} }, r = await fetch(`${this.base()}${e}`, { ...t, credentials: "omit", headers: s });
69
69
  if (r.status === 204) return;
70
70
  const n = await r.json().catch(() => null);
71
71
  if (!r.ok) throw new Error((p = n == null ? void 0 : n.message) != null ? p : r.status === 413 ? "源码包超过网关上传大小限制,请压缩代码包或联系管理员调整上传限制。" : "PageOps 请求失败");
@@ -205,6 +205,7 @@ class V {
205
205
  i(this, "sourceLoadError", "");
206
206
  i(this, "sourceUploading", !1);
207
207
  i(this, "sourceRefreshTimer", null);
208
+ i(this, "developerAccess", !1);
208
209
  i(this, "streaming", "");
209
210
  i(this, "asking", !1);
210
211
  i(this, "waitingForFirstContent", !1);
@@ -217,6 +218,7 @@ class V {
217
218
  i(this, "deletingThreadId", "");
218
219
  i(this, "streamFailure", "");
219
220
  i(this, "streamDone", null);
221
+ i(this, "activeQuestionStartedAt", "");
220
222
  i(this, "answerAudience", "developer");
221
223
  i(this, "floatingRatio", 0.5);
222
224
  i(this, "floatingCleanup", null);
@@ -230,10 +232,10 @@ class V {
230
232
  }
231
233
  unmount() {
232
234
  var e;
233
- this.cleanupFloatingLauncher(), this.stopSourceRefresh(), this.cancelActiveAnswer(!1), this.resetAnswerState(), (e = this.root) == null || e.remove(), this.root = null, this.panel = null, this.context = null, this.contextKey = "", this.selectedPageKey = "", this.currentThread = null, this.conversations = [], this.messages = [], this.steps = [], this.selectedAgentRunId = "", this.runLoading = !1, this.runLoadError = "", this.clearCopyFeedback(), this.sourceSnapshots = [], this.selectedSourceSnapshotId = null, this.sourceUploadStatus = "", this.sourceUploadProgress = null, this.sourceUploadError = "", this.sourceLoadError = "", this.sourceUploading = !1, this.answerAudience = "developer";
235
+ this.cleanupFloatingLauncher(), this.stopSourceRefresh(), this.cancelActiveAnswer(!1), this.resetAnswerState(), (e = this.root) == null || e.remove(), this.root = null, this.panel = null, this.context = null, this.contextKey = "", this.selectedPageKey = "", this.currentThread = null, this.conversations = [], this.messages = [], this.steps = [], this.selectedAgentRunId = "", this.runLoading = !1, this.runLoadError = "", this.clearCopyFeedback(), this.sourceSnapshots = [], this.selectedSourceSnapshotId = null, this.sourceUploadStatus = "", this.sourceUploadProgress = null, this.sourceUploadError = "", this.sourceLoadError = "", this.sourceUploading = !1, this.developerAccess = !1, this.activeQuestionStartedAt = "", this.answerAudience = "developer";
234
236
  }
235
237
  open() {
236
- this.root && (this.cleanupFloatingLauncher(), this.root.innerHTML = "", this.panel = document.createElement("div"), this.panel.className = "po-panel", this.root.appendChild(this.panel), this.loadSources(), this.loadConversations(), this.renderPanel());
238
+ this.root && (this.cleanupFloatingLauncher(), this.root.innerHTML = "", this.panel = document.createElement("div"), this.panel.className = "po-panel", this.root.appendChild(this.panel), this.loadDeveloperAccess(), this.loadSources(), this.loadConversations(), this.renderPanel());
237
239
  }
238
240
  close() {
239
241
  this.stopSourceRefresh(), this.cancelActiveAnswer(!1), this.renderClosed();
@@ -243,7 +245,7 @@ class V {
243
245
  const t = this.options.appId, s = String(this.options.ownerId || "").trim();
244
246
  this.options = { ...this.options, ...e }, this.client = new K(this.options);
245
247
  const r = String(this.options.ownerId || "").trim(), n = this.options.appId !== t;
246
- (n || r !== s) && (n && (this.answerAudience = this.loadAnswerAudience(this.options.appId), this.selectedSourceSnapshotId = null), this.currentThread = null, this.selectedPageKey = "", this.conversations = [], this.messages = [], this.steps = [], this.selectedAgentRunId = "", this.runLoadError = ""), this.context = null, this.contextKey = "", this.panel && this.loadConversations(), this.renderPanel();
248
+ (n || r !== s) && (n && (this.answerAudience = this.loadAnswerAudience(this.options.appId), this.selectedSourceSnapshotId = null), this.currentThread = null, this.selectedPageKey = "", this.conversations = [], this.messages = [], this.steps = [], this.selectedAgentRunId = "", this.runLoadError = ""), this.context = null, this.contextKey = "", r !== s && this.loadDeveloperAccess(), this.panel && this.loadConversations(), this.renderPanel();
247
249
  }
248
250
  renderClosed() {
249
251
  var e;
@@ -300,6 +302,19 @@ class V {
300
302
  cleanupFloatingLauncher() {
301
303
  this.floatingCleanup && (this.floatingCleanup(), this.floatingCleanup = null), this.root && this.root.style.removeProperty("--pageops-fab-top");
302
304
  }
305
+ async loadDeveloperAccess() {
306
+ var e, t;
307
+ this.developerAccess = !1, this.renderPanel();
308
+ const s = String(((e = this.options) == null ? void 0 : e.developerAccessUrl) || "").trim();
309
+ if (!s) return;
310
+ try {
311
+ const r = await fetch(s, { credentials: "same-origin", headers: { Accept: "application/json" } }), n = await r.json().catch(() => null);
312
+ this.developerAccess = !!(r.ok && (n == null ? void 0 : n.code) === 1e4 && ((t = n == null ? void 0 : n.data) == null ? void 0 : t.developer) === !0);
313
+ } catch (r) {
314
+ this.developerAccess = !1;
315
+ }
316
+ this.renderPanel();
317
+ }
303
318
  async ensureContext() {
304
319
  if (!this.client || !this.options) throw new Error("PageOps SDK 未初始化");
305
320
  const e = this.getPageContext(), t = this.getPageContextKey(e);
@@ -377,11 +392,11 @@ class V {
377
392
  async loadConversations() {
378
393
  this.client && (this.conversations = await this.client.listConversations(), this.renderPanel());
379
394
  }
380
- async newConversation() {
395
+ async newConversation(e = "") {
381
396
  var s;
382
397
  if (!this.client) return;
383
- const e = await this.ensureContext(), t = (s = e.menu_path) != null && s.length ? e.menu_path.join(" / ") : e.route || document.title || "新的问答";
384
- this.currentThread = await this.client.createConversation(e.id, t), this.selectedPageKey = this.contextKey, this.conversations = [this.currentThread, ...this.conversations], this.messages = [], this.steps = [], this.streaming = "", this.answerError = "", this.answerStopped = !1, this.renderPanel();
398
+ const t = await this.ensureContext(), r = e ? conversationTitle(t, e) : (s = t.menu_path) != null && s.length ? t.menu_path.join(" / ") : t.route || document.title || "新的问答";
399
+ this.currentThread = await this.client.createConversation(t.id, r), this.selectedPageKey = this.contextKey, this.conversations = [this.currentThread, ...this.conversations], this.messages = [], this.steps = [], this.streaming = "", this.answerError = "", this.answerStopped = !1, this.renderPanel();
385
400
  }
386
401
  async selectThread(e) {
387
402
  if (!this.client) return;
@@ -428,13 +443,13 @@ class V {
428
443
  this.sourceUploadError = t, this.renderPanel();
429
444
  return;
430
445
  }
431
- this.resetAnswerState();
446
+ this.resetAnswerState(), this.activeQuestionStartedAt = (/* @__PURE__ */ new Date()).toISOString();
432
447
  const s = ++this.activeRunId, r = new AbortController();
433
448
  this.abortController = r, this.stoppedRunId = -1, this.asking = !0, this.waitingForFirstContent = !0, this.renderPanel();
434
449
  try {
435
450
  const n = this.getPageContextKey(this.getPageContext());
436
- if (this.currentThread && this.selectedPageKey && this.selectedPageKey !== n && (this.currentThread = null), this.currentThread || await this.newConversation(), !this.currentThread) throw new Error("无法创建问答会话,请稍后重试。");
437
- this.messages.push({ id: `local-${Date.now()}`, role: "user", content: e, metadata_json: {} }), this.steps = [], this.selectedAgentRunId = "", this.runLoadError = "", this.renderPanel();
451
+ if (this.currentThread && this.selectedPageKey && this.selectedPageKey !== n && (this.currentThread = null), this.currentThread || await this.newConversation(e), !this.currentThread) throw new Error("无法创建问答会话,请稍后重试。");
452
+ this.messages.push({ id: `local-${Date.now()}`, role: "user", content: e, metadata_json: {}, created_at: this.activeQuestionStartedAt }), this.steps = [], this.selectedAgentRunId = "", this.runLoadError = "", this.renderPanel();
438
453
  const c = await this.client.streamQuestion(this.currentThread.id, e, this.answerAudience, r.signal);
439
454
  if (!c.ok) throw new Error(await z(c, "服务暂时无法回答,请稍后重试。"));
440
455
  if (!c.body) throw new Error("服务未返回有效的流式响应,请稍后重试。");
@@ -581,7 +596,7 @@ class V {
581
596
  }).join("")}</div>
582
597
  </aside>
583
598
  <main class="po-chat">
584
- <header><div class="po-chat-title"><p>问数回答</p><h2>${this.currentThread ? l(this.currentThread.title) : "未选择问答"}</h2></div><div class="po-audience"><span>回答角色</span><div class="po-audience-options" role="group" aria-label="回答角色">${m}</div></div></header>
599
+ <header><div class="po-chat-title"><p>问数回答</p><h2>${this.currentThread ? l(this.currentThread.title) : "输入问题即可开始"}</h2></div><div class="po-audience"><span>回答角色</span><div class="po-audience-options" role="group" aria-label="回答角色">${m}</div></div></header>
585
600
  <section class="po-messages">
586
601
  ${this.messages.map((o) => this.renderMessage(o)).join("")}
587
602
  ${this.renderAnswerState()}
@@ -602,7 +617,7 @@ class V {
602
617
  }
603
618
  }
604
619
  renderAnswerState() {
605
- const e = this.asking && this.waitingForFirstContent ? '<article class="po-message assistant waiting" role="status" aria-live="polite"><b>PageOps 分析</b><div class="po-ai-waiting"><span class="po-ai-spinner" aria-hidden="true"></span><span>正在理解问题并检索证据</span></div></article>' : "", t = this.streaming ? `<article class="po-message assistant streaming"><b>PageOps 分析</b><div><span data-streaming-content>${R(this.streaming)}</span>${this.asking ? '<span class="po-typing-cursor" aria-hidden="true"></span>' : ""}</div></article>` : "", s = this.answerError ? `<article class="po-message assistant answer-error" role="alert"><b>PageOps 分析</b><strong>本次回答失败</strong><div>${l(this.answerError)}</div></article>` : "", r = this.answerStopped ? '<div class="po-stream-stopped" role="status">已停止输出</div>' : "";
620
+ const n = messageTime(this.activeQuestionStartedAt), c = this.renderThinkingProcess(), e = this.asking && this.waitingForFirstContent ? `<article class="po-message assistant waiting" role="status" aria-live="polite">${n}<b>PageOps 分析</b><div class="po-ai-waiting"><span class="po-ai-spinner" aria-hidden="true"></span><span>正在理解问题并检索证据</span></div>${c}</article>` : "", t = this.streaming ? `<article class="po-message assistant streaming">${n}<b>PageOps 分析</b><div><span data-streaming-content>${R(this.streaming)}</span>${this.asking ? '<span class="po-typing-cursor" aria-hidden="true"></span>' : ""}</div>${c}</article>` : "", s = this.answerError ? `<article class="po-message assistant answer-error" role="alert">${n}<b>PageOps 分析</b><strong>本次回答失败</strong><div>${l(this.answerError)}</div></article>` : "", r = this.answerStopped ? '<div class="po-stream-stopped" role="status">已停止输出</div>' : "";
606
621
  return `${e}${t}${r}${s}`;
607
622
  }
608
623
  renderMessage(e) {
@@ -610,14 +625,15 @@ class V {
610
625
  <span class="po-run-id" title="${l(t)}">问答流水号 <code>${l(t)}</code></span>
611
626
  <button type="button" data-copy-run="${l(t)}">${r ? "已复制" : n ? "复制失败" : "复制流水号"}</button>
612
627
  <button type="button" data-view-run="${l(t)}" aria-pressed="${s}">${s ? "正在查看" : "查看执行过程"}</button>
613
- </footer>` : "";
628
+ </footer>` : "", p = messageTime(e.created_at), m = e.role === "user" ? "" : "<b>PageOps 分析</b>", g = e.role === "assistant" && s ? this.renderThinkingProcess() : "";
614
629
  return `<article class="po-message ${e.role} ${s ? "run-selected" : ""}" data-message="${l(e.id)}">
615
- <b>${e.role === "user" ? "你" : "PageOps 分析"}</b>
630
+ ${p}${m}
616
631
  <div>${e.role === "assistant" ? R(e.content) : l(e.content)}</div>
617
- ${c}
632
+ ${g}${c}
618
633
  </article>`;
619
634
  }
620
635
  renderSourceUploader() {
636
+ if (!this.developerAccess) return "";
621
637
  const e = this.sourceLoadError ? "暂时无法读取项目共享代码包,连接恢复后将自动重试。" : "当前项目暂无代码包,请先上传后再提问。";
622
638
  const t = typeof this.sourceUploadProgress == "number" ? Math.max(0, Math.min(100, Math.round(this.sourceUploadProgress))) : 0;
623
639
  return `
@@ -666,10 +682,15 @@ class V {
666
682
  </details>`;
667
683
  }).join("");
668
684
  }
685
+ renderThinkingProcess() {
686
+ if (!this.asking && !this.steps.length && !this.runLoading && !this.runLoadError) return "";
687
+ const e = this.asking || this.steps.some((t) => t.status === "running"), t = this.asking ? "思考中" : this.runLoading ? "加载中" : this.runLoadError ? "加载失败" : "已思考完成";
688
+ return `<details class="po-thinking-process" ${e ? "open" : ""}><summary><strong>思考过程</strong><span>${t}</span></summary><div class="po-thinking-content">${this.renderSteps()}</div></details>`;
689
+ }
669
690
  renderExecutionSteps(e = !1) {
670
691
  var s, r;
671
692
  const t = (s = this.panel) == null ? void 0 : s.querySelector(".po-steps");
672
- if (t && (t.innerHTML = this.renderSteps(), e)) {
693
+ if (t && (t.innerHTML = this.renderSteps(), this.panel == null || this.panel.querySelectorAll(".po-thinking-content").forEach((n) => n.innerHTML = this.renderSteps()), e)) {
673
694
  const n = (r = this.panel) == null ? void 0 : r.querySelector(".po-trace > section");
674
695
  n && (n.scrollTop = n.scrollHeight);
675
696
  }
@@ -740,6 +761,21 @@ function M(a) {
740
761
  const e = (t = a.metadata_json) == null ? void 0 : t.agent_run_id;
741
762
  return typeof e == "number" ? String(e) : typeof e == "string" ? e : "";
742
763
  }
764
+ function conversationTitle(a, e) {
765
+ var s;
766
+ const t = (s = a.menu_path) != null && s.length ? String(a.menu_path[a.menu_path.length - 1]) : String(a.title || a.route || document.title || "新的问答").split(" - ")[0], r = String(e || "").replace(/\s+/g, " ").replace(/[??!!。]+$/, "").trim(), n = r.length > 28 ? `${r.slice(0, 28)}...` : r;
767
+ return [t.trim(), n].filter(Boolean).join(" · ");
768
+ }
769
+ function formatFullTimestamp(a) {
770
+ const e = new Date(a);
771
+ if (Number.isNaN(e.getTime())) return "";
772
+ const t = new Intl.DateTimeFormat("zh-CN", { timeZone: "Asia/Shanghai", year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", hourCycle: "h23" }).formatToParts(e).reduce((s, r) => (s[r.type] = r.value, s), {});
773
+ return `${t.year}-${t.month}-${t.day} ${t.hour}:${t.minute}:${t.second}`;
774
+ }
775
+ function messageTime(a) {
776
+ const e = formatFullTimestamp(a);
777
+ return e ? `<time class="po-message-time" datetime="${l(a)}">${e}</time>` : "";
778
+ }
743
779
  function O(a) {
744
780
  return new Intl.DateTimeFormat("zh-CN", {
745
781
  timeZone: "Asia/Shanghai",
@@ -1,5 +1,5 @@
1
1
  (function(g,f){typeof exports==='object'&&typeof module!=='undefined'?f(exports):typeof define==='function'&&define.amd?define(['exports'],f):(g=typeof globalThis!=='undefined'?globalThis:g||self,f(g.PageOpsSDK={}));})(this,(function(exports){'use strict';var W = Object.defineProperty;
2
- var H = (a, e, t) => e in a ? W(a, e, { enumerable: true, configurable: true, writable: true, value: t }) : a[e] = t;
2
+ var H = (a, e, t) => e in a ? W(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t;
3
3
  var i = (a, e, t) => H(a, typeof e != "symbol" ? e + "" : e, t);
4
4
  const UPLOAD_MAX_BYTES = 500 * 1024 * 1024;
5
5
  class K {
@@ -57,7 +57,7 @@ class K {
57
57
  streamQuestion(e, t, s, r) {
58
58
  return fetch(`${this.base()}/question/stream`, {
59
59
  method: "POST",
60
- credentials: "include",
60
+ credentials: "omit",
61
61
  headers: { "content-type": "application/json", ...this.ownerHeaders() },
62
62
  body: JSON.stringify({ app_id: this.options.appId, thread_id: e, question: t, user_role: s }),
63
63
  signal: r
@@ -65,7 +65,7 @@ class K {
65
65
  }
66
66
  async request(e, t) {
67
67
  var c, p;
68
- const s = (t == null ? void 0 : t.body) instanceof FormData ? t.headers : { "content-type": "application/json", ...(c = t == null ? void 0 : t.headers) != null ? c : {} }, r = await fetch(`${this.base()}${e}`, { ...t, credentials: "include", headers: s });
68
+ const s = (t == null ? void 0 : t.body) instanceof FormData ? t.headers : { "content-type": "application/json", ...(c = t == null ? void 0 : t.headers) != null ? c : {} }, r = await fetch(`${this.base()}${e}`, { ...t, credentials: "omit", headers: s });
69
69
  if (r.status === 204) return;
70
70
  const n = await r.json().catch(() => null);
71
71
  if (!r.ok) throw new Error((p = n == null ? void 0 : n.message) != null ? p : r.status === 413 ? "源码包超过网关上传大小限制,请压缩代码包或联系管理员调整上传限制。" : "PageOps 请求失败");
@@ -136,7 +136,7 @@ async function J(a, e) {
136
136
  r += s.decode();
137
137
  break;
138
138
  }
139
- r += s.decode(p, { stream: true });
139
+ r += s.decode(p, { stream: !0 });
140
140
  const m = r.split(/\r?\n\r?\n/);
141
141
  r = (n = m.pop()) != null ? n : "";
142
142
  for (const g of m) j(g, e);
@@ -193,7 +193,7 @@ class V {
193
193
  i(this, "messages", []);
194
194
  i(this, "steps", []);
195
195
  i(this, "selectedAgentRunId", "");
196
- i(this, "runLoading", false);
196
+ i(this, "runLoading", !1);
197
197
  i(this, "runLoadError", "");
198
198
  i(this, "copiedAgentRunId", "");
199
199
  i(this, "copyFeedbackTimer", null);
@@ -203,20 +203,22 @@ class V {
203
203
  i(this, "sourceUploadProgress", null);
204
204
  i(this, "sourceUploadError", "");
205
205
  i(this, "sourceLoadError", "");
206
- i(this, "sourceUploading", false);
206
+ i(this, "sourceUploading", !1);
207
207
  i(this, "sourceRefreshTimer", null);
208
+ i(this, "developerAccess", !1);
208
209
  i(this, "streaming", "");
209
- i(this, "asking", false);
210
- i(this, "waitingForFirstContent", false);
210
+ i(this, "asking", !1);
211
+ i(this, "waitingForFirstContent", !1);
211
212
  i(this, "abortController", null);
212
213
  i(this, "activeRunId", 0);
213
214
  i(this, "stoppedRunId", -1);
214
- i(this, "answerStopped", false);
215
+ i(this, "answerStopped", !1);
215
216
  i(this, "answerError", "");
216
217
  i(this, "conversationError", "");
217
218
  i(this, "deletingThreadId", "");
218
219
  i(this, "streamFailure", "");
219
220
  i(this, "streamDone", null);
221
+ i(this, "activeQuestionStartedAt", "");
220
222
  i(this, "answerAudience", "developer");
221
223
  i(this, "floatingRatio", 0.5);
222
224
  i(this, "floatingCleanup", null);
@@ -230,20 +232,20 @@ class V {
230
232
  }
231
233
  unmount() {
232
234
  var e;
233
- this.cleanupFloatingLauncher(), this.stopSourceRefresh(), this.cancelActiveAnswer(false), this.resetAnswerState(), (e = this.root) == null || e.remove(), this.root = null, this.panel = null, this.context = null, this.contextKey = "", this.selectedPageKey = "", this.currentThread = null, this.conversations = [], this.messages = [], this.steps = [], this.selectedAgentRunId = "", this.runLoading = false, this.runLoadError = "", this.clearCopyFeedback(), this.sourceSnapshots = [], this.selectedSourceSnapshotId = null, this.sourceUploadStatus = "", this.sourceUploadProgress = null, this.sourceUploadError = "", this.sourceLoadError = "", this.sourceUploading = false, this.answerAudience = "developer";
235
+ this.cleanupFloatingLauncher(), this.stopSourceRefresh(), this.cancelActiveAnswer(!1), this.resetAnswerState(), (e = this.root) == null || e.remove(), this.root = null, this.panel = null, this.context = null, this.contextKey = "", this.selectedPageKey = "", this.currentThread = null, this.conversations = [], this.messages = [], this.steps = [], this.selectedAgentRunId = "", this.runLoading = !1, this.runLoadError = "", this.clearCopyFeedback(), this.sourceSnapshots = [], this.selectedSourceSnapshotId = null, this.sourceUploadStatus = "", this.sourceUploadProgress = null, this.sourceUploadError = "", this.sourceLoadError = "", this.sourceUploading = !1, this.developerAccess = !1, this.activeQuestionStartedAt = "", this.answerAudience = "developer";
234
236
  }
235
237
  open() {
236
- this.root && (this.cleanupFloatingLauncher(), this.root.innerHTML = "", this.panel = document.createElement("div"), this.panel.className = "po-panel", this.root.appendChild(this.panel), this.loadSources(), this.loadConversations(), this.renderPanel());
238
+ this.root && (this.cleanupFloatingLauncher(), this.root.innerHTML = "", this.panel = document.createElement("div"), this.panel.className = "po-panel", this.root.appendChild(this.panel), this.loadDeveloperAccess(), this.loadSources(), this.loadConversations(), this.renderPanel());
237
239
  }
238
240
  close() {
239
- this.stopSourceRefresh(), this.cancelActiveAnswer(false), this.renderClosed();
241
+ this.stopSourceRefresh(), this.cancelActiveAnswer(!1), this.renderClosed();
240
242
  }
241
243
  updateContext(e) {
242
244
  if (!this.options) return;
243
245
  const t = this.options.appId, s = String(this.options.ownerId || "").trim();
244
246
  this.options = { ...this.options, ...e }, this.client = new K(this.options);
245
247
  const r = String(this.options.ownerId || "").trim(), n = this.options.appId !== t;
246
- (n || r !== s) && (n && (this.answerAudience = this.loadAnswerAudience(this.options.appId), this.selectedSourceSnapshotId = null), this.currentThread = null, this.selectedPageKey = "", this.conversations = [], this.messages = [], this.steps = [], this.selectedAgentRunId = "", this.runLoadError = ""), this.context = null, this.contextKey = "", this.panel && this.loadConversations(), this.renderPanel();
248
+ (n || r !== s) && (n && (this.answerAudience = this.loadAnswerAudience(this.options.appId), this.selectedSourceSnapshotId = null), this.currentThread = null, this.selectedPageKey = "", this.conversations = [], this.messages = [], this.steps = [], this.selectedAgentRunId = "", this.runLoadError = ""), this.context = null, this.contextKey = "", r !== s && this.loadDeveloperAccess(), this.panel && this.loadConversations(), this.renderPanel();
247
249
  }
248
250
  renderClosed() {
249
251
  var e;
@@ -276,30 +278,43 @@ class V {
276
278
  }
277
279
  bindFloatingLauncher(e) {
278
280
  this.cleanupFloatingLauncher(), this.floatingRatio = this.loadFloatingRatio(), this.setFloatingPosition(e, window.innerHeight * this.floatingRatio);
279
- let t = null, s = false;
281
+ let t = null, s = !1;
280
282
  const r = (o) => {
281
283
  if (!t || o.pointerId !== t.pointerId) return;
282
284
  s = t.moved, e.classList.remove("po-fab-dragging"), e.hasPointerCapture && e.hasPointerCapture(o.pointerId) && e.releasePointerCapture(o.pointerId), t.moved && this.saveFloatingRatio(), t = null;
283
285
  }, n = (o) => {
284
286
  if (typeof o.button == "number" && o.button !== 0) return;
285
287
  const l = e.getBoundingClientRect();
286
- t = { pointerId: o.pointerId, startClientY: o.clientY, startCenterY: l.top + l.height / 2, moved: false }, e.setPointerCapture && e.setPointerCapture(o.pointerId), e.classList.add("po-fab-dragging");
288
+ t = { pointerId: o.pointerId, startClientY: o.clientY, startCenterY: l.top + l.height / 2, moved: !1 }, e.setPointerCapture && e.setPointerCapture(o.pointerId), e.classList.add("po-fab-dragging");
287
289
  }, c = (o) => {
288
290
  if (!t || o.pointerId !== t.pointerId) return;
289
291
  const l = o.clientY - t.startClientY;
290
- !t.moved && Math.abs(l) >= 4 && (t.moved = true), t.moved && (o.preventDefault(), this.setFloatingPosition(e, t.startCenterY + l));
292
+ !t.moved && Math.abs(l) >= 4 && (t.moved = !0), t.moved && (o.preventDefault(), this.setFloatingPosition(e, t.startCenterY + l));
291
293
  }, p = (o) => {
292
- s && (s = false, o.preventDefault(), o.stopImmediatePropagation());
294
+ s && (s = !1, o.preventDefault(), o.stopImmediatePropagation());
293
295
  }, m = () => {
294
296
  e.isConnected && this.setFloatingPosition(e, window.innerHeight * this.floatingRatio);
295
297
  };
296
- e.addEventListener("pointerdown", n), e.addEventListener("pointermove", c), e.addEventListener("pointerup", r), e.addEventListener("pointercancel", r), e.addEventListener("click", p, true), window.addEventListener("resize", m), this.floatingCleanup = () => {
297
- e.removeEventListener("pointerdown", n), e.removeEventListener("pointermove", c), e.removeEventListener("pointerup", r), e.removeEventListener("pointercancel", r), e.removeEventListener("click", p, true), window.removeEventListener("resize", m);
298
+ e.addEventListener("pointerdown", n), e.addEventListener("pointermove", c), e.addEventListener("pointerup", r), e.addEventListener("pointercancel", r), e.addEventListener("click", p, !0), window.addEventListener("resize", m), this.floatingCleanup = () => {
299
+ e.removeEventListener("pointerdown", n), e.removeEventListener("pointermove", c), e.removeEventListener("pointerup", r), e.removeEventListener("pointercancel", r), e.removeEventListener("click", p, !0), window.removeEventListener("resize", m);
298
300
  };
299
301
  }
300
302
  cleanupFloatingLauncher() {
301
303
  this.floatingCleanup && (this.floatingCleanup(), this.floatingCleanup = null), this.root && this.root.style.removeProperty("--pageops-fab-top");
302
304
  }
305
+ async loadDeveloperAccess() {
306
+ var e, t;
307
+ this.developerAccess = !1, this.renderPanel();
308
+ const s = String(((e = this.options) == null ? void 0 : e.developerAccessUrl) || "").trim();
309
+ if (!s) return;
310
+ try {
311
+ const r = await fetch(s, { credentials: "same-origin", headers: { Accept: "application/json" } }), n = await r.json().catch(() => null);
312
+ this.developerAccess = !!(r.ok && (n == null ? void 0 : n.code) === 1e4 && ((t = n == null ? void 0 : n.data) == null ? void 0 : t.developer) === !0);
313
+ } catch (r) {
314
+ this.developerAccess = !1;
315
+ }
316
+ this.renderPanel();
317
+ }
303
318
  async ensureContext() {
304
319
  if (!this.client || !this.options) throw new Error("PageOps SDK 未初始化");
305
320
  const e = this.getPageContext(), t = this.getPageContextKey(e);
@@ -352,7 +367,7 @@ class V {
352
367
  if (this.asking) return;
353
368
  const s = e === "" ? null : e;
354
369
  if ((this.selectedSourceSnapshotId == null ? "" : String(this.selectedSourceSnapshotId)) === (s == null ? "" : String(s))) return;
355
- this.selectedSourceSnapshotId = s, this.context = null, this.contextKey = "", this.selectedPageKey = "", this.currentThread = null, this.messages = [], this.steps = [], this.selectedAgentRunId = "", this.runLoadError = "", this.streaming = "", this.answerError = "", this.answerStopped = false, this.renderPanel();
370
+ this.selectedSourceSnapshotId = s, this.context = null, this.contextKey = "", this.selectedPageKey = "", this.currentThread = null, this.messages = [], this.steps = [], this.selectedAgentRunId = "", this.runLoadError = "", this.streaming = "", this.answerError = "", this.answerStopped = !1, this.renderPanel();
356
371
  }
357
372
  sourceSnapshotValue(e) {
358
373
  var t, s;
@@ -377,15 +392,15 @@ class V {
377
392
  async loadConversations() {
378
393
  this.client && (this.conversations = await this.client.listConversations(), this.renderPanel());
379
394
  }
380
- async newConversation() {
395
+ async newConversation(e = "") {
381
396
  var s;
382
397
  if (!this.client) return;
383
- const e = await this.ensureContext(), t = (s = e.menu_path) != null && s.length ? e.menu_path.join(" / ") : e.route || document.title || "新的问答";
384
- this.currentThread = await this.client.createConversation(e.id, t), this.selectedPageKey = this.contextKey, this.conversations = [this.currentThread, ...this.conversations], this.messages = [], this.steps = [], this.streaming = "", this.answerError = "", this.answerStopped = false, this.renderPanel();
398
+ const t = await this.ensureContext(), r = e ? conversationTitle(t, e) : (s = t.menu_path) != null && s.length ? t.menu_path.join(" / ") : t.route || document.title || "新的问答";
399
+ this.currentThread = await this.client.createConversation(t.id, r), this.selectedPageKey = this.contextKey, this.conversations = [this.currentThread, ...this.conversations], this.messages = [], this.steps = [], this.streaming = "", this.answerError = "", this.answerStopped = !1, this.renderPanel();
385
400
  }
386
401
  async selectThread(e) {
387
402
  if (!this.client) return;
388
- this.currentThread = e, this.selectedPageKey = this.getPageContextKey(this.getPageContext()), this.messages = await this.client.listMessages(e.id), this.steps = [], this.selectedAgentRunId = "", this.runLoadError = "", this.streaming = "", this.answerError = "", this.answerStopped = false, this.renderPanel();
403
+ this.currentThread = e, this.selectedPageKey = this.getPageContextKey(this.getPageContext()), this.messages = await this.client.listMessages(e.id), this.steps = [], this.selectedAgentRunId = "", this.runLoadError = "", this.streaming = "", this.answerError = "", this.answerStopped = !1, this.renderPanel();
389
404
  const t = [...this.messages].reverse().map(M).find(Boolean);
390
405
  t && await this.loadAgentRun(t);
391
406
  }
@@ -409,7 +424,7 @@ class V {
409
424
  async uploadSource(e, t) {
410
425
  var s;
411
426
  if (this.client) {
412
- this.sourceUploading = true, this.sourceUploadProgress = 0, this.sourceUploadStatus = "上传中 0%", this.sourceUploadError = "", this.renderPanel();
427
+ this.sourceUploading = !0, this.sourceUploadProgress = 0, this.sourceUploadStatus = "上传中 0%", this.sourceUploadError = "", this.renderPanel();
413
428
  try {
414
429
  await this.client.uploadSource(e, { ...t, uploader: (s = this.options) == null ? void 0 : s.uploader, onProgress: (r) => {
415
430
  this.sourceUploadProgress = Math.max(0, Math.min(100, Math.round(r.percent))), this.sourceUploadStatus = `上传中 ${this.sourceUploadProgress}%`, this.renderPanel();
@@ -417,7 +432,7 @@ class V {
417
432
  } catch (r) {
418
433
  this.sourceUploadError = r instanceof Error ? r.message : "源码包上传失败", this.sourceUploadStatus = "";
419
434
  } finally {
420
- this.sourceUploading = false, this.sourceUploadProgress = null, this.renderPanel();
435
+ this.sourceUploading = !1, this.sourceUploadProgress = null, this.renderPanel();
421
436
  }
422
437
  }
423
438
  }
@@ -428,13 +443,13 @@ class V {
428
443
  this.sourceUploadError = t, this.renderPanel();
429
444
  return;
430
445
  }
431
- this.resetAnswerState();
446
+ this.resetAnswerState(), this.activeQuestionStartedAt = (/* @__PURE__ */ new Date()).toISOString();
432
447
  const s = ++this.activeRunId, r = new AbortController();
433
- this.abortController = r, this.stoppedRunId = -1, this.asking = true, this.waitingForFirstContent = true, this.renderPanel();
448
+ this.abortController = r, this.stoppedRunId = -1, this.asking = !0, this.waitingForFirstContent = !0, this.renderPanel();
434
449
  try {
435
450
  const n = this.getPageContextKey(this.getPageContext());
436
- if (this.currentThread && this.selectedPageKey && this.selectedPageKey !== n && (this.currentThread = null), this.currentThread || await this.newConversation(), !this.currentThread) throw new Error("无法创建问答会话,请稍后重试。");
437
- this.messages.push({ id: `local-${Date.now()}`, role: "user", content: e, metadata_json: {} }), this.steps = [], this.selectedAgentRunId = "", this.runLoadError = "", this.renderPanel();
451
+ if (this.currentThread && this.selectedPageKey && this.selectedPageKey !== n && (this.currentThread = null), this.currentThread || await this.newConversation(e), !this.currentThread) throw new Error("无法创建问答会话,请稍后重试。");
452
+ this.messages.push({ id: `local-${Date.now()}`, role: "user", content: e, metadata_json: {}, created_at: this.activeQuestionStartedAt }), this.steps = [], this.selectedAgentRunId = "", this.runLoadError = "", this.renderPanel();
438
453
  const c = await this.client.streamQuestion(this.currentThread.id, e, this.answerAudience, r.signal);
439
454
  if (!c.ok) throw new Error(await z(c, "服务暂时无法回答,请稍后重试。"));
440
455
  if (!c.body) throw new Error("服务未返回有效的流式响应,请稍后重试。");
@@ -445,34 +460,34 @@ class V {
445
460
  await this.selectThread(this.currentThread);
446
461
  } catch (n) {
447
462
  if (s !== this.activeRunId) return;
448
- this.typewriter.clear(), this.waitingForFirstContent = false, s === this.stoppedRunId ? this.answerStopped = true : (this.answerError = n instanceof Error ? n.message : "服务异常,请稍后重试。", this.upsertStep({ id: "error", title: "执行失败", status: "failed", summary: this.answerError }));
463
+ this.typewriter.clear(), this.waitingForFirstContent = !1, s === this.stoppedRunId ? this.answerStopped = !0 : (this.answerError = n instanceof Error ? n.message : "服务异常,请稍后重试。", this.upsertStep({ id: "error", title: "执行失败", status: "failed", summary: this.answerError }));
449
464
  } finally {
450
465
  if (s !== this.activeRunId) return;
451
- this.asking = false, this.waitingForFirstContent = false, this.abortController === r && (this.abortController = null), this.renderPanel(true);
466
+ this.asking = !1, this.waitingForFirstContent = !1, this.abortController === r && (this.abortController = null), this.renderPanel(!0);
452
467
  }
453
468
  }
454
469
  cancelActiveAnswer(e) {
455
470
  var t;
456
471
  if (!(!this.asking && !this.abortController)) {
457
472
  if (e) {
458
- this.stoppedRunId = this.activeRunId, this.answerStopped = true;
473
+ this.stoppedRunId = this.activeRunId, this.answerStopped = !0;
459
474
  const s = this.steps.map((r) => r.status === "running" ? { ...r, status: "cancelled" } : r);
460
475
  this.steps = s.some((r) => r.status === "cancelled") ? s : [...s, { id: "stopped", title: "已停止", status: "cancelled" }];
461
476
  } else
462
- this.activeRunId += 1, this.asking = false;
463
- this.typewriter.clear(), this.waitingForFirstContent = false, (t = this.abortController) == null || t.abort(), this.abortController = null, e && this.renderPanel();
477
+ this.activeRunId += 1, this.asking = !1;
478
+ this.typewriter.clear(), this.waitingForFirstContent = !1, (t = this.abortController) == null || t.abort(), this.abortController = null, e && this.renderPanel();
464
479
  }
465
480
  }
466
481
  handleStreamEvent(e, t) {
467
482
  var s, r;
468
483
  if (e === "step") {
469
- this.upsertStep(t), this.renderExecutionSteps(true);
484
+ this.upsertStep(t), this.renderExecutionSteps(!0);
470
485
  return;
471
486
  }
472
487
  if (e === "delta") {
473
488
  const n = String((s = t.content) != null ? s : "");
474
489
  if (!n) return;
475
- this.waitingForFirstContent = false, this.typewriter.enqueue(n);
490
+ this.waitingForFirstContent = !1, this.typewriter.enqueue(n);
476
491
  return;
477
492
  }
478
493
  e === "done" && (this.streamDone = t), e === "error" && (this.streamFailure = String((r = t.message) != null ? r : "服务异常,请稍后重试。"));
@@ -489,7 +504,7 @@ class V {
489
504
  t && (t.scrollTop = t.scrollHeight);
490
505
  }
491
506
  resetAnswerState() {
492
- this.typewriter.clear(), this.asking = false, this.waitingForFirstContent = false, this.answerError = "", this.answerStopped = false, this.streamFailure = "", this.streamDone = null, this.streaming = "";
507
+ this.typewriter.clear(), this.asking = !1, this.waitingForFirstContent = !1, this.answerError = "", this.answerStopped = !1, this.streamFailure = "", this.streamDone = null, this.streaming = "";
493
508
  }
494
509
  upsertStep(e) {
495
510
  const t = this.steps.findIndex((s) => s.id === e.id);
@@ -497,7 +512,7 @@ class V {
497
512
  }
498
513
  async loadAgentRun(e) {
499
514
  if (!(!this.client || !e)) {
500
- this.selectedAgentRunId = e, this.runLoading = true, this.runLoadError = "", this.renderPanel();
515
+ this.selectedAgentRunId = e, this.runLoading = !0, this.runLoadError = "", this.renderPanel();
501
516
  try {
502
517
  const t = await this.client.getAgentRun(e);
503
518
  if (this.selectedAgentRunId !== e) return;
@@ -506,7 +521,7 @@ class V {
506
521
  if (this.selectedAgentRunId !== e) return;
507
522
  this.steps = [], this.runLoadError = t instanceof Error ? t.message : "执行过程加载失败";
508
523
  } finally {
509
- this.selectedAgentRunId === e && (this.runLoading = false, this.renderPanel());
524
+ this.selectedAgentRunId === e && (this.runLoading = !1, this.renderPanel());
510
525
  }
511
526
  }
512
527
  }
@@ -525,7 +540,7 @@ class V {
525
540
  } catch (s) {
526
541
  this.copiedAgentRunId = `error:${e}`;
527
542
  }
528
- this.clearCopyFeedback(false), this.copyFeedbackTimer = setTimeout(() => {
543
+ this.clearCopyFeedback(!1), this.copyFeedbackTimer = setTimeout(() => {
529
544
  this.copiedAgentRunId = "", this.copyFeedbackTimer = null, this.updateCopyFeedback();
530
545
  }, 1800), this.updateCopyFeedback();
531
546
  }
@@ -538,7 +553,7 @@ class V {
538
553
  t.textContent = this.copiedAgentRunId === s ? "已复制" : this.copiedAgentRunId === `error:${s}` ? "复制失败" : "复制流水号";
539
554
  });
540
555
  }
541
- clearCopyFeedback(e = true) {
556
+ clearCopyFeedback(e = !0) {
542
557
  this.copyFeedbackTimer && clearTimeout(this.copyFeedbackTimer), this.copyFeedbackTimer = null, e && (this.copiedAgentRunId = "");
543
558
  }
544
559
  latestAnalysisStatus() {
@@ -557,7 +572,7 @@ class V {
557
572
  stopSourceRefresh() {
558
573
  this.sourceRefreshTimer && clearTimeout(this.sourceRefreshTimer), this.sourceRefreshTimer = null;
559
574
  }
560
- renderPanel(e = this.asking, t = false) {
575
+ renderPanel(e = this.asking, t = !1) {
561
576
  var g;
562
577
  if (!this.panel) return;
563
578
  const s = (g = this.panel.querySelector(".po-messages")) == null ? void 0 : g.scrollTop, r = this.questionUnavailableReason(), n = r || (this.asking ? "回答生成中,请稍候" : "请输入页面分析问题,Enter 发送,Shift + Enter 换行"), c = !!(r || this.asking), p = r ? '<button type="submit" disabled>暂不可提问</button>' : this.asking ? '<button class="po-stop" type="button" data-action="stop-answer" aria-label="停止输出" title="停止输出"><span aria-hidden="true"></span></button>' : '<button type="submit">发送</button>', m = C.map((o) => `
@@ -581,7 +596,7 @@ class V {
581
596
  }).join("")}</div>
582
597
  </aside>
583
598
  <main class="po-chat">
584
- <header><div class="po-chat-title"><p>问数回答</p><h2>${this.currentThread ? l(this.currentThread.title) : "未选择问答"}</h2></div><div class="po-audience"><span>回答角色</span><div class="po-audience-options" role="group" aria-label="回答角色">${m}</div></div></header>
599
+ <header><div class="po-chat-title"><p>问数回答</p><h2>${this.currentThread ? l(this.currentThread.title) : "输入问题即可开始"}</h2></div><div class="po-audience"><span>回答角色</span><div class="po-audience-options" role="group" aria-label="回答角色">${m}</div></div></header>
585
600
  <section class="po-messages">
586
601
  ${this.messages.map((o) => this.renderMessage(o)).join("")}
587
602
  ${this.renderAnswerState()}
@@ -602,7 +617,7 @@ class V {
602
617
  }
603
618
  }
604
619
  renderAnswerState() {
605
- const e = this.asking && this.waitingForFirstContent ? '<article class="po-message assistant waiting" role="status" aria-live="polite"><b>PageOps 分析</b><div class="po-ai-waiting"><span class="po-ai-spinner" aria-hidden="true"></span><span>正在理解问题并检索证据</span></div></article>' : "", t = this.streaming ? `<article class="po-message assistant streaming"><b>PageOps 分析</b><div><span data-streaming-content>${R(this.streaming)}</span>${this.asking ? '<span class="po-typing-cursor" aria-hidden="true"></span>' : ""}</div></article>` : "", s = this.answerError ? `<article class="po-message assistant answer-error" role="alert"><b>PageOps 分析</b><strong>本次回答失败</strong><div>${l(this.answerError)}</div></article>` : "", r = this.answerStopped ? '<div class="po-stream-stopped" role="status">已停止输出</div>' : "";
620
+ const n = messageTime(this.activeQuestionStartedAt), c = this.renderThinkingProcess(), e = this.asking && this.waitingForFirstContent ? `<article class="po-message assistant waiting" role="status" aria-live="polite">${n}<b>PageOps 分析</b><div class="po-ai-waiting"><span class="po-ai-spinner" aria-hidden="true"></span><span>正在理解问题并检索证据</span></div>${c}</article>` : "", t = this.streaming ? `<article class="po-message assistant streaming">${n}<b>PageOps 分析</b><div><span data-streaming-content>${R(this.streaming)}</span>${this.asking ? '<span class="po-typing-cursor" aria-hidden="true"></span>' : ""}</div>${c}</article>` : "", s = this.answerError ? `<article class="po-message assistant answer-error" role="alert">${n}<b>PageOps 分析</b><strong>本次回答失败</strong><div>${l(this.answerError)}</div></article>` : "", r = this.answerStopped ? '<div class="po-stream-stopped" role="status">已停止输出</div>' : "";
606
621
  return `${e}${t}${r}${s}`;
607
622
  }
608
623
  renderMessage(e) {
@@ -610,14 +625,15 @@ class V {
610
625
  <span class="po-run-id" title="${l(t)}">问答流水号 <code>${l(t)}</code></span>
611
626
  <button type="button" data-copy-run="${l(t)}">${r ? "已复制" : n ? "复制失败" : "复制流水号"}</button>
612
627
  <button type="button" data-view-run="${l(t)}" aria-pressed="${s}">${s ? "正在查看" : "查看执行过程"}</button>
613
- </footer>` : "";
628
+ </footer>` : "", p = messageTime(e.created_at), m = e.role === "user" ? "" : "<b>PageOps 分析</b>", g = e.role === "assistant" && s ? this.renderThinkingProcess() : "";
614
629
  return `<article class="po-message ${e.role} ${s ? "run-selected" : ""}" data-message="${l(e.id)}">
615
- <b>${e.role === "user" ? "你" : "PageOps 分析"}</b>
630
+ ${p}${m}
616
631
  <div>${e.role === "assistant" ? R(e.content) : l(e.content)}</div>
617
- ${c}
632
+ ${g}${c}
618
633
  </article>`;
619
634
  }
620
635
  renderSourceUploader() {
636
+ if (!this.developerAccess) return "";
621
637
  const e = this.sourceLoadError ? "暂时无法读取项目共享代码包,连接恢复后将自动重试。" : "当前项目暂无代码包,请先上传后再提问。";
622
638
  const t = typeof this.sourceUploadProgress == "number" ? Math.max(0, Math.min(100, Math.round(this.sourceUploadProgress))) : 0;
623
639
  return `
@@ -666,17 +682,22 @@ class V {
666
682
  </details>`;
667
683
  }).join("");
668
684
  }
669
- renderExecutionSteps(e = false) {
685
+ renderThinkingProcess() {
686
+ if (!this.asking && !this.steps.length && !this.runLoading && !this.runLoadError) return "";
687
+ const e = this.asking || this.steps.some((t) => t.status === "running"), t = this.asking ? "思考中" : this.runLoading ? "加载中" : this.runLoadError ? "加载失败" : "已思考完成";
688
+ return `<details class="po-thinking-process" ${e ? "open" : ""}><summary><strong>思考过程</strong><span>${t}</span></summary><div class="po-thinking-content">${this.renderSteps()}</div></details>`;
689
+ }
690
+ renderExecutionSteps(e = !1) {
670
691
  var s, r;
671
692
  const t = (s = this.panel) == null ? void 0 : s.querySelector(".po-steps");
672
- if (t && (t.innerHTML = this.renderSteps(), e)) {
693
+ if (t && (t.innerHTML = this.renderSteps(), this.panel == null || this.panel.querySelectorAll(".po-thinking-content").forEach((n) => n.innerHTML = this.renderSteps()), e)) {
673
694
  const n = (r = this.panel) == null ? void 0 : r.querySelector(".po-trace > section");
674
695
  n && (n.scrollTop = n.scrollHeight);
675
696
  }
676
697
  }
677
698
  bindPanelEvents() {
678
699
  var e, t, s, r, n, c, p, m, g, o, y, w, b, S, $, E, A, T, h, d;
679
- (t = (e = this.panel) == null ? void 0 : e.querySelector('[data-action="new"]')) == null || t.addEventListener("click", () => void this.newConversation()), (r = (s = this.panel) == null ? void 0 : s.querySelector('[data-action="close"]')) == null || r.addEventListener("click", () => this.close()), (c = (n = this.panel) == null ? void 0 : n.querySelector('[data-action="stop-answer"]')) == null || c.addEventListener("click", () => this.cancelActiveAnswer(true)), (p = this.panel) == null || p.querySelectorAll("[data-audience]").forEach((h) => {
700
+ (t = (e = this.panel) == null ? void 0 : e.querySelector('[data-action="new"]')) == null || t.addEventListener("click", () => void this.newConversation()), (r = (s = this.panel) == null ? void 0 : s.querySelector('[data-action="close"]')) == null || r.addEventListener("click", () => this.close()), (c = (n = this.panel) == null ? void 0 : n.querySelector('[data-action="stop-answer"]')) == null || c.addEventListener("click", () => this.cancelActiveAnswer(!0)), (p = this.panel) == null || p.querySelectorAll("[data-audience]").forEach((h) => {
680
701
  h.addEventListener("click", () => {
681
702
  var d;
682
703
  return this.changeAnswerAudience((d = h.dataset.audience) != null ? d : "");
@@ -740,6 +761,21 @@ function M(a) {
740
761
  const e = (t = a.metadata_json) == null ? void 0 : t.agent_run_id;
741
762
  return typeof e == "number" ? String(e) : typeof e == "string" ? e : "";
742
763
  }
764
+ function conversationTitle(a, e) {
765
+ var s;
766
+ const t = (s = a.menu_path) != null && s.length ? String(a.menu_path[a.menu_path.length - 1]) : String(a.title || a.route || document.title || "新的问答").split(" - ")[0], r = String(e || "").replace(/\s+/g, " ").replace(/[??!!。]+$/, "").trim(), n = r.length > 28 ? `${r.slice(0, 28)}...` : r;
767
+ return [t.trim(), n].filter(Boolean).join(" · ");
768
+ }
769
+ function formatFullTimestamp(a) {
770
+ const e = new Date(a);
771
+ if (Number.isNaN(e.getTime())) return "";
772
+ const t = new Intl.DateTimeFormat("zh-CN", { timeZone: "Asia/Shanghai", year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", hourCycle: "h23" }).formatToParts(e).reduce((s, r) => (s[r.type] = r.value, s), {});
773
+ return `${t.year}-${t.month}-${t.day} ${t.hour}:${t.minute}:${t.second}`;
774
+ }
775
+ function messageTime(a) {
776
+ const e = formatFullTimestamp(a);
777
+ return e ? `<time class="po-message-time" datetime="${l(a)}">${e}</time>` : "";
778
+ }
743
779
  function O(a) {
744
780
  return new Intl.DateTimeFormat("zh-CN", {
745
781
  timeZone: "Asia/Shanghai",
@@ -761,4 +797,5 @@ function G(a) {
761
797
  function Z(a) {
762
798
  return { pending: "等待中", running: "执行中", completed: "已完成", failed: "失败", cancelled: "已停止" }[a];
763
799
  }
764
- const Y = new V();exports.PageOpsSDK=Y;}));
800
+ const Y = new V();
801
+ exports.PageOpsSDK=Y;}));
package/dist/types.d.ts CHANGED
@@ -12,6 +12,8 @@ export interface PageOpsMountOptions {
12
12
  apiBase: string;
13
13
  appId: string;
14
14
  env: string;
15
+ /** Host endpoint used to decide whether source-package controls are visible. */
16
+ developerAccessUrl?: string;
15
17
  ownerId?: string;
16
18
  uploader?: string;
17
19
  getPageContext?: () => PageOpsContext;
@@ -42,6 +44,7 @@ export interface ConversationMessage {
42
44
  content: string;
43
45
  evidence_bundle_id?: string | null;
44
46
  metadata_json: Record<string, unknown>;
47
+ created_at?: string;
45
48
  }
46
49
  export interface ExecutionStep {
47
50
  id: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pageops/sdk",
3
- "version": "0.1.33",
3
+ "version": "0.1.35",
4
4
  "description": "PageOps embeddable frontend SDK",
5
5
  "type": "module",
6
6
  "main": "dist/pageops-sdk.umd.cjs",
@@ -19,7 +19,8 @@
19
19
  "dist"
20
20
  ],
21
21
  "scripts": {
22
- "build": "vite build && tsc --emitDeclarationOnly",
22
+ "build": "node scripts/sync-artifacts.cjs",
23
+ "verify:static": "node --check dist/pageops-sdk.es.js && node --check dist/pageops-sdk.umd.cjs && node scripts/verify-static.cjs",
23
24
  "pack:check": "npm pack --dry-run",
24
25
  "test": "vitest run"
25
26
  },
@@ -31,6 +32,6 @@
31
32
  "vitest": "3.2.4"
32
33
  },
33
34
  "engines": {
34
- "node": ">=20"
35
+ "node": ">=14.21.3"
35
36
  }
36
37
  }