@vite-plugin-opencode-assistant/opencode 1.0.16 → 1.0.18
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/es/plugins/page-context.js +112 -209
- package/lib/plugins/page-context.js +2 -118
- package/package.json +2 -2
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
N.call(e, r) && P(t, r, e[r]);
|
|
1
|
+
const P = "[vite-plugin-opencode]";
|
|
2
|
+
var E = Object.defineProperty, w = Object.defineProperties, b = Object.getOwnPropertyDescriptors, y = Object.getOwnPropertySymbols, S = Object.prototype.hasOwnProperty, C = Object.prototype.propertyIsEnumerable, _ = (r, t, e) => t in r ? E(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e, c = (r, t) => {
|
|
3
|
+
for (var e in t || (t = {}))
|
|
4
|
+
S.call(t, e) && _(r, e, t[e]);
|
|
6
5
|
if (y)
|
|
7
|
-
for (var
|
|
8
|
-
|
|
9
|
-
return
|
|
10
|
-
},
|
|
11
|
-
const
|
|
6
|
+
for (var e of y(t))
|
|
7
|
+
C.call(t, e) && _(r, e, t[e]);
|
|
8
|
+
return r;
|
|
9
|
+
}, p = (r, t) => w(r, b(t)), O = (r, t, e) => _(r, typeof t != "symbol" ? t + "" : t, e);
|
|
10
|
+
const s = {
|
|
12
11
|
reset: "\x1B[0m",
|
|
13
12
|
dim: "\x1B[2m",
|
|
14
13
|
bright: "\x1B[1m",
|
|
@@ -18,214 +17,143 @@ const u = {
|
|
|
18
17
|
magenta: "\x1B[35m",
|
|
19
18
|
cyan: "\x1B[36m"
|
|
20
19
|
}, j = {
|
|
21
|
-
0:
|
|
22
|
-
1:
|
|
23
|
-
2:
|
|
24
|
-
3:
|
|
25
|
-
4:
|
|
26
|
-
},
|
|
20
|
+
0: s.cyan,
|
|
21
|
+
1: s.green,
|
|
22
|
+
2: s.yellow,
|
|
23
|
+
3: s.red,
|
|
24
|
+
4: s.reset
|
|
25
|
+
}, L = {
|
|
27
26
|
0: "DEBUG",
|
|
28
27
|
1: "INFO",
|
|
29
28
|
2: "WARN",
|
|
30
29
|
3: "ERROR",
|
|
31
30
|
4: "NONE"
|
|
32
31
|
};
|
|
33
|
-
let
|
|
32
|
+
let N = {
|
|
34
33
|
level: 1
|
|
35
34
|
/* INFO */
|
|
36
35
|
};
|
|
37
|
-
function
|
|
38
|
-
const
|
|
39
|
-
return `${
|
|
36
|
+
function x() {
|
|
37
|
+
const r = /* @__PURE__ */ new Date(), t = String(r.getHours()).padStart(2, "0"), e = String(r.getMinutes()).padStart(2, "0"), n = String(r.getSeconds()).padStart(2, "0"), o = String(r.getMilliseconds()).padStart(3, "0");
|
|
38
|
+
return `${t}:${e}:${n}.${o}`;
|
|
40
39
|
}
|
|
41
|
-
function
|
|
42
|
-
const
|
|
43
|
-
if (!
|
|
44
|
-
const
|
|
45
|
-
`)[
|
|
40
|
+
function R(r = 3) {
|
|
41
|
+
const t = new Error().stack;
|
|
42
|
+
if (!t) return "";
|
|
43
|
+
const n = t.split(`
|
|
44
|
+
`)[r];
|
|
45
|
+
if (!n) return "";
|
|
46
|
+
const o = n.match(/at\s+(?:(.+?)\s+\()?(.+?):(\d+):(\d+)\)?/);
|
|
46
47
|
if (!o) return "";
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
const [, n, a, s] = i;
|
|
50
|
-
return `${a.split("/").pop() || a}:${s} ${n || "<anonymous>"}`;
|
|
48
|
+
const [, a, m, l] = o;
|
|
49
|
+
return `${m.split("/").pop() || m}:${l} ${a || "<anonymous>"}`;
|
|
51
50
|
}
|
|
52
|
-
function
|
|
53
|
-
if (
|
|
54
|
-
if (
|
|
55
|
-
if (
|
|
56
|
-
if (typeof
|
|
57
|
-
if (typeof
|
|
58
|
-
if (
|
|
59
|
-
return `${
|
|
60
|
-
${
|
|
61
|
-
if (Array.isArray(
|
|
62
|
-
return
|
|
63
|
-
if (typeof
|
|
64
|
-
const
|
|
65
|
-
return
|
|
51
|
+
function u(r, t = 0) {
|
|
52
|
+
if (t > 3) return "...";
|
|
53
|
+
if (r === null) return "null";
|
|
54
|
+
if (r === void 0) return "undefined";
|
|
55
|
+
if (typeof r == "string") return t > 0 ? `"${r}"` : r;
|
|
56
|
+
if (typeof r == "number" || typeof r == "boolean") return String(r);
|
|
57
|
+
if (r instanceof Error)
|
|
58
|
+
return `${r.name}: ${r.message}${r.stack ? `
|
|
59
|
+
${r.stack}` : ""}`;
|
|
60
|
+
if (Array.isArray(r))
|
|
61
|
+
return r.length === 0 ? "[]" : r.length > 5 ? `[${r.slice(0, 3).map((o) => u(o, t + 1)).join(", ")}, ... ${r.length - 3} more items]` : `[${r.map((n) => u(n, t + 1)).join(", ")}]`;
|
|
62
|
+
if (typeof r == "object") {
|
|
63
|
+
const e = Object.entries(r);
|
|
64
|
+
return e.length === 0 ? "{}" : e.length > 5 ? `{${e.slice(0, 3).map(([a, m]) => `${a}: ${u(m, t + 1)}`).join(", ")}, ... ${e.length - 3} more keys}` : `{${e.map(([o, a]) => `${o}: ${u(a, t + 1)}`).join(", ")}}`;
|
|
66
65
|
}
|
|
67
|
-
return String(
|
|
66
|
+
return String(r);
|
|
68
67
|
}
|
|
69
|
-
function
|
|
70
|
-
if (!
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
(
|
|
68
|
+
function I(r) {
|
|
69
|
+
if (!r || Object.keys(r).length === 0) return "";
|
|
70
|
+
const t = [];
|
|
71
|
+
r.module && t.push(`[${r.module}]`), r.operation && t.push(`(${r.operation})`), r.traceId && t.push(`trace:${r.traceId}`), r.duration !== void 0 && t.push(`${r.duration}ms`);
|
|
72
|
+
const e = Object.keys(r).filter(
|
|
73
|
+
(n) => !["module", "operation", "traceId", "duration", "error"].includes(n)
|
|
75
74
|
);
|
|
76
|
-
if (
|
|
77
|
-
const
|
|
78
|
-
|
|
75
|
+
if (e.length > 0) {
|
|
76
|
+
const n = {};
|
|
77
|
+
e.forEach((o) => n[o] = r[o]), t.push(u(n));
|
|
79
78
|
}
|
|
80
|
-
return
|
|
79
|
+
return t.join(" ");
|
|
81
80
|
}
|
|
82
|
-
function
|
|
83
|
-
if (
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
if (
|
|
90
|
-
const
|
|
91
|
-
|
|
81
|
+
function $(r, t, e, ...n) {
|
|
82
|
+
if (r < N.level) return;
|
|
83
|
+
const o = [];
|
|
84
|
+
o.push(`${s.dim}[${process.pid}]${s.reset}`), o.push(`${s.dim}${x()}${s.reset}`);
|
|
85
|
+
const a = j[r], m = L[r].padEnd(5);
|
|
86
|
+
o.push(`${a}${m}${s.reset}`), o.push(`${s.bright}${P}${s.reset}`);
|
|
87
|
+
const l = I(e);
|
|
88
|
+
if (l && o.push(`${s.magenta}${l}${s.reset}`), o.push(t), r >= 2) {
|
|
89
|
+
const i = R(4);
|
|
90
|
+
i && o.push(`${s.dim}(${i})${s.reset}`);
|
|
92
91
|
}
|
|
93
|
-
const
|
|
94
|
-
if (
|
|
95
|
-
const
|
|
96
|
-
|
|
92
|
+
const h = n.map((i) => u(i)).join(" ");
|
|
93
|
+
if (h && o.push(h), e != null && e.error) {
|
|
94
|
+
const i = e.error;
|
|
95
|
+
i instanceof Error ? o.push(`${s.red}Error: ${i.message}${s.reset}`) : o.push(`${s.red}Error: ${u(i)}${s.reset}`);
|
|
97
96
|
}
|
|
98
|
-
const
|
|
99
|
-
|
|
97
|
+
const d = o.join(" ");
|
|
98
|
+
r >= 3 ? console.error(d) : r === 2 ? console.warn(d) : console.log(d);
|
|
100
99
|
}
|
|
101
|
-
const
|
|
102
|
-
debug(
|
|
103
|
-
|
|
100
|
+
const f = {
|
|
101
|
+
debug(r, t, ...e) {
|
|
102
|
+
$(0, r, t, ...e);
|
|
104
103
|
},
|
|
105
|
-
info(
|
|
106
|
-
|
|
104
|
+
info(r, t, ...e) {
|
|
105
|
+
$(1, r, t, ...e);
|
|
107
106
|
},
|
|
108
|
-
warn(
|
|
109
|
-
|
|
107
|
+
warn(r, t, ...e) {
|
|
108
|
+
$(2, r, t, ...e);
|
|
110
109
|
},
|
|
111
|
-
error(
|
|
112
|
-
|
|
110
|
+
error(r, t, ...e) {
|
|
111
|
+
$(3, r, t, ...e);
|
|
113
112
|
},
|
|
114
|
-
group(
|
|
113
|
+
group(r, t) {
|
|
115
114
|
},
|
|
116
115
|
groupEnd() {
|
|
117
116
|
}
|
|
118
117
|
};
|
|
119
|
-
class
|
|
120
|
-
constructor(
|
|
121
|
-
O(this, "startTime"), O(this, "context"), O(this, "operation"), this.operation =
|
|
118
|
+
class T {
|
|
119
|
+
constructor(t, e) {
|
|
120
|
+
O(this, "startTime"), O(this, "context"), O(this, "operation"), this.operation = t, this.context = e || {}, this.startTime = performance.now(), f.debug(`⏱️ Starting: ${t}`, this.context);
|
|
122
121
|
}
|
|
123
|
-
end(
|
|
124
|
-
const
|
|
125
|
-
return
|
|
122
|
+
end(t) {
|
|
123
|
+
const e = Math.round(performance.now() - this.startTime), n = t || `✓ Completed: ${this.operation}`;
|
|
124
|
+
return f.debug(n, p(c({}, this.context), { duration: e })), e;
|
|
126
125
|
}
|
|
127
|
-
checkpoint(
|
|
128
|
-
const
|
|
129
|
-
return
|
|
126
|
+
checkpoint(t) {
|
|
127
|
+
const e = Math.round(performance.now() - this.startTime);
|
|
128
|
+
return f.debug(` ↳ ${t}`, p(c({}, this.context), { duration: e })), e;
|
|
130
129
|
}
|
|
131
130
|
}
|
|
132
|
-
function
|
|
131
|
+
function B(r) {
|
|
133
132
|
return {
|
|
134
|
-
debug(
|
|
135
|
-
|
|
133
|
+
debug(t, e, ...n) {
|
|
134
|
+
f.debug(t, p(c({}, e), { module: r }), ...n);
|
|
136
135
|
},
|
|
137
|
-
info(
|
|
138
|
-
|
|
136
|
+
info(t, e, ...n) {
|
|
137
|
+
f.info(t, p(c({}, e), { module: r }), ...n);
|
|
139
138
|
},
|
|
140
|
-
warn(
|
|
141
|
-
|
|
139
|
+
warn(t, e, ...n) {
|
|
140
|
+
f.warn(t, p(c({}, e), { module: r }), ...n);
|
|
142
141
|
},
|
|
143
|
-
error(
|
|
144
|
-
|
|
142
|
+
error(t, e, ...n) {
|
|
143
|
+
f.error(t, p(c({}, e), { module: r }), ...n);
|
|
145
144
|
},
|
|
146
|
-
timer(
|
|
147
|
-
return new
|
|
145
|
+
timer(t, e) {
|
|
146
|
+
return new T(t, p(c({}, e), { module: r }));
|
|
148
147
|
}
|
|
149
148
|
};
|
|
150
149
|
}
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
async function r() {
|
|
159
|
-
try {
|
|
160
|
-
p.debug("Fetching context...", { apiUrl: e });
|
|
161
|
-
const n = await fetch(e);
|
|
162
|
-
if (!n.ok)
|
|
163
|
-
return p.error("Context API returned error status", {
|
|
164
|
-
status: n.status,
|
|
165
|
-
statusText: n.statusText,
|
|
166
|
-
apiUrl: e
|
|
167
|
-
}), null;
|
|
168
|
-
const a = await n.json();
|
|
169
|
-
return p.debug("Context received", { url: a.url, title: a.title }), {
|
|
170
|
-
url: a.url || "",
|
|
171
|
-
title: a.title || "",
|
|
172
|
-
selectedElements: a.selectedElements
|
|
173
|
-
};
|
|
174
|
-
} catch (n) {
|
|
175
|
-
const a = n instanceof Error ? n.message : String(n), s = n instanceof Error ? n.name : "UnknownError";
|
|
176
|
-
return p.error("Failed to get context", {
|
|
177
|
-
error: a,
|
|
178
|
-
errorType: s,
|
|
179
|
-
apiUrl: e
|
|
180
|
-
}), null;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
function o(n, a) {
|
|
184
|
-
var l, c;
|
|
185
|
-
const s = [];
|
|
186
|
-
if (s.push(`### 用户选中节点 ${a + 1}`), n.filePath)
|
|
187
|
-
if (n.filePath.includes("node_modules")) {
|
|
188
|
-
if (s.push(`- **元素选择器**: \`${n.description}\``), (l = n.innerText) != null && l.trim()) {
|
|
189
|
-
const d = n.innerText.trim().substring(0, 100);
|
|
190
|
-
s.push(`- **节点文本**: \`${d}${n.innerText.length > 100 ? "..." : ""}\``);
|
|
191
|
-
}
|
|
192
|
-
} else {
|
|
193
|
-
let d = n.filePath;
|
|
194
|
-
if (n.line && (d += `:${n.line}`, n.column && (d += `:${n.column}`)), s.push(`- **文件位置**: \`${d}\``), (c = n.innerText) != null && c.trim()) {
|
|
195
|
-
const E = n.innerText.trim().substring(0, 100), b = n.innerText.length > 100 ? `
|
|
196
|
-
... (已省略部分内容)` : "";
|
|
197
|
-
s.push(`- **节点文本**:
|
|
198
|
-
\`\`\`text
|
|
199
|
-
${E}${b}
|
|
200
|
-
\`\`\``);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
return s.join(`
|
|
204
|
-
`) + `
|
|
205
|
-
`;
|
|
206
|
-
}
|
|
207
|
-
function i(n) {
|
|
208
|
-
var l;
|
|
209
|
-
const a = n.title ? `[${n.title}](${n.url})` : n.url;
|
|
210
|
-
let s = `【系统提示:以下是用户当前正在浏览的页面上下文,请将其作为最高优先级的背景信息来理解和响应用户的请求。】
|
|
211
|
-
|
|
212
|
-
`;
|
|
213
|
-
return s += `用户现在正在浏览项目中的这个页面:${a}
|
|
214
|
-
|
|
215
|
-
`, (l = n.selectedElements) != null && l.length && (s += `用户选中了以下节点:
|
|
216
|
-
|
|
217
|
-
`, n.selectedElements.forEach((c, f) => {
|
|
218
|
-
s += o(c, f) + `
|
|
219
|
-
`;
|
|
220
|
-
})), s += `---
|
|
221
|
-
**用户的请求**:
|
|
222
|
-
|
|
223
|
-
`, s;
|
|
224
|
-
}
|
|
225
|
-
return {
|
|
226
|
-
"experimental.chat.system.transform": async (n, a) => {
|
|
227
|
-
p.debug("System transform hook called");
|
|
228
|
-
const s = `
|
|
150
|
+
const g = B("OpenCodePluginPageContext"), A = async () => {
|
|
151
|
+
g.info("PageContextPlugin loading...");
|
|
152
|
+
const r = process.env.OPENCODE_CONTEXT_API_URL;
|
|
153
|
+
return g.debug("Context API URL:", { contextApiUrl: r }), r ? (g.info("Plugin initialized successfully"), {
|
|
154
|
+
"experimental.chat.system.transform": async (t, e) => {
|
|
155
|
+
g.debug("System transform hook called");
|
|
156
|
+
const n = `
|
|
229
157
|
你是一个专业的前端开发助手,运行在 **OpenCode** 平台中,并通过 **vite-plugin-opencode-assistant** 插件集成到用户的 Vite 开发环境。
|
|
230
158
|
|
|
231
159
|
## 你的工作环境
|
|
@@ -248,18 +176,6 @@ ${E}${b}
|
|
|
248
176
|
└─────────────────────────────────────────────────────────────┘
|
|
249
177
|
\`\`\`
|
|
250
178
|
|
|
251
|
-
### 上下文感知机制
|
|
252
|
-
|
|
253
|
-
当用户与你对话时,系统会自动收集并注入以下页面上下文信息:
|
|
254
|
-
|
|
255
|
-
1. **页面信息**:用户当前浏览的页面 URL 和标题
|
|
256
|
-
2. **选中元素**:用户在页面上选中的 DOM 元素信息,包括:
|
|
257
|
-
- 元素的选择器
|
|
258
|
-
- 元素的文本内容
|
|
259
|
-
- 源码文件路径(如果有)
|
|
260
|
-
|
|
261
|
-
这些信息由 Vite 插件通过内部 API 收集,并在用户发送消息时自动附加到消息前缀中。
|
|
262
|
-
|
|
263
179
|
请遵循以下规则:
|
|
264
180
|
|
|
265
181
|
1. **前置要求:定位节点位置**(强制)
|
|
@@ -268,30 +184,17 @@ ${E}${b}
|
|
|
268
184
|
- 通过文件搜索、DOM 分析等方式定位到具体代码位置
|
|
269
185
|
- 明确知道节点在哪里被定义/使用后,再处理用户请求
|
|
270
186
|
|
|
187
|
+
2. 如有需要,使用 Chrome DevTools Mcp 来获取用户当前正在浏览的页面 URL,而不是直接从上下文中提取。
|
|
188
|
+
|
|
271
189
|
2. **理解上下文**:将页面 URL、标题和选中节点信息作为用户请求的背景,帮助理解用户的真实意图。
|
|
272
190
|
|
|
273
191
|
3. **直接行动**:在明确节点位置后,针对用户的实际请求给出清晰、可执行的方案。
|
|
274
192
|
`.trim();
|
|
275
|
-
|
|
276
|
-
},
|
|
277
|
-
"experimental.chat.messages.transform": async (n, a) => {
|
|
278
|
-
var d;
|
|
279
|
-
p.debug("Message transform hook called");
|
|
280
|
-
const s = await r();
|
|
281
|
-
if (p.debug("Context data", {
|
|
282
|
-
hasUrl: !!(s != null && s.url),
|
|
283
|
-
hasElements: !!((d = s == null ? void 0 : s.selectedElements) != null && d.length)
|
|
284
|
-
}), !(s != null && s.url)) return;
|
|
285
|
-
const l = [...a.messages].reverse().find((E) => E.info.role === "user");
|
|
286
|
-
if (!l) return;
|
|
287
|
-
const c = l.parts.find((E) => E.type === "text");
|
|
288
|
-
if (!c || !("text" in c) || c.text.includes(T)) return;
|
|
289
|
-
const f = i(s);
|
|
290
|
-
c.text = f + c.text;
|
|
193
|
+
e.system.push(n);
|
|
291
194
|
}
|
|
292
|
-
};
|
|
195
|
+
}) : (g.warn("OPENCODE_CONTEXT_API_URL is not set, page context plugin will not work"), {});
|
|
293
196
|
};
|
|
294
197
|
export {
|
|
295
|
-
|
|
296
|
-
|
|
198
|
+
A as PageContextPlugin,
|
|
199
|
+
A as default
|
|
297
200
|
};
|
|
@@ -51,96 +51,7 @@ const PageContextPlugin = () => __async(null, null, function* () {
|
|
|
51
51
|
log.warn("OPENCODE_CONTEXT_API_URL is not set, page context plugin will not work");
|
|
52
52
|
return {};
|
|
53
53
|
}
|
|
54
|
-
const apiUrl = contextApiUrl;
|
|
55
54
|
log.info("Plugin initialized successfully");
|
|
56
|
-
function getPageContext() {
|
|
57
|
-
return __async(this, null, function* () {
|
|
58
|
-
try {
|
|
59
|
-
log.debug("Fetching context...", { apiUrl });
|
|
60
|
-
const response = yield fetch(apiUrl);
|
|
61
|
-
if (!response.ok) {
|
|
62
|
-
log.error("Context API returned error status", {
|
|
63
|
-
status: response.status,
|
|
64
|
-
statusText: response.statusText,
|
|
65
|
-
apiUrl
|
|
66
|
-
});
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
const data = yield response.json();
|
|
70
|
-
log.debug("Context received", { url: data.url, title: data.title });
|
|
71
|
-
return {
|
|
72
|
-
url: data.url || "",
|
|
73
|
-
title: data.title || "",
|
|
74
|
-
selectedElements: data.selectedElements
|
|
75
|
-
};
|
|
76
|
-
} catch (error) {
|
|
77
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
78
|
-
const errorName = error instanceof Error ? error.name : "UnknownError";
|
|
79
|
-
log.error("Failed to get context", {
|
|
80
|
-
error: errorMessage,
|
|
81
|
-
errorType: errorName,
|
|
82
|
-
apiUrl
|
|
83
|
-
});
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
function formatSelectedElement(element, index) {
|
|
89
|
-
var _a, _b;
|
|
90
|
-
const parts = [];
|
|
91
|
-
parts.push(`### \u7528\u6237\u9009\u4E2D\u8282\u70B9 ${index + 1}`);
|
|
92
|
-
if (element.filePath) {
|
|
93
|
-
const isNodeModule = element.filePath.includes("node_modules");
|
|
94
|
-
if (isNodeModule) {
|
|
95
|
-
parts.push(`- **\u5143\u7D20\u9009\u62E9\u5668**: \`${element.description}\``);
|
|
96
|
-
if ((_a = element.innerText) == null ? void 0 : _a.trim()) {
|
|
97
|
-
const text = element.innerText.trim().substring(0, 100);
|
|
98
|
-
parts.push(`- **\u8282\u70B9\u6587\u672C**: \`${text}${element.innerText.length > 100 ? "..." : ""}\``);
|
|
99
|
-
}
|
|
100
|
-
} else {
|
|
101
|
-
let location = element.filePath;
|
|
102
|
-
if (element.line) {
|
|
103
|
-
location += `:${element.line}`;
|
|
104
|
-
if (element.column) {
|
|
105
|
-
location += `:${element.column}`;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
parts.push(`- **\u6587\u4EF6\u4F4D\u7F6E**: \`${location}\``);
|
|
109
|
-
if ((_b = element.innerText) == null ? void 0 : _b.trim()) {
|
|
110
|
-
const text = element.innerText.trim().substring(0, import_shared.MAX_TEXT_LENGTH);
|
|
111
|
-
const suffix = element.innerText.length > import_shared.MAX_TEXT_LENGTH ? "\n... (\u5DF2\u7701\u7565\u90E8\u5206\u5185\u5BB9)" : "";
|
|
112
|
-
parts.push(`- **\u8282\u70B9\u6587\u672C**:
|
|
113
|
-
\`\`\`text
|
|
114
|
-
${text}${suffix}
|
|
115
|
-
\`\`\``);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return parts.join("\n") + "\n";
|
|
120
|
-
}
|
|
121
|
-
function buildContextPrefix(context) {
|
|
122
|
-
var _a;
|
|
123
|
-
const pageLink = context.title ? `[${context.title}](${context.url})` : context.url;
|
|
124
|
-
let prefix = `\u3010\u7CFB\u7EDF\u63D0\u793A\uFF1A\u4EE5\u4E0B\u662F\u7528\u6237\u5F53\u524D\u6B63\u5728\u6D4F\u89C8\u7684\u9875\u9762\u4E0A\u4E0B\u6587\uFF0C\u8BF7\u5C06\u5176\u4F5C\u4E3A\u6700\u9AD8\u4F18\u5148\u7EA7\u7684\u80CC\u666F\u4FE1\u606F\u6765\u7406\u89E3\u548C\u54CD\u5E94\u7528\u6237\u7684\u8BF7\u6C42\u3002\u3011
|
|
125
|
-
|
|
126
|
-
`;
|
|
127
|
-
prefix += `\u7528\u6237\u73B0\u5728\u6B63\u5728\u6D4F\u89C8\u9879\u76EE\u4E2D\u7684\u8FD9\u4E2A\u9875\u9762\uFF1A${pageLink}
|
|
128
|
-
|
|
129
|
-
`;
|
|
130
|
-
if ((_a = context.selectedElements) == null ? void 0 : _a.length) {
|
|
131
|
-
prefix += `\u7528\u6237\u9009\u4E2D\u4E86\u4EE5\u4E0B\u8282\u70B9\uFF1A
|
|
132
|
-
|
|
133
|
-
`;
|
|
134
|
-
context.selectedElements.forEach((element, index) => {
|
|
135
|
-
prefix += formatSelectedElement(element, index) + "\n";
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
prefix += `---
|
|
139
|
-
**\u7528\u6237\u7684\u8BF7\u6C42**\uFF1A
|
|
140
|
-
|
|
141
|
-
`;
|
|
142
|
-
return prefix;
|
|
143
|
-
}
|
|
144
55
|
return {
|
|
145
56
|
"experimental.chat.system.transform": (_input, output) => __async(null, null, function* () {
|
|
146
57
|
log.debug("System transform hook called");
|
|
@@ -167,18 +78,6 @@ ${text}${suffix}
|
|
|
167
78
|
\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518
|
|
168
79
|
\`\`\`
|
|
169
80
|
|
|
170
|
-
### \u4E0A\u4E0B\u6587\u611F\u77E5\u673A\u5236
|
|
171
|
-
|
|
172
|
-
\u5F53\u7528\u6237\u4E0E\u4F60\u5BF9\u8BDD\u65F6\uFF0C\u7CFB\u7EDF\u4F1A\u81EA\u52A8\u6536\u96C6\u5E76\u6CE8\u5165\u4EE5\u4E0B\u9875\u9762\u4E0A\u4E0B\u6587\u4FE1\u606F\uFF1A
|
|
173
|
-
|
|
174
|
-
1. **\u9875\u9762\u4FE1\u606F**\uFF1A\u7528\u6237\u5F53\u524D\u6D4F\u89C8\u7684\u9875\u9762 URL \u548C\u6807\u9898
|
|
175
|
-
2. **\u9009\u4E2D\u5143\u7D20**\uFF1A\u7528\u6237\u5728\u9875\u9762\u4E0A\u9009\u4E2D\u7684 DOM \u5143\u7D20\u4FE1\u606F\uFF0C\u5305\u62EC\uFF1A
|
|
176
|
-
- \u5143\u7D20\u7684\u9009\u62E9\u5668
|
|
177
|
-
- \u5143\u7D20\u7684\u6587\u672C\u5185\u5BB9
|
|
178
|
-
- \u6E90\u7801\u6587\u4EF6\u8DEF\u5F84\uFF08\u5982\u679C\u6709\uFF09
|
|
179
|
-
|
|
180
|
-
\u8FD9\u4E9B\u4FE1\u606F\u7531 Vite \u63D2\u4EF6\u901A\u8FC7\u5185\u90E8 API \u6536\u96C6\uFF0C\u5E76\u5728\u7528\u6237\u53D1\u9001\u6D88\u606F\u65F6\u81EA\u52A8\u9644\u52A0\u5230\u6D88\u606F\u524D\u7F00\u4E2D\u3002
|
|
181
|
-
|
|
182
81
|
\u8BF7\u9075\u5FAA\u4EE5\u4E0B\u89C4\u5219\uFF1A
|
|
183
82
|
|
|
184
83
|
1. **\u524D\u7F6E\u8981\u6C42\uFF1A\u5B9A\u4F4D\u8282\u70B9\u4F4D\u7F6E**\uFF08\u5F3A\u5236\uFF09
|
|
@@ -187,28 +86,13 @@ ${text}${suffix}
|
|
|
187
86
|
- \u901A\u8FC7\u6587\u4EF6\u641C\u7D22\u3001DOM \u5206\u6790\u7B49\u65B9\u5F0F\u5B9A\u4F4D\u5230\u5177\u4F53\u4EE3\u7801\u4F4D\u7F6E
|
|
188
87
|
- \u660E\u786E\u77E5\u9053\u8282\u70B9\u5728\u54EA\u91CC\u88AB\u5B9A\u4E49/\u4F7F\u7528\u540E\uFF0C\u518D\u5904\u7406\u7528\u6237\u8BF7\u6C42
|
|
189
88
|
|
|
89
|
+
2. \u5982\u6709\u9700\u8981\uFF0C\u4F7F\u7528 Chrome DevTools Mcp \u6765\u83B7\u53D6\u7528\u6237\u5F53\u524D\u6B63\u5728\u6D4F\u89C8\u7684\u9875\u9762 URL\uFF0C\u800C\u4E0D\u662F\u76F4\u63A5\u4ECE\u4E0A\u4E0B\u6587\u4E2D\u63D0\u53D6\u3002
|
|
90
|
+
|
|
190
91
|
2. **\u7406\u89E3\u4E0A\u4E0B\u6587**\uFF1A\u5C06\u9875\u9762 URL\u3001\u6807\u9898\u548C\u9009\u4E2D\u8282\u70B9\u4FE1\u606F\u4F5C\u4E3A\u7528\u6237\u8BF7\u6C42\u7684\u80CC\u666F\uFF0C\u5E2E\u52A9\u7406\u89E3\u7528\u6237\u7684\u771F\u5B9E\u610F\u56FE\u3002
|
|
191
92
|
|
|
192
93
|
3. **\u76F4\u63A5\u884C\u52A8**\uFF1A\u5728\u660E\u786E\u8282\u70B9\u4F4D\u7F6E\u540E\uFF0C\u9488\u5BF9\u7528\u6237\u7684\u5B9E\u9645\u8BF7\u6C42\u7ED9\u51FA\u6E05\u6670\u3001\u53EF\u6267\u884C\u7684\u65B9\u6848\u3002
|
|
193
94
|
`.trim();
|
|
194
95
|
output.system.push(systemPrompt);
|
|
195
|
-
}),
|
|
196
|
-
"experimental.chat.messages.transform": (_input, output) => __async(null, null, function* () {
|
|
197
|
-
var _a;
|
|
198
|
-
log.debug("Message transform hook called");
|
|
199
|
-
const context = yield getPageContext();
|
|
200
|
-
log.debug("Context data", {
|
|
201
|
-
hasUrl: !!(context == null ? void 0 : context.url),
|
|
202
|
-
hasElements: !!((_a = context == null ? void 0 : context.selectedElements) == null ? void 0 : _a.length)
|
|
203
|
-
});
|
|
204
|
-
if (!(context == null ? void 0 : context.url)) return;
|
|
205
|
-
const lastUserMsg = [...output.messages].reverse().find((m) => m.info.role === "user");
|
|
206
|
-
if (!lastUserMsg) return;
|
|
207
|
-
const textPart = lastUserMsg.parts.find((p) => p.type === "text");
|
|
208
|
-
if (!textPart || !("text" in textPart)) return;
|
|
209
|
-
if (textPart.text.includes(import_shared.CONTEXT_MARKER)) return;
|
|
210
|
-
const prefix = buildContextPrefix(context);
|
|
211
|
-
textPart.text = prefix + textPart.text;
|
|
212
96
|
})
|
|
213
97
|
};
|
|
214
98
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vite-plugin-opencode-assistant/opencode",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.18",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "es/index.js",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"execa": "^9.6.1",
|
|
25
|
-
"@vite-plugin-opencode-assistant/shared": "1.0.
|
|
25
|
+
"@vite-plugin-opencode-assistant/shared": "1.0.18"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@opencode-ai/plugin": "^1.3.15",
|