@vite-plugin-opencode-assistant/opencode 1.0.29 → 1.0.31

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/web.js CHANGED
@@ -21,7 +21,7 @@ import { execa } from "execa";
21
21
  import fs from "fs";
22
22
  import { createRequire } from "module";
23
23
  import path from "path";
24
- import { createLogger } from "@vite-plugin-opencode-assistant/shared";
24
+ import { createLogger, getProcessLogBuffer } from "@vite-plugin-opencode-assistant/shared";
25
25
  const require2 = createRequire(path.join(process.cwd(), "package.json"));
26
26
  const packageDir = resolvePackageDir();
27
27
  const log = createLogger("OpenCodeWeb");
@@ -32,12 +32,8 @@ function prepareOpenCodeRuntime(cwd) {
32
32
  if (!fs.existsSync(pluginsDir)) {
33
33
  fs.mkdirSync(pluginsDir, { recursive: true });
34
34
  }
35
- const pluginSourcePath = resolvePluginSourcePath();
36
- const pluginTargetPath = path.join(pluginsDir, "page-context.js");
37
- if (!fs.existsSync(pluginSourcePath)) {
38
- throw new Error(`Page context plugin not found: ${pluginSourcePath}`);
39
- }
40
- fs.copyFileSync(pluginSourcePath, pluginTargetPath);
35
+ const sourcePluginsDir = resolveSourcePluginsDir();
36
+ copyPluginFiles(sourcePluginsDir, pluginsDir);
41
37
  const mcpConfigPath = path.join(cacheDir, "opencode.json");
42
38
  fs.writeFileSync(
43
39
  mcpConfigPath,
@@ -57,23 +53,28 @@ function prepareOpenCodeRuntime(cwd) {
57
53
  );
58
54
  log.debug("OpenCode runtime ready", {
59
55
  cacheDir,
60
- pluginTargetPath,
56
+ pluginsDir,
61
57
  mcpConfigPath
62
58
  });
63
59
  return cacheDir;
64
60
  }
65
61
  function startOpenCodeWeb(options) {
66
62
  var _a, _b;
67
- const { port, hostname, cwd, configDir, corsOrigins, contextApiUrl } = options;
63
+ const { port, hostname, cwd, configDir, corsOrigins, contextApiUrl, logsApiUrl } = options;
68
64
  const stateDir = createStateDirectory(cwd);
69
- const pluginPath = path.join(stateDir, "plugins", "page-context.js");
65
+ const pluginsDir = path.join(stateDir, "plugins");
66
+ const pluginPaths = [
67
+ path.join(pluginsDir, "page-context.js"),
68
+ path.join(pluginsDir, "vite-logs.js")
69
+ ].join(",");
70
70
  log.debug("Building process environment", {
71
71
  stateDir,
72
72
  configDir,
73
73
  contextApiUrl,
74
- pluginPath
74
+ logsApiUrl,
75
+ pluginPaths
75
76
  });
76
- const env = buildProcessEnv(stateDir, configDir, contextApiUrl, pluginPath);
77
+ const env = buildProcessEnv(stateDir, configDir, contextApiUrl, logsApiUrl, pluginPaths);
77
78
  const args = ["serve", "--port", String(port), "--hostname", hostname];
78
79
  if (corsOrigins && corsOrigins.length > 0) {
79
80
  corsOrigins.forEach((origin) => {
@@ -96,12 +97,14 @@ function startOpenCodeWeb(options) {
96
97
  const output = data.toString().trim();
97
98
  if (output) {
98
99
  log.debug("[OpenCode stdout]", { output });
100
+ getProcessLogBuffer().addOpenCodeStdout(output);
99
101
  }
100
102
  });
101
103
  (_b = proc.stderr) == null ? void 0 : _b.on("data", (data) => {
102
104
  const output = data.toString().trim();
103
105
  if (output) {
104
106
  log.warn("[OpenCode stderr]", { output });
107
+ getProcessLogBuffer().addOpenCodeStderr(output);
105
108
  }
106
109
  });
107
110
  return proc;
@@ -118,10 +121,10 @@ function resolvePackageDir() {
118
121
  const entryPath = require2.resolve("@vite-plugin-opencode-assistant/opencode");
119
122
  return path.resolve(path.dirname(entryPath), "..");
120
123
  }
121
- function resolvePluginSourcePath() {
124
+ function resolveSourcePluginsDir() {
122
125
  const candidatePaths = [
123
- path.join(packageDir, "es", "plugins", "page-context.js"),
124
- path.join(packageDir, "lib", "plugins", "page-context.js")
126
+ path.join(packageDir, "es", "plugins"),
127
+ path.join(packageDir, "lib", "plugins")
125
128
  ];
126
129
  for (const candidatePath of candidatePaths) {
127
130
  if (fs.existsSync(candidatePath)) {
@@ -130,7 +133,17 @@ function resolvePluginSourcePath() {
130
133
  }
131
134
  return candidatePaths[0];
132
135
  }
133
- function buildProcessEnv(stateDir, configDir, contextApiUrl, pluginPath) {
136
+ function copyPluginFiles(sourceDir, targetDir) {
137
+ const files = fs.readdirSync(sourceDir).filter((f) => f.endsWith(".js"));
138
+ for (const file of files) {
139
+ const sourcePath = path.join(sourceDir, file);
140
+ const targetPath = path.join(targetDir, file);
141
+ fs.copyFileSync(sourcePath, targetPath);
142
+ log.debug("Plugin file copied", { source: sourcePath, target: targetPath });
143
+ }
144
+ log.debug("All plugin files copied", { count: files.length, files });
145
+ }
146
+ function buildProcessEnv(stateDir, configDir, contextApiUrl, logsApiUrl, pluginPaths) {
134
147
  const env = __spreadProps(__spreadValues({}, Object.fromEntries(
135
148
  Object.entries(process.env).filter(([, v]) => v !== void 0)
136
149
  )), {
@@ -144,9 +157,13 @@ function buildProcessEnv(stateDir, configDir, contextApiUrl, pluginPath) {
144
157
  env.OPENCODE_CONTEXT_API_URL = contextApiUrl;
145
158
  log.debug("Set OPENCODE_CONTEXT_API_URL", { contextApiUrl });
146
159
  }
147
- if (pluginPath) {
148
- env.OPENCODE_PLUGINS = pluginPath;
149
- log.debug("Set OPENCODE_PLUGINS", { pluginPath });
160
+ if (logsApiUrl) {
161
+ env.OPENCODE_VITE_LOGS_API_URL = logsApiUrl;
162
+ log.debug("Set OPENCODE_VITE_LOGS_API_URL", { logsApiUrl });
163
+ }
164
+ if (pluginPaths) {
165
+ env.OPENCODE_PLUGINS = pluginPaths;
166
+ log.debug("Set OPENCODE_PLUGINS", { pluginPaths });
150
167
  }
151
168
  return env;
152
169
  }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @fileoverview Vite 开发服务器日志工具插件
3
+ * @description 为 OpenCode Agent 提供 get_vite_dev_logs 工具
4
+ */
5
+ import type { Plugin } from "@opencode-ai/plugin";
6
+ /**
7
+ * Vite 开发服务器日志工具插件
8
+ *
9
+ * 通过环境变量 OPENCODE_VITE_LOGS_API_URL 获取日志 API 地址
10
+ * Agent 可以调用 get_vite_dev_logs 工具获取 Vite 开发服务器的进程日志
11
+ */
12
+ export declare const ViteLogsPlugin: Plugin;
13
+ export default ViteLogsPlugin;
@@ -0,0 +1,146 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var __async = (__this, __arguments, generator) => {
19
+ return new Promise((resolve, reject) => {
20
+ var fulfilled = (value) => {
21
+ try {
22
+ step(generator.next(value));
23
+ } catch (e) {
24
+ reject(e);
25
+ }
26
+ };
27
+ var rejected = (value) => {
28
+ try {
29
+ step(generator.throw(value));
30
+ } catch (e) {
31
+ reject(e);
32
+ }
33
+ };
34
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
35
+ step((generator = generator.apply(__this, __arguments)).next());
36
+ });
37
+ };
38
+ var vite_logs_exports = {};
39
+ __export(vite_logs_exports, {
40
+ ViteLogsPlugin: () => ViteLogsPlugin,
41
+ default: () => vite_logs_default
42
+ });
43
+ module.exports = __toCommonJS(vite_logs_exports);
44
+ var import_plugin = require("@opencode-ai/plugin");
45
+ var import_shared = require("@vite-plugin-opencode-assistant/shared");
46
+ const log = (0, import_shared.createLogger)("OpenCodePluginViteLogs");
47
+ const ViteLogsPlugin = () => __async(null, null, function* () {
48
+ log.info("ViteLogsPlugin loading...");
49
+ const logsApiUrl = process.env.OPENCODE_VITE_LOGS_API_URL;
50
+ log.debug("Vite Logs API URL:", { logsApiUrl });
51
+ if (!logsApiUrl) {
52
+ log.warn("OPENCODE_VITE_LOGS_API_URL is not set, vite logs plugin will not work");
53
+ return {};
54
+ }
55
+ log.info("Plugin initialized successfully");
56
+ const getViteDevLogsTool = (0, import_plugin.tool)({
57
+ description: `\u83B7\u53D6 Vite \u5F00\u53D1\u670D\u52A1\u5668\u7684\u8FD0\u884C\u65E5\u5FD7\u3002
58
+
59
+ **\u4F55\u65F6\u4F7F\u7528\u6B64\u5DE5\u5177**\uFF1A
60
+ - \u7528\u6237\u62A5\u544A"\u9875\u9762\u6CA1\u66F4\u65B0"\u3001"\u70ED\u66F4\u65B0\u4E0D\u5DE5\u4F5C"\u3001"HMR \u5931\u6548"\u65F6
61
+ - \u6784\u5EFA\u62A5\u9519\u6216\u7F16\u8BD1\u5931\u8D25\uFF0C\u9700\u8981\u67E5\u770B\u8BE6\u7EC6\u9519\u8BEF\u4FE1\u606F
62
+ - \u9875\u9762\u767D\u5C4F\u3001\u6837\u5F0F\u4E22\u5931\u3001\u6A21\u5757\u52A0\u8F7D\u5931\u8D25\u7B49\u5F00\u53D1\u95EE\u9898
63
+ - \u7528\u6237\u63D0\u5230"\u5F00\u53D1\u670D\u52A1\u5668\u6709\u95EE\u9898"\u3001"vite \u62A5\u9519"
64
+ - \u9700\u8981\u786E\u8BA4\u6700\u8FD1\u7684\u6587\u4EF6\u53D8\u66F4\u662F\u5426\u88AB Vite \u6B63\u786E\u5904\u7406
65
+
66
+ **\u65E5\u5FD7\u5185\u5BB9**\uFF1A
67
+ - Vite HMR \u70ED\u66F4\u65B0\u65E5\u5FD7\uFF08\u54EA\u4E9B\u6587\u4EF6\u88AB\u66F4\u65B0\u3001\u66F4\u65B0\u72B6\u6001\uFF09
68
+ - \u6784\u5EFA\u7F16\u8BD1\u65E5\u5FD7\uFF08\u9519\u8BEF\u3001\u8B66\u544A\u3001\u6210\u529F\u4FE1\u606F\uFF09
69
+ - OpenCode Web \u8FDB\u7A0B\u8F93\u51FA
70
+ - \u63D2\u4EF6\u8FD0\u884C\u65E5\u5FD7
71
+
72
+ \u65E5\u5FD7\u4FDD\u5B58\u5728\u5185\u5B58\u7F13\u51B2\u533A\uFF08\u6700\u8FD1 500 \u6761\uFF09\u3002`,
73
+ args: {
74
+ level: import_plugin.tool.schema.string().optional().describe(
75
+ "\u65E5\u5FD7\u7EA7\u522B\u8FC7\u6EE4\uFF1Aerror(\u9519\u8BEF)\u3001warn(\u8B66\u544A)\u3001info(\u4FE1\u606F)\u3001debug(\u8C03\u8BD5)\u3001log(\u666E\u901A)\u3002\u591A\u4E2A\u7528\u9017\u53F7\u5206\u9694\uFF0C\u5982 'error,warn'"
76
+ ),
77
+ limit: import_plugin.tool.schema.number().int().min(1).max(200).optional().default(50).describe("\u8FD4\u56DE\u6761\u6570\uFF0C\u9ED8\u8BA4 50\uFF0C\u6700\u5927 200"),
78
+ source: import_plugin.tool.schema.string().optional().describe("\u6765\u6E90\u8FC7\u6EE4\uFF1Aconsole(\u63A7\u5236\u53F0)\u3001opencode-stdout(\u670D\u52A1\u8F93\u51FA)\u3001opencode-stderr(\u670D\u52A1\u9519\u8BEF)")
79
+ },
80
+ execute(args, context) {
81
+ return __async(this, null, function* () {
82
+ const { level, limit, source } = args;
83
+ log.debug("get_vite_dev_logs called", {
84
+ args,
85
+ sessionID: context.sessionID,
86
+ directory: context.directory
87
+ });
88
+ try {
89
+ const url = new URL(logsApiUrl);
90
+ if (level) url.searchParams.set("level", level);
91
+ if (limit) url.searchParams.set("limit", String(limit));
92
+ if (source) url.searchParams.set("source", source);
93
+ log.debug("Fetching logs from", { url: url.toString() });
94
+ const response = yield fetch(url.toString(), {
95
+ method: "GET",
96
+ headers: { Accept: "application/json" },
97
+ signal: context.abort
98
+ });
99
+ if (!response.ok) {
100
+ const errorText = yield response.text();
101
+ log.error("Failed to fetch logs", { status: response.status, error: errorText });
102
+ return `\u83B7\u53D6\u65E5\u5FD7\u5931\u8D25: HTTP ${response.status} - ${errorText}`;
103
+ }
104
+ const data = yield response.json();
105
+ log.debug("Logs fetched successfully", {
106
+ count: data.logs.length,
107
+ total: data.meta.total
108
+ });
109
+ if (data.logs.length === 0) {
110
+ return `\u5F53\u524D\u6CA1\u6709\u7B26\u5408\u6761\u4EF6\u7684\u65E5\u5FD7\uFF08\u7F13\u51B2\u533A\u5171 ${data.meta.total} \u6761\uFF09\u3002
111
+
112
+ \u5EFA\u8BAE\uFF1A
113
+ - \u4E0D\u6307\u5B9A\u53C2\u6570\u83B7\u53D6\u6240\u6709\u65E5\u5FD7
114
+ - \u4F7F\u7528 level=error,warn \u83B7\u53D6\u9519\u8BEF\u548C\u8B66\u544A`;
115
+ }
116
+ const formattedLogs = data.logs.map((entry) => {
117
+ const time = new Date(entry.timestamp).toLocaleTimeString();
118
+ const levelIcon = entry.level === "error" ? "\u274C" : entry.level === "warn" ? "\u26A0\uFE0F" : entry.level === "info" ? "\u2139\uFE0F" : "";
119
+ return `${time} ${levelIcon} ${entry.message}`;
120
+ }).join("\n");
121
+ return `Vite \u5F00\u53D1\u670D\u52A1\u5668\u65E5\u5FD7\uFF08${data.meta.returned}/${data.meta.total} \u6761\uFF09\uFF1A
122
+
123
+ ${formattedLogs}`;
124
+ } catch (error) {
125
+ const err = error;
126
+ if (context.abort.aborted) {
127
+ log.debug("Request aborted");
128
+ return "\u8BF7\u6C42\u5DF2\u53D6\u6D88";
129
+ }
130
+ log.error("Error fetching vite logs", { error: err });
131
+ return `\u83B7\u53D6\u65E5\u5FD7\u65F6\u53D1\u751F\u9519\u8BEF: ${err.message}`;
132
+ }
133
+ });
134
+ }
135
+ });
136
+ return {
137
+ tool: {
138
+ get_vite_dev_logs: getViteDevLogsTool
139
+ }
140
+ };
141
+ });
142
+ var vite_logs_default = ViteLogsPlugin;
143
+ // Annotate the CommonJS export names for ESM import in node:
144
+ 0 && (module.exports = {
145
+ ViteLogsPlugin
146
+ });
package/lib/web.js CHANGED
@@ -63,12 +63,8 @@ function prepareOpenCodeRuntime(cwd) {
63
63
  if (!import_fs.default.existsSync(pluginsDir)) {
64
64
  import_fs.default.mkdirSync(pluginsDir, { recursive: true });
65
65
  }
66
- const pluginSourcePath = resolvePluginSourcePath();
67
- const pluginTargetPath = import_path.default.join(pluginsDir, "page-context.js");
68
- if (!import_fs.default.existsSync(pluginSourcePath)) {
69
- throw new Error(`Page context plugin not found: ${pluginSourcePath}`);
70
- }
71
- import_fs.default.copyFileSync(pluginSourcePath, pluginTargetPath);
66
+ const sourcePluginsDir = resolveSourcePluginsDir();
67
+ copyPluginFiles(sourcePluginsDir, pluginsDir);
72
68
  const mcpConfigPath = import_path.default.join(cacheDir, "opencode.json");
73
69
  import_fs.default.writeFileSync(
74
70
  mcpConfigPath,
@@ -88,23 +84,28 @@ function prepareOpenCodeRuntime(cwd) {
88
84
  );
89
85
  log.debug("OpenCode runtime ready", {
90
86
  cacheDir,
91
- pluginTargetPath,
87
+ pluginsDir,
92
88
  mcpConfigPath
93
89
  });
94
90
  return cacheDir;
95
91
  }
96
92
  function startOpenCodeWeb(options) {
97
93
  var _a, _b;
98
- const { port, hostname, cwd, configDir, corsOrigins, contextApiUrl } = options;
94
+ const { port, hostname, cwd, configDir, corsOrigins, contextApiUrl, logsApiUrl } = options;
99
95
  const stateDir = createStateDirectory(cwd);
100
- const pluginPath = import_path.default.join(stateDir, "plugins", "page-context.js");
96
+ const pluginsDir = import_path.default.join(stateDir, "plugins");
97
+ const pluginPaths = [
98
+ import_path.default.join(pluginsDir, "page-context.js"),
99
+ import_path.default.join(pluginsDir, "vite-logs.js")
100
+ ].join(",");
101
101
  log.debug("Building process environment", {
102
102
  stateDir,
103
103
  configDir,
104
104
  contextApiUrl,
105
- pluginPath
105
+ logsApiUrl,
106
+ pluginPaths
106
107
  });
107
- const env = buildProcessEnv(stateDir, configDir, contextApiUrl, pluginPath);
108
+ const env = buildProcessEnv(stateDir, configDir, contextApiUrl, logsApiUrl, pluginPaths);
108
109
  const args = ["serve", "--port", String(port), "--hostname", hostname];
109
110
  if (corsOrigins && corsOrigins.length > 0) {
110
111
  corsOrigins.forEach((origin) => {
@@ -127,12 +128,14 @@ function startOpenCodeWeb(options) {
127
128
  const output = data.toString().trim();
128
129
  if (output) {
129
130
  log.debug("[OpenCode stdout]", { output });
131
+ (0, import_shared.getProcessLogBuffer)().addOpenCodeStdout(output);
130
132
  }
131
133
  });
132
134
  (_b = proc.stderr) == null ? void 0 : _b.on("data", (data) => {
133
135
  const output = data.toString().trim();
134
136
  if (output) {
135
137
  log.warn("[OpenCode stderr]", { output });
138
+ (0, import_shared.getProcessLogBuffer)().addOpenCodeStderr(output);
136
139
  }
137
140
  });
138
141
  return proc;
@@ -149,10 +152,10 @@ function resolvePackageDir() {
149
152
  const entryPath = require2.resolve("@vite-plugin-opencode-assistant/opencode");
150
153
  return import_path.default.resolve(import_path.default.dirname(entryPath), "..");
151
154
  }
152
- function resolvePluginSourcePath() {
155
+ function resolveSourcePluginsDir() {
153
156
  const candidatePaths = [
154
- import_path.default.join(packageDir, "es", "plugins", "page-context.js"),
155
- import_path.default.join(packageDir, "lib", "plugins", "page-context.js")
157
+ import_path.default.join(packageDir, "es", "plugins"),
158
+ import_path.default.join(packageDir, "lib", "plugins")
156
159
  ];
157
160
  for (const candidatePath of candidatePaths) {
158
161
  if (import_fs.default.existsSync(candidatePath)) {
@@ -161,7 +164,17 @@ function resolvePluginSourcePath() {
161
164
  }
162
165
  return candidatePaths[0];
163
166
  }
164
- function buildProcessEnv(stateDir, configDir, contextApiUrl, pluginPath) {
167
+ function copyPluginFiles(sourceDir, targetDir) {
168
+ const files = import_fs.default.readdirSync(sourceDir).filter((f) => f.endsWith(".js"));
169
+ for (const file of files) {
170
+ const sourcePath = import_path.default.join(sourceDir, file);
171
+ const targetPath = import_path.default.join(targetDir, file);
172
+ import_fs.default.copyFileSync(sourcePath, targetPath);
173
+ log.debug("Plugin file copied", { source: sourcePath, target: targetPath });
174
+ }
175
+ log.debug("All plugin files copied", { count: files.length, files });
176
+ }
177
+ function buildProcessEnv(stateDir, configDir, contextApiUrl, logsApiUrl, pluginPaths) {
165
178
  const env = __spreadProps(__spreadValues({}, Object.fromEntries(
166
179
  Object.entries(process.env).filter(([, v]) => v !== void 0)
167
180
  )), {
@@ -175,9 +188,13 @@ function buildProcessEnv(stateDir, configDir, contextApiUrl, pluginPath) {
175
188
  env.OPENCODE_CONTEXT_API_URL = contextApiUrl;
176
189
  log.debug("Set OPENCODE_CONTEXT_API_URL", { contextApiUrl });
177
190
  }
178
- if (pluginPath) {
179
- env.OPENCODE_PLUGINS = pluginPath;
180
- log.debug("Set OPENCODE_PLUGINS", { pluginPath });
191
+ if (logsApiUrl) {
192
+ env.OPENCODE_VITE_LOGS_API_URL = logsApiUrl;
193
+ log.debug("Set OPENCODE_VITE_LOGS_API_URL", { logsApiUrl });
194
+ }
195
+ if (pluginPaths) {
196
+ env.OPENCODE_PLUGINS = pluginPaths;
197
+ log.debug("Set OPENCODE_PLUGINS", { pluginPaths });
181
198
  }
182
199
  return env;
183
200
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vite-plugin-opencode-assistant/opencode",
3
- "version": "1.0.29",
3
+ "version": "1.0.31",
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.29"
25
+ "@vite-plugin-opencode-assistant/shared": "1.0.31"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@opencode-ai/plugin": "^1.3.15",