opencode-sync-plugin 0.2.4 → 0.2.7
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 +3 -1
- package/dist/cli.js +239 -9
- package/dist/index.d.ts +4 -9
- package/dist/index.js +163 -57
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ npm run build
|
|
|
27
27
|
|
|
28
28
|
### 1. Get your credentials
|
|
29
29
|
|
|
30
|
-
You need two things from your OpenSync deployment
|
|
30
|
+
You need two things from your OpenSync deployment
|
|
31
31
|
|
|
32
32
|
- **Convex URL**: Your deployment URL from the Convex dashboard (e.g., `https://your-project-123.convex.cloud`)
|
|
33
33
|
- **API Key**: Generated in the OpenSync dashboard at **Settings > API Key** (starts with `osk_`)
|
|
@@ -116,6 +116,8 @@ Data is stored in your Convex deployment. You can view, search, and share sessio
|
|
|
116
116
|
|---------|-------------|
|
|
117
117
|
| `opencode-sync login` | Configure with Convex URL and API Key |
|
|
118
118
|
| `opencode-sync verify` | Verify credentials and OpenCode config |
|
|
119
|
+
| `opencode-sync sync` | Test connectivity and create a test session |
|
|
120
|
+
| `opencode-sync sync --all` | Sync all local OpenCode sessions to the cloud |
|
|
119
121
|
| `opencode-sync logout` | Clear stored credentials |
|
|
120
122
|
| `opencode-sync status` | Show authentication status |
|
|
121
123
|
| `opencode-sync config` | Show current configuration |
|
package/dist/cli.js
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
} from "./chunk-JPPDGYOB.js";
|
|
7
7
|
|
|
8
8
|
// src/cli.ts
|
|
9
|
-
import { readFileSync, existsSync } from "fs";
|
|
9
|
+
import { readFileSync, existsSync, readdirSync } from "fs";
|
|
10
10
|
import { homedir } from "os";
|
|
11
11
|
import { join, dirname } from "path";
|
|
12
12
|
import { createInterface } from "readline";
|
|
@@ -41,6 +41,9 @@ async function main() {
|
|
|
41
41
|
case "config":
|
|
42
42
|
showConfig();
|
|
43
43
|
break;
|
|
44
|
+
case "sync":
|
|
45
|
+
await sync();
|
|
46
|
+
break;
|
|
44
47
|
case "version":
|
|
45
48
|
case "-v":
|
|
46
49
|
case "--version":
|
|
@@ -199,21 +202,246 @@ function showConfig() {
|
|
|
199
202
|
console.log(" API Key:", config.apiKey ? config.apiKey.slice(0, 8) + "..." + config.apiKey.slice(-4) : "Not set");
|
|
200
203
|
console.log();
|
|
201
204
|
}
|
|
205
|
+
async function sync() {
|
|
206
|
+
const syncAll = args.includes("--all");
|
|
207
|
+
const config = getConfig();
|
|
208
|
+
if (!config || !config.apiKey || !config.convexUrl) {
|
|
209
|
+
console.log("\n Status: Not configured\n");
|
|
210
|
+
console.log(" Run: opencode-sync login\n");
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const siteUrl = config.convexUrl.replace(".convex.cloud", ".convex.site");
|
|
214
|
+
if (syncAll) {
|
|
215
|
+
await syncAllSessions(siteUrl, config.apiKey);
|
|
216
|
+
} else {
|
|
217
|
+
await syncConnectivityTest(siteUrl, config.apiKey);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
async function syncConnectivityTest(siteUrl, apiKey) {
|
|
221
|
+
console.log("\n OpenSync Connectivity Test\n");
|
|
222
|
+
console.log(" Testing backend health...");
|
|
223
|
+
try {
|
|
224
|
+
const healthRes = await fetch(`${siteUrl}/health`);
|
|
225
|
+
if (healthRes.ok) {
|
|
226
|
+
const healthData = await healthRes.json();
|
|
227
|
+
console.log(" Health: OK");
|
|
228
|
+
console.log(" Response:", JSON.stringify(healthData));
|
|
229
|
+
} else {
|
|
230
|
+
console.log(" Health: FAILED");
|
|
231
|
+
console.log(" Status:", healthRes.status);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
} catch (e) {
|
|
235
|
+
console.log(" Health: FAILED");
|
|
236
|
+
console.log(" Error:", e instanceof Error ? e.message : String(e));
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
console.log();
|
|
240
|
+
console.log(" Testing sync endpoint...");
|
|
241
|
+
const testSessionId = `test-${Date.now()}`;
|
|
242
|
+
try {
|
|
243
|
+
const syncRes = await fetch(`${siteUrl}/sync/session`, {
|
|
244
|
+
method: "POST",
|
|
245
|
+
headers: {
|
|
246
|
+
"Content-Type": "application/json",
|
|
247
|
+
Authorization: `Bearer ${apiKey}`
|
|
248
|
+
},
|
|
249
|
+
body: JSON.stringify({
|
|
250
|
+
externalId: testSessionId,
|
|
251
|
+
title: "CLI Sync Test",
|
|
252
|
+
projectPath: process.cwd(),
|
|
253
|
+
projectName: process.cwd().split("/").pop(),
|
|
254
|
+
model: "test",
|
|
255
|
+
provider: "opencode-sync-cli",
|
|
256
|
+
promptTokens: 0,
|
|
257
|
+
completionTokens: 0,
|
|
258
|
+
cost: 0
|
|
259
|
+
})
|
|
260
|
+
});
|
|
261
|
+
if (syncRes.ok) {
|
|
262
|
+
const syncData = await syncRes.json();
|
|
263
|
+
console.log(" Sync: OK");
|
|
264
|
+
console.log(" Response:", JSON.stringify(syncData));
|
|
265
|
+
console.log();
|
|
266
|
+
console.log(" Test session created. Check your OpenSync dashboard.\n");
|
|
267
|
+
} else {
|
|
268
|
+
console.log(" Sync: FAILED");
|
|
269
|
+
console.log(" Status:", syncRes.status);
|
|
270
|
+
const text = await syncRes.text();
|
|
271
|
+
if (text) console.log(" Body:", text);
|
|
272
|
+
console.log();
|
|
273
|
+
}
|
|
274
|
+
} catch (e) {
|
|
275
|
+
console.log(" Sync: FAILED");
|
|
276
|
+
console.log(" Error:", e instanceof Error ? e.message : String(e));
|
|
277
|
+
console.log();
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
async function syncAllSessions(siteUrl, apiKey) {
|
|
281
|
+
console.log("\n OpenSync: Syncing All Local Sessions\n");
|
|
282
|
+
const opencodePath = join(homedir(), ".local", "share", "opencode", "storage");
|
|
283
|
+
const sessionPath = join(opencodePath, "session");
|
|
284
|
+
const messagePath = join(opencodePath, "message");
|
|
285
|
+
if (!existsSync(sessionPath)) {
|
|
286
|
+
console.log(" No OpenCode sessions found.");
|
|
287
|
+
console.log(" Expected path:", sessionPath);
|
|
288
|
+
console.log();
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
const sessions = [];
|
|
292
|
+
try {
|
|
293
|
+
const projectDirs = readdirSync(sessionPath, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name);
|
|
294
|
+
for (const projectDir of projectDirs) {
|
|
295
|
+
const projectSessionPath = join(sessionPath, projectDir);
|
|
296
|
+
const sessionFiles = readdirSync(projectSessionPath).filter((f) => f.endsWith(".json"));
|
|
297
|
+
for (const file of sessionFiles) {
|
|
298
|
+
try {
|
|
299
|
+
const content = readFileSync(join(projectSessionPath, file), "utf8");
|
|
300
|
+
const data = JSON.parse(content);
|
|
301
|
+
if (data.id) {
|
|
302
|
+
sessions.push({ file, data });
|
|
303
|
+
}
|
|
304
|
+
} catch {
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
} catch (e) {
|
|
309
|
+
console.log(" Error reading sessions:", e instanceof Error ? e.message : String(e));
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
console.log(` Found ${sessions.length} sessions
|
|
313
|
+
`);
|
|
314
|
+
if (sessions.length === 0) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
let syncedSessions = 0;
|
|
318
|
+
let syncedMessages = 0;
|
|
319
|
+
let failedSessions = 0;
|
|
320
|
+
for (const session of sessions) {
|
|
321
|
+
const { data } = session;
|
|
322
|
+
process.stdout.write(` Syncing: ${data.title || data.slug || data.id}... `);
|
|
323
|
+
let totalPromptTokens = 0;
|
|
324
|
+
let totalCompletionTokens = 0;
|
|
325
|
+
let totalCost = 0;
|
|
326
|
+
let model = "";
|
|
327
|
+
let provider = "";
|
|
328
|
+
const sessionMessagePath = join(messagePath, data.id);
|
|
329
|
+
const messages = [];
|
|
330
|
+
if (existsSync(sessionMessagePath)) {
|
|
331
|
+
try {
|
|
332
|
+
const messageFiles = readdirSync(sessionMessagePath).filter((f) => f.endsWith(".json"));
|
|
333
|
+
for (const msgFile of messageFiles) {
|
|
334
|
+
try {
|
|
335
|
+
const msgContent = readFileSync(join(sessionMessagePath, msgFile), "utf8");
|
|
336
|
+
const msgData = JSON.parse(msgContent);
|
|
337
|
+
if (msgData.id && msgData.sessionID === data.id) {
|
|
338
|
+
messages.push(msgData);
|
|
339
|
+
if (msgData.tokens) {
|
|
340
|
+
totalPromptTokens += msgData.tokens.input || 0;
|
|
341
|
+
totalCompletionTokens += msgData.tokens.output || 0;
|
|
342
|
+
}
|
|
343
|
+
if (msgData.cost) {
|
|
344
|
+
totalCost += msgData.cost;
|
|
345
|
+
}
|
|
346
|
+
if (msgData.modelID && !model) {
|
|
347
|
+
model = msgData.modelID;
|
|
348
|
+
}
|
|
349
|
+
if (msgData.providerID && !provider) {
|
|
350
|
+
provider = msgData.providerID;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
} catch {
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
} catch {
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
try {
|
|
360
|
+
const sessionRes = await fetch(`${siteUrl}/sync/session`, {
|
|
361
|
+
method: "POST",
|
|
362
|
+
headers: {
|
|
363
|
+
"Content-Type": "application/json",
|
|
364
|
+
Authorization: `Bearer ${apiKey}`
|
|
365
|
+
},
|
|
366
|
+
body: JSON.stringify({
|
|
367
|
+
externalId: data.id,
|
|
368
|
+
title: data.title || data.slug || "Untitled",
|
|
369
|
+
projectPath: data.directory,
|
|
370
|
+
projectName: data.directory?.split("/").pop(),
|
|
371
|
+
model: model || "unknown",
|
|
372
|
+
provider: provider || "opencode",
|
|
373
|
+
promptTokens: totalPromptTokens,
|
|
374
|
+
completionTokens: totalCompletionTokens,
|
|
375
|
+
cost: totalCost
|
|
376
|
+
})
|
|
377
|
+
});
|
|
378
|
+
if (!sessionRes.ok) {
|
|
379
|
+
console.log("FAILED");
|
|
380
|
+
failedSessions++;
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
syncedSessions++;
|
|
384
|
+
let msgCount = 0;
|
|
385
|
+
for (const msg of messages) {
|
|
386
|
+
try {
|
|
387
|
+
const msgRes = await fetch(`${siteUrl}/sync/message`, {
|
|
388
|
+
method: "POST",
|
|
389
|
+
headers: {
|
|
390
|
+
"Content-Type": "application/json",
|
|
391
|
+
Authorization: `Bearer ${apiKey}`
|
|
392
|
+
},
|
|
393
|
+
body: JSON.stringify({
|
|
394
|
+
sessionExternalId: data.id,
|
|
395
|
+
externalId: msg.id,
|
|
396
|
+
role: msg.role,
|
|
397
|
+
textContent: "",
|
|
398
|
+
// We don't have content in the message metadata files
|
|
399
|
+
model: msg.modelID,
|
|
400
|
+
promptTokens: msg.tokens?.input,
|
|
401
|
+
completionTokens: msg.tokens?.output,
|
|
402
|
+
durationMs: msg.time?.completed && msg.time?.created ? msg.time.completed - msg.time.created : void 0
|
|
403
|
+
})
|
|
404
|
+
});
|
|
405
|
+
if (msgRes.ok) {
|
|
406
|
+
msgCount++;
|
|
407
|
+
syncedMessages++;
|
|
408
|
+
}
|
|
409
|
+
} catch {
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
console.log(`OK (${msgCount} messages)`);
|
|
413
|
+
} catch (e) {
|
|
414
|
+
console.log("FAILED");
|
|
415
|
+
failedSessions++;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
console.log();
|
|
419
|
+
console.log(` Summary:`);
|
|
420
|
+
console.log(` Sessions synced: ${syncedSessions}`);
|
|
421
|
+
console.log(` Messages synced: ${syncedMessages}`);
|
|
422
|
+
if (failedSessions > 0) {
|
|
423
|
+
console.log(` Failed: ${failedSessions}`);
|
|
424
|
+
}
|
|
425
|
+
console.log();
|
|
426
|
+
console.log(" Check your OpenSync dashboard to view synced sessions.\n");
|
|
427
|
+
}
|
|
202
428
|
function help() {
|
|
203
429
|
const version = getVersion();
|
|
204
430
|
console.log(`
|
|
205
431
|
OpenSync CLI v${version}
|
|
206
432
|
|
|
207
|
-
Usage: opencode-sync <command>
|
|
433
|
+
Usage: opencode-sync <command> [options]
|
|
208
434
|
|
|
209
435
|
Commands:
|
|
210
|
-
login
|
|
211
|
-
verify
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
436
|
+
login Configure with Convex URL and API Key
|
|
437
|
+
verify Verify credentials and OpenCode config
|
|
438
|
+
sync Test connectivity and create a test session
|
|
439
|
+
sync --all Sync all local OpenCode sessions to the cloud
|
|
440
|
+
logout Clear stored credentials
|
|
441
|
+
status Show current authentication status
|
|
442
|
+
config Show current configuration
|
|
443
|
+
version Show version number
|
|
444
|
+
help Show this help message
|
|
217
445
|
|
|
218
446
|
Setup:
|
|
219
447
|
1. Go to your OpenSync dashboard Settings page
|
|
@@ -222,6 +450,8 @@ function help() {
|
|
|
222
450
|
4. Enter your Convex URL and API Key
|
|
223
451
|
5. Add plugin to opencode.json (see instructions after login)
|
|
224
452
|
6. Run: opencode-sync verify
|
|
453
|
+
7. Run: opencode-sync sync (to test connectivity)
|
|
454
|
+
8. Run: opencode-sync sync --all (to sync existing sessions)
|
|
225
455
|
`);
|
|
226
456
|
}
|
|
227
457
|
function prompt(question) {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
event: (input: {
|
|
3
|
-
event: {
|
|
4
|
-
type: string;
|
|
5
|
-
properties?: Record<string, unknown>;
|
|
6
|
-
};
|
|
7
|
-
}) => Promise<void>;
|
|
8
|
-
}>;
|
|
1
|
+
import { Plugin } from '@opencode-ai/plugin';
|
|
9
2
|
|
|
10
|
-
|
|
3
|
+
declare const OpenCodeSyncPlugin: Plugin;
|
|
4
|
+
|
|
5
|
+
export { OpenCodeSyncPlugin, OpenCodeSyncPlugin as default };
|
package/dist/index.js
CHANGED
|
@@ -3,102 +3,208 @@ import {
|
|
|
3
3
|
} from "./chunk-JPPDGYOB.js";
|
|
4
4
|
|
|
5
5
|
// src/index.ts
|
|
6
|
-
var syncedMessages = /* @__PURE__ */ new Set();
|
|
7
6
|
var syncedSessions = /* @__PURE__ */ new Set();
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
var syncedMessages = /* @__PURE__ */ new Set();
|
|
8
|
+
var messagePartsText = /* @__PURE__ */ new Map();
|
|
9
|
+
var messageMetadata = /* @__PURE__ */ new Map();
|
|
10
|
+
var syncTimeouts = /* @__PURE__ */ new Map();
|
|
11
|
+
var DEBOUNCE_MS = 800;
|
|
12
|
+
function inferRole(textContent) {
|
|
13
|
+
const assistantPatterns = [
|
|
14
|
+
/^(I'll|Let me|Here's|I can|I've|I'm going to|I will|Sure|Certainly|Of course)/i,
|
|
15
|
+
/```[\s\S]+```/,
|
|
16
|
+
// Code blocks
|
|
17
|
+
/^(Yes|No),?\s+(I|you|we|this|that)/i,
|
|
18
|
+
// Answering patterns
|
|
19
|
+
/\*\*[^*]+\*\*/,
|
|
20
|
+
// Bold markdown (explanations)
|
|
21
|
+
/^\d+\.\s+\*\*/
|
|
22
|
+
// Numbered lists with bold
|
|
23
|
+
];
|
|
24
|
+
const userPatterns = [
|
|
25
|
+
/\?$/,
|
|
26
|
+
// Questions
|
|
27
|
+
/^(create|fix|add|update|show|make|build|implement|write|delete|remove|change|modify|help|can you|please|I want|I need)/i,
|
|
28
|
+
/^@/
|
|
29
|
+
// File references
|
|
30
|
+
];
|
|
31
|
+
for (const pattern of assistantPatterns) {
|
|
32
|
+
if (pattern.test(textContent)) {
|
|
33
|
+
return "assistant";
|
|
34
|
+
}
|
|
12
35
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (first) {
|
|
18
|
-
const text = extractText(first.content);
|
|
19
|
-
if (text) return text.slice(0, 100) + (text.length > 100 ? "..." : "");
|
|
36
|
+
for (const pattern of userPatterns) {
|
|
37
|
+
if (pattern.test(textContent)) {
|
|
38
|
+
return "user";
|
|
39
|
+
}
|
|
20
40
|
}
|
|
21
|
-
return "
|
|
41
|
+
return textContent.length > 500 ? "assistant" : "user";
|
|
22
42
|
}
|
|
23
43
|
function doSyncSession(session) {
|
|
24
44
|
try {
|
|
25
45
|
const config = getConfig();
|
|
26
|
-
if (!config?.apiKey || !config?.convexUrl)
|
|
46
|
+
if (!config?.apiKey || !config?.convexUrl) {
|
|
47
|
+
console.error("[opencode-sync] Missing config - cannot sync session");
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
27
50
|
const url = config.convexUrl.replace(".convex.cloud", ".convex.site");
|
|
51
|
+
console.log("[opencode-sync] Syncing session:", session.id);
|
|
52
|
+
const projectPath = session.path?.cwd || session.cwd || session.directory;
|
|
53
|
+
const modelId = session.modelID || session.model?.modelID || session.model;
|
|
54
|
+
const providerId = session.providerID || session.model?.providerID || session.provider;
|
|
55
|
+
const promptTokens = session.tokens?.input || session.usage?.promptTokens || 0;
|
|
56
|
+
const completionTokens = session.tokens?.output || session.usage?.completionTokens || 0;
|
|
57
|
+
const cost = session.cost || session.usage?.cost || 0;
|
|
28
58
|
fetch(`${url}/sync/session`, {
|
|
29
59
|
method: "POST",
|
|
30
|
-
headers: {
|
|
60
|
+
headers: {
|
|
61
|
+
"Content-Type": "application/json",
|
|
62
|
+
Authorization: `Bearer ${config.apiKey}`
|
|
63
|
+
},
|
|
31
64
|
body: JSON.stringify({
|
|
32
65
|
externalId: session.id,
|
|
33
|
-
title: session.title ||
|
|
34
|
-
projectPath
|
|
35
|
-
projectName:
|
|
36
|
-
model:
|
|
37
|
-
provider:
|
|
38
|
-
promptTokens
|
|
39
|
-
completionTokens
|
|
40
|
-
cost
|
|
66
|
+
title: session.title || "Untitled Session",
|
|
67
|
+
projectPath,
|
|
68
|
+
projectName: projectPath?.split("/").pop(),
|
|
69
|
+
model: modelId,
|
|
70
|
+
provider: providerId,
|
|
71
|
+
promptTokens,
|
|
72
|
+
completionTokens,
|
|
73
|
+
cost
|
|
41
74
|
})
|
|
42
|
-
}).catch(() =>
|
|
43
|
-
|
|
44
|
-
|
|
75
|
+
}).then((r) => r.json()).then((data) => console.log("[opencode-sync] Session sync response:", data)).catch((err) => console.error("[opencode-sync] Session sync error:", err));
|
|
76
|
+
} catch (err) {
|
|
77
|
+
console.error("[opencode-sync] doSyncSession error:", err);
|
|
45
78
|
}
|
|
46
79
|
}
|
|
47
|
-
function doSyncMessage(sessionId,
|
|
80
|
+
function doSyncMessage(sessionId, messageId, role, textContent, metadata) {
|
|
48
81
|
try {
|
|
49
82
|
const config = getConfig();
|
|
50
|
-
if (!config?.apiKey || !config?.convexUrl)
|
|
83
|
+
if (!config?.apiKey || !config?.convexUrl) {
|
|
84
|
+
console.error("[opencode-sync] Missing config - cannot sync message");
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (!textContent || textContent.trim().length === 0) {
|
|
88
|
+
console.log("[opencode-sync] Skipping empty message:", messageId);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const finalRole = role === "unknown" || !role ? inferRole(textContent) : role;
|
|
51
92
|
const url = config.convexUrl.replace(".convex.cloud", ".convex.site");
|
|
93
|
+
console.log(
|
|
94
|
+
"[opencode-sync] Syncing message:",
|
|
95
|
+
messageId,
|
|
96
|
+
"role:",
|
|
97
|
+
finalRole,
|
|
98
|
+
"text length:",
|
|
99
|
+
textContent.length
|
|
100
|
+
);
|
|
101
|
+
let durationMs;
|
|
102
|
+
if (metadata?.time?.completed && metadata?.time?.created) {
|
|
103
|
+
durationMs = metadata.time.completed - metadata.time.created;
|
|
104
|
+
}
|
|
52
105
|
fetch(`${url}/sync/message`, {
|
|
53
106
|
method: "POST",
|
|
54
|
-
headers: {
|
|
107
|
+
headers: {
|
|
108
|
+
"Content-Type": "application/json",
|
|
109
|
+
Authorization: `Bearer ${config.apiKey}`
|
|
110
|
+
},
|
|
55
111
|
body: JSON.stringify({
|
|
56
112
|
sessionExternalId: sessionId,
|
|
57
|
-
externalId:
|
|
58
|
-
role:
|
|
59
|
-
textContent
|
|
60
|
-
model:
|
|
61
|
-
promptTokens:
|
|
62
|
-
completionTokens:
|
|
63
|
-
durationMs
|
|
113
|
+
externalId: messageId,
|
|
114
|
+
role: finalRole,
|
|
115
|
+
textContent,
|
|
116
|
+
model: metadata?.modelID,
|
|
117
|
+
promptTokens: metadata?.tokens?.input,
|
|
118
|
+
completionTokens: metadata?.tokens?.output,
|
|
119
|
+
durationMs
|
|
64
120
|
})
|
|
65
|
-
}).catch(() =>
|
|
66
|
-
|
|
67
|
-
|
|
121
|
+
}).then((r) => r.json()).then((data) => console.log("[opencode-sync] Message sync response:", data)).catch((err) => console.error("[opencode-sync] Message sync error:", err));
|
|
122
|
+
} catch (err) {
|
|
123
|
+
console.error("[opencode-sync] doSyncMessage error:", err);
|
|
68
124
|
}
|
|
69
125
|
}
|
|
70
|
-
|
|
126
|
+
function trySyncMessage(messageId) {
|
|
127
|
+
if (syncedMessages.has(messageId)) return;
|
|
128
|
+
const metadata = messageMetadata.get(messageId);
|
|
129
|
+
const textParts = messagePartsText.get(messageId);
|
|
130
|
+
if (!metadata || !textParts || textParts.length === 0) return;
|
|
131
|
+
const textContent = textParts.join("");
|
|
132
|
+
if (!textContent.trim()) return;
|
|
133
|
+
syncedMessages.add(messageId);
|
|
134
|
+
doSyncMessage(metadata.sessionId, messageId, metadata.role, textContent, metadata.info);
|
|
135
|
+
messagePartsText.delete(messageId);
|
|
136
|
+
messageMetadata.delete(messageId);
|
|
137
|
+
}
|
|
138
|
+
function scheduleSyncMessage(messageId) {
|
|
139
|
+
const existing = syncTimeouts.get(messageId);
|
|
140
|
+
if (existing) clearTimeout(existing);
|
|
141
|
+
const timeout = setTimeout(() => {
|
|
142
|
+
syncTimeouts.delete(messageId);
|
|
143
|
+
trySyncMessage(messageId);
|
|
144
|
+
}, DEBOUNCE_MS);
|
|
145
|
+
syncTimeouts.set(messageId, timeout);
|
|
146
|
+
}
|
|
147
|
+
var OpenCodeSyncPlugin = async (input) => {
|
|
148
|
+
console.log("[opencode-sync] Plugin initialized for project:", input.project?.id);
|
|
71
149
|
return {
|
|
72
|
-
event: async (
|
|
150
|
+
event: async ({ event }) => {
|
|
73
151
|
try {
|
|
74
|
-
const { event } = input;
|
|
75
152
|
const props = event.properties;
|
|
76
153
|
if (event.type === "session.created" || event.type === "session.updated" || event.type === "session.idle") {
|
|
77
|
-
const
|
|
78
|
-
if (
|
|
79
|
-
if (event.type === "session.created"
|
|
80
|
-
|
|
81
|
-
|
|
154
|
+
const sessionId = props?.id;
|
|
155
|
+
if (sessionId) {
|
|
156
|
+
if (event.type === "session.created") {
|
|
157
|
+
if (syncedSessions.has(sessionId)) return;
|
|
158
|
+
syncedSessions.add(sessionId);
|
|
159
|
+
}
|
|
160
|
+
doSyncSession(props);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (event.type === "message.updated") {
|
|
164
|
+
const info = props?.info;
|
|
165
|
+
if (info?.id && info?.sessionID && info?.role) {
|
|
166
|
+
console.log("[opencode-sync] Message metadata received:", info.id, "role:", info.role);
|
|
167
|
+
messageMetadata.set(info.id, {
|
|
168
|
+
role: info.role,
|
|
169
|
+
sessionId: info.sessionID,
|
|
170
|
+
info
|
|
171
|
+
});
|
|
172
|
+
if (messagePartsText.has(info.id)) {
|
|
173
|
+
scheduleSyncMessage(info.id);
|
|
174
|
+
}
|
|
82
175
|
}
|
|
83
176
|
}
|
|
84
|
-
if (event.type === "message.
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
177
|
+
if (event.type === "message.part.updated") {
|
|
178
|
+
const part = props?.part;
|
|
179
|
+
if (part?.type === "text" && part?.messageID && part?.sessionID) {
|
|
180
|
+
const messageId = part.messageID;
|
|
181
|
+
const text = part.text || "";
|
|
182
|
+
console.log(
|
|
183
|
+
"[opencode-sync] Text part received for message:",
|
|
184
|
+
messageId,
|
|
185
|
+
"length:",
|
|
186
|
+
text.length
|
|
187
|
+
);
|
|
188
|
+
messagePartsText.set(messageId, [text]);
|
|
189
|
+
if (!messageMetadata.has(messageId)) {
|
|
190
|
+
messageMetadata.set(messageId, {
|
|
191
|
+
role: "unknown",
|
|
192
|
+
// Will be inferred or updated from message.updated
|
|
193
|
+
sessionId: part.sessionID,
|
|
194
|
+
info: {}
|
|
195
|
+
});
|
|
91
196
|
}
|
|
92
|
-
|
|
93
|
-
doSyncMessage(sessionId, message);
|
|
197
|
+
scheduleSyncMessage(messageId);
|
|
94
198
|
}
|
|
95
199
|
}
|
|
96
|
-
} catch {
|
|
200
|
+
} catch (err) {
|
|
201
|
+
console.error("[opencode-sync] Event handler error:", err);
|
|
97
202
|
}
|
|
98
203
|
}
|
|
99
204
|
};
|
|
100
205
|
};
|
|
101
206
|
var index_default = OpenCodeSyncPlugin;
|
|
102
207
|
export {
|
|
208
|
+
OpenCodeSyncPlugin,
|
|
103
209
|
index_default as default
|
|
104
210
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-sync-plugin",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "Sync your OpenCode sessions to the cloud",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -36,12 +36,13 @@
|
|
|
36
36
|
},
|
|
37
37
|
"license": "MIT",
|
|
38
38
|
"devDependencies": {
|
|
39
|
+
"@opencode-ai/plugin": "^1.1.25",
|
|
39
40
|
"@types/node": "^20.0.0",
|
|
40
41
|
"tsup": "^8.0.0",
|
|
41
42
|
"typescript": "^5.3.0"
|
|
42
43
|
},
|
|
43
44
|
"peerDependencies": {
|
|
44
|
-
"@opencode-ai/plugin": ">=
|
|
45
|
+
"@opencode-ai/plugin": ">=1.0.0"
|
|
45
46
|
},
|
|
46
47
|
"peerDependenciesMeta": {
|
|
47
48
|
"@opencode-ai/plugin": {
|