@vite-plugin-opencode-assistant/opencode 1.0.57 → 1.0.59

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.
@@ -1,13 +1,25 @@
1
- const b = "[vite-plugin-opencode]";
2
- var w = Object.defineProperty, y = Object.defineProperties, E = Object.getOwnPropertyDescriptors, _ = Object.getOwnPropertySymbols, S = Object.prototype.hasOwnProperty, j = Object.prototype.propertyIsEnumerable, O = (r, t, e) => t in r ? w(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) && O(r, e, t[e]);
5
- if (_)
6
- for (var e of _(t))
7
- j.call(t, e) && O(r, e, t[e]);
8
- return r;
9
- }, f = (r, t) => y(r, E(t)), h = (r, t, e) => O(r, typeof t != "symbol" ? t + "" : t, e);
10
- const s = {
1
+ const LOG_PREFIX = "[vite-plugin-opencode]";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __spreadValues = (a, b) => {
10
+ for (var prop in b || (b = {}))
11
+ if (__hasOwnProp.call(b, prop))
12
+ __defNormalProp(a, prop, b[prop]);
13
+ if (__getOwnPropSymbols)
14
+ for (var prop of __getOwnPropSymbols(b)) {
15
+ if (__propIsEnum.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ }
18
+ return a;
19
+ };
20
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
21
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
22
+ const COLORS = {
11
23
  reset: "\x1B[0m",
12
24
  dim: "\x1B[2m",
13
25
  bright: "\x1B[1m",
@@ -16,137 +28,228 @@ const s = {
16
28
  yellow: "\x1B[33m",
17
29
  magenta: "\x1B[35m",
18
30
  cyan: "\x1B[36m"
19
- }, P = {
20
- 0: s.cyan,
21
- 1: s.green,
22
- 2: s.yellow,
23
- 3: s.red,
24
- 4: s.reset
25
- }, B = {
26
- 0: "DEBUG",
27
- 1: "INFO",
28
- 2: "WARN",
29
- 3: "ERROR",
30
- 4: "NONE"
31
31
  };
32
- let N = {
32
+ const LEVEL_COLORS = {
33
+ [
34
+ 0
35
+ /* DEBUG */
36
+ ]: COLORS.cyan,
37
+ [
38
+ 1
39
+ /* INFO */
40
+ ]: COLORS.green,
41
+ [
42
+ 2
43
+ /* WARN */
44
+ ]: COLORS.yellow,
45
+ [
46
+ 3
47
+ /* ERROR */
48
+ ]: COLORS.red,
49
+ [
50
+ 4
51
+ /* NONE */
52
+ ]: COLORS.reset
53
+ };
54
+ const LEVEL_NAMES = {
55
+ [
56
+ 0
57
+ /* DEBUG */
58
+ ]: "DEBUG",
59
+ [
60
+ 1
61
+ /* INFO */
62
+ ]: "INFO",
63
+ [
64
+ 2
65
+ /* WARN */
66
+ ]: "WARN",
67
+ [
68
+ 3
69
+ /* ERROR */
70
+ ]: "ERROR",
71
+ [
72
+ 4
73
+ /* NONE */
74
+ ]: "NONE"
75
+ };
76
+ let globalConfig = {
33
77
  level: 1
34
78
  /* INFO */
35
79
  };
36
- function L() {
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}`;
80
+ function getTimestamp() {
81
+ const now = /* @__PURE__ */ new Date();
82
+ const hours = String(now.getHours()).padStart(2, "0");
83
+ const minutes = String(now.getMinutes()).padStart(2, "0");
84
+ const seconds = String(now.getSeconds()).padStart(2, "0");
85
+ const ms = String(now.getMilliseconds()).padStart(3, "0");
86
+ return `${hours}:${minutes}:${seconds}.${ms}`;
39
87
  }
40
- function I(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+)\)?/);
47
- if (!o) return "";
48
- const [, a, m, g] = o;
49
- return `${m.split("/").pop() || m}:${g} ${a || "<anonymous>"}`;
88
+ function getCallerInfo(depth = 3) {
89
+ const stack = new Error().stack;
90
+ if (!stack) return "";
91
+ const lines = stack.split("\n");
92
+ const targetLine = lines[depth];
93
+ if (!targetLine) return "";
94
+ const match = targetLine.match(/at\s+(?:(.+?)\s+\()?(.+?):(\d+):(\d+)\)?/);
95
+ if (!match) return "";
96
+ const [, funcName, filePath, line] = match;
97
+ const fileName = filePath.split("/").pop() || filePath;
98
+ const func = funcName || "<anonymous>";
99
+ return `${fileName}:${line} ${func}`;
50
100
  }
51
- function p(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) => p(o, t + 1)).join(", ")}, ... ${r.length - 3} more items]` : `[${r.map((n) => p(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}: ${p(m, t + 1)}`).join(", ")}, ... ${e.length - 3} more keys}` : `{${e.map(([o, a]) => `${o}: ${p(a, t + 1)}`).join(", ")}}`;
101
+ function formatValue(value, depth = 0) {
102
+ if (depth > 3) return "...";
103
+ if (value === null) return "null";
104
+ if (value === void 0) return "undefined";
105
+ if (typeof value === "string") return depth > 0 ? `"${value}"` : value;
106
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
107
+ if (value instanceof Error) {
108
+ return `${value.name}: ${value.message}${value.stack ? `
109
+ ${value.stack}` : ""}`;
110
+ }
111
+ if (Array.isArray(value)) {
112
+ if (value.length === 0) return "[]";
113
+ if (value.length > 5) {
114
+ const items2 = value.slice(0, 3).map((v) => formatValue(v, depth + 1));
115
+ return `[${items2.join(", ")}, ... ${value.length - 3} more items]`;
116
+ }
117
+ const items = value.map((v) => formatValue(v, depth + 1));
118
+ return `[${items.join(", ")}]`;
119
+ }
120
+ if (typeof value === "object") {
121
+ const entries = Object.entries(value);
122
+ if (entries.length === 0) return "{}";
123
+ if (entries.length > 5) {
124
+ const shown = entries.slice(0, 3).map(([k, v]) => `${k}: ${formatValue(v, depth + 1)}`);
125
+ return `{${shown.join(", ")}, ... ${entries.length - 3} more keys}`;
126
+ }
127
+ const formatted = entries.map(([k, v]) => `${k}: ${formatValue(v, depth + 1)}`);
128
+ return `{${formatted.join(", ")}}`;
65
129
  }
66
- return String(r);
130
+ return String(value);
67
131
  }
68
- function C(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)
132
+ function formatContext(context) {
133
+ if (!context || Object.keys(context).length === 0) return "";
134
+ const parts = [];
135
+ if (context.module) parts.push(`[${context.module}]`);
136
+ if (context.operation) parts.push(`(${context.operation})`);
137
+ if (context.traceId) parts.push(`trace:${context.traceId}`);
138
+ if (context.duration !== void 0) parts.push(`${context.duration}ms`);
139
+ const extraKeys = Object.keys(context).filter(
140
+ (k) => !["module", "operation", "traceId", "duration", "error"].includes(k)
74
141
  );
75
- if (e.length > 0) {
76
- const n = {};
77
- e.forEach((o) => n[o] = r[o]), t.push(p(n));
142
+ if (extraKeys.length > 0) {
143
+ const extra = {};
144
+ extraKeys.forEach((k) => extra[k] = context[k]);
145
+ parts.push(formatValue(extra));
78
146
  }
79
- return t.join(" ");
147
+ return parts.join(" ");
80
148
  }
81
- function d(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}${L()}${s.reset}`);
85
- const a = P[r], m = B[r].padEnd(5);
86
- o.push(`${a}${m}${s.reset}`), o.push(`${s.bright}${b}${s.reset}`);
87
- const g = C(e);
88
- if (g && o.push(`${s.magenta}${g}${s.reset}`), o.push(t), r >= 2) {
89
- const i = I(4);
90
- i && o.push(`${s.dim}(${i})${s.reset}`);
149
+ function log(level, message, context, ...args) {
150
+ if (level < globalConfig.level) return;
151
+ const parts = [];
152
+ parts.push(`${COLORS.dim}[${process.pid}]${COLORS.reset}`);
153
+ {
154
+ parts.push(`${COLORS.dim}${getTimestamp()}${COLORS.reset}`);
155
+ }
156
+ const levelColor = LEVEL_COLORS[level];
157
+ const levelName = LEVEL_NAMES[level].padEnd(5);
158
+ parts.push(`${levelColor}${levelName}${COLORS.reset}`);
159
+ parts.push(`${COLORS.bright}${LOG_PREFIX}${COLORS.reset}`);
160
+ const contextStr = formatContext(context);
161
+ if (contextStr) {
162
+ parts.push(`${COLORS.magenta}${contextStr}${COLORS.reset}`);
163
+ }
164
+ parts.push(message);
165
+ if (level >= 2) {
166
+ const caller = getCallerInfo(4);
167
+ if (caller) {
168
+ parts.push(`${COLORS.dim}(${caller})${COLORS.reset}`);
169
+ }
170
+ }
171
+ const formattedArgs = args.map((a) => formatValue(a)).join(" ");
172
+ if (formattedArgs) {
173
+ parts.push(formattedArgs);
174
+ }
175
+ if (context == null ? void 0 : context.error) {
176
+ const err = context.error;
177
+ if (err instanceof Error) {
178
+ parts.push(`${COLORS.red}Error: ${err.message}${COLORS.reset}`);
179
+ } else {
180
+ parts.push(`${COLORS.red}Error: ${formatValue(err)}${COLORS.reset}`);
181
+ }
91
182
  }
92
- const l = n.map((i) => p(i)).join(" ");
93
- if (l && o.push(l), 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: ${p(i)}${s.reset}`);
183
+ const output = parts.join(" ");
184
+ if (level >= 3) {
185
+ console.error(output);
186
+ } else if (level === 2) {
187
+ console.warn(output);
188
+ } else {
189
+ console.log(output);
96
190
  }
97
- const $ = o.join(" ");
98
- r >= 3 ? console.error($) : r === 2 ? console.warn($) : console.log($);
99
191
  }
100
- const u = {
101
- debug(r, t, ...e) {
102
- d(0, r, t, ...e);
192
+ const logger = {
193
+ debug(message, context, ...args) {
194
+ log(0, message, context, ...args);
103
195
  },
104
- info(r, t, ...e) {
105
- d(1, r, t, ...e);
196
+ info(message, context, ...args) {
197
+ log(1, message, context, ...args);
106
198
  },
107
- warn(r, t, ...e) {
108
- d(2, r, t, ...e);
199
+ warn(message, context, ...args) {
200
+ log(2, message, context, ...args);
109
201
  },
110
- error(r, t, ...e) {
111
- d(3, r, t, ...e);
202
+ error(message, context, ...args) {
203
+ log(3, message, context, ...args);
112
204
  },
113
- group(r, t) {
205
+ group(label, context) {
206
+ return;
114
207
  },
115
208
  groupEnd() {
209
+ return;
116
210
  }
117
211
  };
118
- class R {
119
- constructor(t, e) {
120
- h(this, "startTime"), h(this, "context"), h(this, "operation"), this.operation = t, this.context = e || {}, this.startTime = performance.now(), u.debug(`⏱️ Starting: ${t}`, this.context);
212
+ class PerformanceTimer {
213
+ constructor(operation, context) {
214
+ __publicField(this, "startTime");
215
+ __publicField(this, "context");
216
+ __publicField(this, "operation");
217
+ this.operation = operation;
218
+ this.context = context || {};
219
+ this.startTime = performance.now();
220
+ logger.debug(`⏱️ Starting: ${operation}`, this.context);
121
221
  }
122
- end(t) {
123
- const e = Math.round(performance.now() - this.startTime), n = t || `✓ Completed: ${this.operation}`;
124
- return u.debug(n, f(c({}, this.context), { duration: e })), e;
222
+ end(message) {
223
+ const duration = Math.round(performance.now() - this.startTime);
224
+ const msg = message || `✓ Completed: ${this.operation}`;
225
+ logger.debug(msg, __spreadProps(__spreadValues({}, this.context), { duration }));
226
+ return duration;
125
227
  }
126
- checkpoint(t) {
127
- const e = Math.round(performance.now() - this.startTime);
128
- return u.debug(` ↳ ${t}`, f(c({}, this.context), { duration: e })), e;
228
+ checkpoint(label) {
229
+ const elapsed = Math.round(performance.now() - this.startTime);
230
+ logger.debug(` ↳ ${label}`, __spreadProps(__spreadValues({}, this.context), { duration: elapsed }));
231
+ return elapsed;
129
232
  }
130
233
  }
131
- function F(r) {
234
+ function createLogger(module) {
132
235
  return {
133
- debug(t, e, ...n) {
134
- u.debug(t, f(c({}, e), { module: r }), ...n);
236
+ debug(message, context, ...args) {
237
+ logger.debug(message, __spreadProps(__spreadValues({}, context), { module }), ...args);
135
238
  },
136
- info(t, e, ...n) {
137
- u.info(t, f(c({}, e), { module: r }), ...n);
239
+ info(message, context, ...args) {
240
+ logger.info(message, __spreadProps(__spreadValues({}, context), { module }), ...args);
138
241
  },
139
- warn(t, e, ...n) {
140
- u.warn(t, f(c({}, e), { module: r }), ...n);
242
+ warn(message, context, ...args) {
243
+ logger.warn(message, __spreadProps(__spreadValues({}, context), { module }), ...args);
141
244
  },
142
- error(t, e, ...n) {
143
- u.error(t, f(c({}, e), { module: r }), ...n);
245
+ error(message, context, ...args) {
246
+ logger.error(message, __spreadProps(__spreadValues({}, context), { module }), ...args);
144
247
  },
145
- timer(t, e) {
146
- return new R(t, f(c({}, e), { module: r }));
248
+ timer(operation, context) {
249
+ return new PerformanceTimer(operation, __spreadProps(__spreadValues({}, context), { module }));
147
250
  }
148
251
  };
149
252
  }
150
253
  export {
151
- F as c
254
+ createLogger as c
152
255
  };
@@ -1,25 +1,36 @@
1
- import { c as s } from "./logger.js";
2
- const e = s("OpenCodePluginPageContext");
3
- async function a(o) {
1
+ import { c as createLogger } from "./logger.js";
2
+ const log = createLogger("OpenCodePluginPageContext");
3
+ async function fetchPageContext(contextApiUrl) {
4
4
  try {
5
- const n = await fetch(o, {
5
+ const response = await fetch(contextApiUrl, {
6
6
  method: "GET",
7
7
  headers: { "Content-Type": "application/json" }
8
8
  });
9
- return n.ok ? await n.json() : (e.debug("Failed to fetch page context", { status: n.status }), null);
10
- } catch (n) {
11
- return e.debug("Error fetching page context", { error: n }), null;
9
+ if (!response.ok) {
10
+ log.debug("Failed to fetch page context", { status: response.status });
11
+ return null;
12
+ }
13
+ return await response.json();
14
+ } catch (error) {
15
+ log.debug("Error fetching page context", { error });
16
+ return null;
12
17
  }
13
18
  }
14
- const l = async () => {
15
- e.info("PageContextPlugin loading...");
16
- const o = process.env.OPENCODE_CONTEXT_API_URL;
17
- return e.debug("Context API URL:", { contextApiUrl: o }), o ? (e.info("Plugin initialized successfully"), {
18
- "experimental.chat.system.transform": async (n, r) => {
19
- e.debug("System transform hook called");
20
- const t = await a(o);
21
- e.debug("Page context fetched", { pageContext: t });
22
- const i = `
19
+ const PageContextPlugin = async () => {
20
+ log.info("PageContextPlugin loading...");
21
+ const contextApiUrl = process.env.OPENCODE_CONTEXT_API_URL;
22
+ log.debug("Context API URL:", { contextApiUrl });
23
+ if (!contextApiUrl) {
24
+ log.warn("OPENCODE_CONTEXT_API_URL is not set, page context plugin will not work");
25
+ return {};
26
+ }
27
+ log.info("Plugin initialized successfully");
28
+ return {
29
+ "experimental.chat.system.transform": async (_input, output) => {
30
+ log.debug("System transform hook called");
31
+ const pageContext = await fetchPageContext(contextApiUrl);
32
+ log.debug("Page context fetched", { pageContext });
33
+ const systemPrompt = `
23
34
  你是一个专业的前端开发助手,运行在 **OpenCode** 平台中,并通过 **vite-plugin-opencode-assistant** 插件集成到用户的 Vite 开发环境。
24
35
 
25
36
  ## ⚠️ 重要:页面上下文优先级规则
@@ -30,8 +41,8 @@ const l = async () => {
30
41
 
31
42
  **这里的上下文为最高优先级,任何情况下都不能被覆盖**
32
43
 
33
- - **页面 URL**: ${(t == null ? void 0 : t.url) || "未知"}
34
- - **页面标题**: ${(t == null ? void 0 : t.title) || "未知"}
44
+ - **页面 URL**: ${(pageContext == null ? void 0 : pageContext.url) || "未知"}
45
+ - **页面标题**: ${(pageContext == null ? void 0 : pageContext.title) || "未知"}
35
46
 
36
47
  **理解问题的优先级顺序:**
37
48
  1. **当前页面上下文**(最高优先级) - 根据用户当前所在页面的 URL 和标题理解问题背景
@@ -104,11 +115,11 @@ const l = async () => {
104
115
  4. **HTTP 请求成功判断(强制)**
105
116
  判断请求成功时,不要只看 HTTP 状态码!HTTP 状态码 200 并不代表业务逻辑成功。必须获取接口的详细响应内容,检查响应体中的业务状态码或错误信息。在确认请求成功之前,始终解析并检查响应体的完整内容
106
117
  `.trim();
107
- r.system.push(i);
118
+ output.system.push(systemPrompt);
108
119
  }
109
- }) : (e.warn("OPENCODE_CONTEXT_API_URL is not set, page context plugin will not work"), {});
120
+ };
110
121
  };
111
122
  export {
112
- l as PageContextPlugin,
113
- l as default
123
+ PageContextPlugin,
124
+ PageContextPlugin as default
114
125
  };
@@ -0,0 +1,3 @@
1
+ import type { Hooks } from "@opencode-ai/plugin";
2
+ export declare const ServiceLogsPlugin: () => Promise<Hooks>;
3
+ export default ServiceLogsPlugin;