opencode-sync-plugin 0.3.3 → 0.3.5
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 +1 -1
- package/dist/index.js +87 -14
- 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
|
[](https://www.npmjs.com/package/opencode-sync-plugin)
|
|
6
6
|
|
|
7
|
-
## OpenSync Ecosystem
|
|
7
|
+
## OpenSync Ecosystem link
|
|
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,23 +201,39 @@ var OpenCodeSyncPlugin = async ({ client }) => {
|
|
|
144
201
|
if (syncedSessions.has(sessionId)) return;
|
|
145
202
|
syncedSessions.add(sessionId);
|
|
146
203
|
}
|
|
147
|
-
if (event.type === "session.idle"
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
204
|
+
if (event.type === "session.idle") {
|
|
205
|
+
const localData = getLocalSessionData(sessionId);
|
|
206
|
+
if (localData) {
|
|
207
|
+
doSyncSession({
|
|
208
|
+
...props,
|
|
209
|
+
title: localData.title || props?.title,
|
|
210
|
+
slug: localData.slug || props?.slug,
|
|
211
|
+
modelID: localData.model || props?.modelID,
|
|
212
|
+
providerID: localData.provider || props?.providerID,
|
|
213
|
+
tokens: {
|
|
214
|
+
input: localData.promptTokens || 0,
|
|
215
|
+
output: localData.completionTokens || 0
|
|
216
|
+
},
|
|
217
|
+
cost: localData.cost || 0
|
|
151
218
|
});
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
title,
|
|
159
|
-
slug
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
if (client) {
|
|
222
|
+
try {
|
|
223
|
+
const response = await client.session.get({
|
|
224
|
+
path: { id: sessionId }
|
|
160
225
|
});
|
|
161
|
-
|
|
226
|
+
const sessionData = response?.data || response;
|
|
227
|
+
if (sessionData?.title || sessionData?.slug) {
|
|
228
|
+
doSyncSession({
|
|
229
|
+
...props,
|
|
230
|
+
title: sessionData.title,
|
|
231
|
+
slug: sessionData.slug
|
|
232
|
+
});
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
} catch {
|
|
162
236
|
}
|
|
163
|
-
} catch {
|
|
164
237
|
}
|
|
165
238
|
}
|
|
166
239
|
doSyncSession(props);
|