opencode-sync-plugin 0.3.3 → 0.3.6

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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/dist/index.js +74 -14
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -4,7 +4,7 @@ Sync your OpenCode sessions to the cloud. Search, share, and access your coding
4
4
 
5
5
  [![npm version](https://img.shields.io/npm/v/opencode-sync-plugin.svg)](https://www.npmjs.com/package/opencode-sync-plugin)
6
6
 
7
- ## OpenSync Ecosystem
7
+ ## OpenSync Ecosystem
8
8
 
9
9
  | Package | Description | Links |
10
10
  |---------|-------------|-------|
package/dist/index.js CHANGED
@@ -3,7 +3,64 @@ import {
3
3
  } from "./chunk-J64QRI6W.js";
4
4
 
5
5
  // src/index.ts
6
+ import { existsSync, readFileSync, readdirSync } from "fs";
7
+ import { homedir } from "os";
8
+ import { join } from "path";
6
9
  var syncedSessions = /* @__PURE__ */ new Set();
10
+ function getLocalSessionData(sessionId) {
11
+ try {
12
+ const basePath = join(homedir(), ".local", "share", "opencode", "storage");
13
+ const sessionPath = join(basePath, "session");
14
+ const messagePath = join(basePath, "message", sessionId);
15
+ if (!existsSync(sessionPath)) return null;
16
+ let result = {};
17
+ const projectDirs = readdirSync(sessionPath, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name);
18
+ for (const projectDir of projectDirs) {
19
+ const sessionFile = join(sessionPath, projectDir, `${sessionId}.json`);
20
+ if (existsSync(sessionFile)) {
21
+ const content = readFileSync(sessionFile, "utf8");
22
+ const data = JSON.parse(content);
23
+ result.title = data.title;
24
+ result.slug = data.slug;
25
+ break;
26
+ }
27
+ }
28
+ if (existsSync(messagePath)) {
29
+ let totalPromptTokens = 0;
30
+ let totalCompletionTokens = 0;
31
+ let totalCost = 0;
32
+ const messageFiles = readdirSync(messagePath).filter(
33
+ (f) => f.endsWith(".json")
34
+ );
35
+ for (const msgFile of messageFiles) {
36
+ try {
37
+ const msgContent = readFileSync(join(messagePath, msgFile), "utf8");
38
+ const msgData = JSON.parse(msgContent);
39
+ if (msgData.tokens) {
40
+ totalPromptTokens += msgData.tokens.input || 0;
41
+ totalCompletionTokens += msgData.tokens.output || 0;
42
+ }
43
+ if (msgData.cost) {
44
+ totalCost += msgData.cost;
45
+ }
46
+ if (!result.model && msgData.modelID) {
47
+ result.model = msgData.modelID;
48
+ }
49
+ if (!result.provider && msgData.providerID) {
50
+ result.provider = msgData.providerID;
51
+ }
52
+ } catch {
53
+ }
54
+ }
55
+ result.promptTokens = totalPromptTokens;
56
+ result.completionTokens = totalCompletionTokens;
57
+ result.cost = totalCost;
58
+ }
59
+ return result;
60
+ } catch {
61
+ }
62
+ return null;
63
+ }
7
64
  var syncedMessages = /* @__PURE__ */ new Set();
8
65
  var messagePartsText = /* @__PURE__ */ new Map();
9
66
  var messageMetadata = /* @__PURE__ */ new Map();
@@ -144,24 +201,27 @@ var OpenCodeSyncPlugin = async ({ client }) => {
144
201
  if (syncedSessions.has(sessionId)) return;
145
202
  syncedSessions.add(sessionId);
146
203
  }
147
- if (event.type === "session.idle" && client) {
148
- try {
149
- const response = await client.session.get({
150
- path: { id: sessionId }
151
- });
152
- const sessionData = response?.data || response;
153
- const title = sessionData?.title;
154
- const slug = sessionData?.slug;
155
- if (title || slug) {
204
+ if (event.type === "session.idle") {
205
+ setTimeout(() => {
206
+ const localData = getLocalSessionData(sessionId);
207
+ if (localData && (localData.title || localData.slug)) {
156
208
  doSyncSession({
157
209
  ...props,
158
- title,
159
- slug
210
+ title: localData.title || props?.title,
211
+ slug: localData.slug || props?.slug,
212
+ modelID: localData.model || props?.modelID,
213
+ providerID: localData.provider || props?.providerID,
214
+ tokens: {
215
+ input: localData.promptTokens || 0,
216
+ output: localData.completionTokens || 0
217
+ },
218
+ cost: localData.cost || 0
160
219
  });
161
- return;
220
+ } else {
221
+ doSyncSession(props);
162
222
  }
163
- } catch {
164
- }
223
+ }, 1e3);
224
+ return;
165
225
  }
166
226
  doSyncSession(props);
167
227
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-sync-plugin",
3
- "version": "0.3.3",
3
+ "version": "0.3.6",
4
4
  "description": "Sync your OpenCode sessions to the OpenSync dashboard",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",