opencode-sync-plugin 0.1.7 → 0.1.9

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.
@@ -0,0 +1,67 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ // src/index.ts
9
+ function getConfigPaths() {
10
+ const { homedir } = __require("os");
11
+ const { join } = __require("path");
12
+ const configDir = join(homedir(), ".config", "opencode-sync");
13
+ const configFile = join(configDir, "config.json");
14
+ return { configDir, configFile };
15
+ }
16
+ function readConfigFile() {
17
+ try {
18
+ const { existsSync, readFileSync } = __require("fs");
19
+ const { configFile } = getConfigPaths();
20
+ if (!existsSync(configFile)) return null;
21
+ const content = readFileSync(configFile, "utf8");
22
+ return JSON.parse(content);
23
+ } catch {
24
+ return null;
25
+ }
26
+ }
27
+ function writeConfigFile(config) {
28
+ try {
29
+ const { existsSync, writeFileSync, mkdirSync } = __require("fs");
30
+ const { configDir, configFile } = getConfigPaths();
31
+ if (!existsSync(configDir)) {
32
+ mkdirSync(configDir, { recursive: true });
33
+ }
34
+ writeFileSync(configFile, JSON.stringify(config, null, 2), "utf8");
35
+ } catch {
36
+ }
37
+ }
38
+ function getConfig() {
39
+ const config = readConfigFile();
40
+ if (!config || !config.convexUrl) return null;
41
+ return config;
42
+ }
43
+ function setConfig(cfg) {
44
+ writeConfigFile(cfg);
45
+ }
46
+ function clearConfig() {
47
+ try {
48
+ const { existsSync, writeFileSync } = __require("fs");
49
+ const { configFile } = getConfigPaths();
50
+ if (existsSync(configFile)) {
51
+ writeFileSync(configFile, "{}", "utf8");
52
+ }
53
+ } catch {
54
+ }
55
+ }
56
+ var OpenCodeSyncPlugin = async (ctx) => {
57
+ return {};
58
+ };
59
+ var index_default = OpenCodeSyncPlugin;
60
+
61
+ export {
62
+ getConfig,
63
+ setConfig,
64
+ clearConfig,
65
+ OpenCodeSyncPlugin,
66
+ index_default
67
+ };
package/dist/cli.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  clearConfig,
4
4
  getConfig,
5
5
  setConfig
6
- } from "./chunk-GVXT3A2F.js";
6
+ } from "./chunk-SLPXDOH3.js";
7
7
 
8
8
  // src/cli.ts
9
9
  import { readFileSync, existsSync } from "fs";
package/dist/index.d.ts CHANGED
@@ -1,30 +1,3 @@
1
- interface PluginClient {
2
- app: {
3
- log: (entry: {
4
- service: string;
5
- level: "debug" | "info" | "warn" | "error";
6
- message: string;
7
- extra?: Record<string, unknown>;
8
- }) => Promise<void>;
9
- };
10
- }
11
- interface PluginContext {
12
- project: unknown;
13
- client: PluginClient;
14
- $: unknown;
15
- directory: string;
16
- worktree: string;
17
- }
18
- interface PluginEvent {
19
- type: string;
20
- properties?: Record<string, unknown>;
21
- }
22
- interface PluginHooks {
23
- event?: (input: {
24
- event: PluginEvent;
25
- }) => Promise<void>;
26
- }
27
- type Plugin = (ctx: PluginContext) => Promise<PluginHooks>;
28
1
  interface Config {
29
2
  convexUrl: string;
30
3
  apiKey: string;
@@ -32,11 +5,6 @@ interface Config {
32
5
  declare function getConfig(): Config | null;
33
6
  declare function setConfig(cfg: Config): void;
34
7
  declare function clearConfig(): void;
35
- /**
36
- * OpenCode Sync Plugin
37
- * Syncs sessions and messages to cloud storage via Convex backend
38
- * Authentication: API Key (osk_*) from OpenSync Settings page
39
- */
40
- declare const OpenCodeSyncPlugin: Plugin;
8
+ declare const OpenCodeSyncPlugin: (ctx: Record<string, unknown>) => Promise<{}>;
41
9
 
42
10
  export { OpenCodeSyncPlugin, clearConfig, OpenCodeSyncPlugin as default, getConfig, setConfig };
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  getConfig,
5
5
  index_default,
6
6
  setConfig
7
- } from "./chunk-GVXT3A2F.js";
7
+ } from "./chunk-SLPXDOH3.js";
8
8
  export {
9
9
  OpenCodeSyncPlugin,
10
10
  clearConfig,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-sync-plugin",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "Sync your OpenCode sessions to the cloud",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -1,268 +0,0 @@
1
- // src/index.ts
2
- import { homedir } from "os";
3
- import { join } from "path";
4
- import { readFileSync, writeFileSync, mkdirSync, existsSync } from "fs";
5
- async function safeLog(client, entry) {
6
- try {
7
- await client?.app?.log?.(entry);
8
- } catch {
9
- }
10
- }
11
- var CONFIG_DIR = join(homedir(), ".config", "opencode-sync");
12
- var CONFIG_FILE = join(CONFIG_DIR, "config.json");
13
- function readConfigFile() {
14
- try {
15
- if (!existsSync(CONFIG_FILE)) return null;
16
- const content = readFileSync(CONFIG_FILE, "utf8");
17
- return JSON.parse(content);
18
- } catch {
19
- return null;
20
- }
21
- }
22
- function writeConfigFile(config) {
23
- try {
24
- if (!existsSync(CONFIG_DIR)) {
25
- mkdirSync(CONFIG_DIR, { recursive: true });
26
- }
27
- writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), "utf8");
28
- } catch {
29
- }
30
- }
31
- function getConfig() {
32
- const config = readConfigFile();
33
- if (!config || !config.convexUrl) return null;
34
- return config;
35
- }
36
- function setConfig(cfg) {
37
- writeConfigFile(cfg);
38
- }
39
- function clearConfig() {
40
- try {
41
- if (existsSync(CONFIG_FILE)) {
42
- writeFileSync(CONFIG_FILE, "{}", "utf8");
43
- }
44
- } catch {
45
- }
46
- }
47
- function getApiKey() {
48
- const cfg = getConfig();
49
- if (!cfg || !cfg.apiKey) return null;
50
- return cfg.apiKey;
51
- }
52
- function normalizeToSiteUrl(url) {
53
- if (url.includes(".convex.cloud")) {
54
- return url.replace(".convex.cloud", ".convex.site");
55
- }
56
- return url;
57
- }
58
- function getSiteUrl() {
59
- const cfg = getConfig();
60
- if (!cfg || !cfg.convexUrl) return null;
61
- return normalizeToSiteUrl(cfg.convexUrl);
62
- }
63
- async function syncSession(session, client) {
64
- const apiKey = getApiKey();
65
- const siteUrl = getSiteUrl();
66
- if (!apiKey || !siteUrl) {
67
- await safeLog(client, {
68
- service: "opencode-sync",
69
- level: "warn",
70
- message: "Not authenticated. Run: opencode-sync login"
71
- });
72
- return;
73
- }
74
- try {
75
- const response = await fetch(`${siteUrl}/sync/session`, {
76
- method: "POST",
77
- headers: {
78
- "Content-Type": "application/json",
79
- Authorization: `Bearer ${apiKey}`
80
- },
81
- body: JSON.stringify({
82
- externalId: session.id,
83
- title: session.title || extractTitle(session),
84
- projectPath: session.cwd,
85
- projectName: session.cwd?.split("/").pop(),
86
- model: session.model,
87
- provider: session.provider,
88
- promptTokens: session.usage?.promptTokens || 0,
89
- completionTokens: session.usage?.completionTokens || 0,
90
- cost: session.usage?.cost || 0
91
- })
92
- });
93
- if (!response.ok) {
94
- const errorText = await response.text();
95
- await safeLog(client, {
96
- service: "opencode-sync",
97
- level: "error",
98
- message: `Session sync failed: ${errorText}`
99
- });
100
- }
101
- } catch (e) {
102
- await safeLog(client, {
103
- service: "opencode-sync",
104
- level: "error",
105
- message: `Session sync error: ${e}`
106
- });
107
- }
108
- }
109
- async function syncMessage(sessionId, message, client) {
110
- const apiKey = getApiKey();
111
- const siteUrl = getSiteUrl();
112
- if (!apiKey || !siteUrl) return;
113
- const parts = extractParts(message.content);
114
- try {
115
- const response = await fetch(`${siteUrl}/sync/message`, {
116
- method: "POST",
117
- headers: {
118
- "Content-Type": "application/json",
119
- Authorization: `Bearer ${apiKey}`
120
- },
121
- body: JSON.stringify({
122
- sessionExternalId: sessionId,
123
- externalId: message.id,
124
- role: message.role,
125
- textContent: extractTextContent(message.content),
126
- model: message.model,
127
- promptTokens: message.usage?.promptTokens,
128
- completionTokens: message.usage?.completionTokens,
129
- durationMs: message.duration,
130
- parts
131
- })
132
- });
133
- if (!response.ok) {
134
- const errorText = await response.text();
135
- await safeLog(client, {
136
- service: "opencode-sync",
137
- level: "error",
138
- message: `Message sync failed: ${errorText}`
139
- });
140
- }
141
- } catch (e) {
142
- await safeLog(client, {
143
- service: "opencode-sync",
144
- level: "error",
145
- message: `Message sync error: ${e}`
146
- });
147
- }
148
- }
149
- function extractTitle(session) {
150
- const firstMessage = session.messages?.find((m) => m.role === "user");
151
- if (firstMessage) {
152
- const text = extractTextContent(firstMessage.content);
153
- if (text) {
154
- return text.slice(0, 100) + (text.length > 100 ? "..." : "");
155
- }
156
- }
157
- return "Untitled Session";
158
- }
159
- function extractTextContent(content) {
160
- if (typeof content === "string") return content;
161
- if (Array.isArray(content)) {
162
- return content.filter((p) => p.type === "text").map((p) => p.text).join("\n");
163
- }
164
- return "";
165
- }
166
- function extractParts(content) {
167
- if (typeof content === "string") {
168
- return [{ type: "text", content }];
169
- }
170
- if (Array.isArray(content)) {
171
- return content.map((part) => {
172
- if (part.type === "text") {
173
- return { type: "text", content: part.text };
174
- }
175
- if (part.type === "tool_use" || part.type === "tool-call") {
176
- const toolPart = part;
177
- return {
178
- type: "tool-call",
179
- content: { name: toolPart.name, args: toolPart.input || toolPart.args || {} }
180
- };
181
- }
182
- if (part.type === "tool_result" || part.type === "tool-result") {
183
- const resultPart = part;
184
- return {
185
- type: "tool-result",
186
- content: { result: resultPart.content || resultPart.result }
187
- };
188
- }
189
- return { type: part.type, content: part };
190
- });
191
- }
192
- return [];
193
- }
194
- var syncedMessages = /* @__PURE__ */ new Set();
195
- var syncedSessions = /* @__PURE__ */ new Set();
196
- var OpenCodeSyncPlugin = async ({ project, client, $, directory, worktree }) => {
197
- const cfg = getConfig();
198
- if (!cfg || !cfg.apiKey) {
199
- await safeLog(client, {
200
- service: "opencode-sync",
201
- level: "warn",
202
- message: "Not configured. Run: opencode-sync login"
203
- });
204
- } else {
205
- await safeLog(client, {
206
- service: "opencode-sync",
207
- level: "info",
208
- message: "Plugin initialized",
209
- extra: { directory, worktree }
210
- });
211
- }
212
- return {
213
- // Handle all events via generic event handler
214
- event: async ({ event }) => {
215
- const props = event.properties;
216
- if (event.type === "session.created") {
217
- const session = props;
218
- if (session?.id && !syncedSessions.has(session.id)) {
219
- syncedSessions.add(session.id);
220
- await syncSession(session, client);
221
- }
222
- }
223
- if (event.type === "session.updated") {
224
- const session = props;
225
- if (session?.id) {
226
- await syncSession(session, client);
227
- }
228
- }
229
- if (event.type === "session.idle") {
230
- const session = props;
231
- if (session?.id) {
232
- await syncSession(session, client);
233
- }
234
- }
235
- if (event.type === "message.updated") {
236
- const messageProps = props;
237
- const sessionId = messageProps?.sessionId;
238
- const message = messageProps?.message;
239
- if (sessionId && message?.id && !syncedMessages.has(message.id)) {
240
- syncedMessages.add(message.id);
241
- await syncMessage(sessionId, message, client);
242
- }
243
- }
244
- if (event.type === "message.part.updated") {
245
- const messageProps = props;
246
- const sessionId = messageProps?.sessionId;
247
- const message = messageProps?.message;
248
- if (sessionId && message?.id) {
249
- if (message.status === "completed" || message.role === "user") {
250
- if (!syncedMessages.has(message.id)) {
251
- syncedMessages.add(message.id);
252
- await syncMessage(sessionId, message, client);
253
- }
254
- }
255
- }
256
- }
257
- }
258
- };
259
- };
260
- var index_default = OpenCodeSyncPlugin;
261
-
262
- export {
263
- getConfig,
264
- setConfig,
265
- clearConfig,
266
- OpenCodeSyncPlugin,
267
- index_default
268
- };