opencode-sync-plugin 0.1.9 → 0.2.0

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.
@@ -5,7 +5,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
5
5
  throw Error('Dynamic require of "' + x + '" is not supported');
6
6
  });
7
7
 
8
- // src/index.ts
8
+ // src/config.ts
9
9
  function getConfigPaths() {
10
10
  const { homedir } = __require("os");
11
11
  const { join } = __require("path");
@@ -13,36 +13,30 @@ function getConfigPaths() {
13
13
  const configFile = join(configDir, "config.json");
14
14
  return { configDir, configFile };
15
15
  }
16
- function readConfigFile() {
16
+ function getConfig() {
17
17
  try {
18
18
  const { existsSync, readFileSync } = __require("fs");
19
19
  const { configFile } = getConfigPaths();
20
20
  if (!existsSync(configFile)) return null;
21
21
  const content = readFileSync(configFile, "utf8");
22
- return JSON.parse(content);
22
+ const config = JSON.parse(content);
23
+ if (!config || !config.convexUrl) return null;
24
+ return config;
23
25
  } catch {
24
26
  return null;
25
27
  }
26
28
  }
27
- function writeConfigFile(config) {
29
+ function setConfig(cfg) {
28
30
  try {
29
31
  const { existsSync, writeFileSync, mkdirSync } = __require("fs");
30
32
  const { configDir, configFile } = getConfigPaths();
31
33
  if (!existsSync(configDir)) {
32
34
  mkdirSync(configDir, { recursive: true });
33
35
  }
34
- writeFileSync(configFile, JSON.stringify(config, null, 2), "utf8");
36
+ writeFileSync(configFile, JSON.stringify(cfg, null, 2), "utf8");
35
37
  } catch {
36
38
  }
37
39
  }
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
40
  function clearConfig() {
47
41
  try {
48
42
  const { existsSync, writeFileSync } = __require("fs");
@@ -53,15 +47,9 @@ function clearConfig() {
53
47
  } catch {
54
48
  }
55
49
  }
56
- var OpenCodeSyncPlugin = async (ctx) => {
57
- return {};
58
- };
59
- var index_default = OpenCodeSyncPlugin;
60
50
 
61
51
  export {
62
52
  getConfig,
63
53
  setConfig,
64
- clearConfig,
65
- OpenCodeSyncPlugin,
66
- index_default
54
+ clearConfig
67
55
  };
package/dist/cli.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  clearConfig,
4
4
  getConfig,
5
5
  setConfig
6
- } from "./chunk-SLPXDOH3.js";
6
+ } from "./chunk-EFSO6MGO.js";
7
7
 
8
8
  // src/cli.ts
9
9
  import { readFileSync, existsSync } from "fs";
@@ -0,0 +1,9 @@
1
+ interface Config {
2
+ convexUrl: string;
3
+ apiKey: string;
4
+ }
5
+ declare function getConfig(): Config | null;
6
+ declare function setConfig(cfg: Config): void;
7
+ declare function clearConfig(): void;
8
+
9
+ export { clearConfig, getConfig, setConfig };
package/dist/config.js ADDED
@@ -0,0 +1,10 @@
1
+ import {
2
+ clearConfig,
3
+ getConfig,
4
+ setConfig
5
+ } from "./chunk-EFSO6MGO.js";
6
+ export {
7
+ clearConfig,
8
+ getConfig,
9
+ setConfig
10
+ };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- interface Config {
2
- convexUrl: string;
3
- apiKey: string;
4
- }
5
- declare function getConfig(): Config | null;
6
- declare function setConfig(cfg: Config): void;
7
- declare function clearConfig(): void;
8
- declare const OpenCodeSyncPlugin: (ctx: Record<string, unknown>) => Promise<{}>;
1
+ declare const OpenCodeSyncPlugin: (_ctx: Record<string, unknown>) => Promise<{
2
+ event: (input: {
3
+ event: {
4
+ type: string;
5
+ properties?: Record<string, unknown>;
6
+ };
7
+ }) => Promise<void>;
8
+ }>;
9
9
 
10
- export { OpenCodeSyncPlugin, clearConfig, OpenCodeSyncPlugin as default, getConfig, setConfig };
10
+ export { OpenCodeSyncPlugin as default };
package/dist/index.js CHANGED
@@ -1,14 +1,104 @@
1
1
  import {
2
- OpenCodeSyncPlugin,
3
- clearConfig,
4
- getConfig,
5
- index_default,
6
- setConfig
7
- } from "./chunk-SLPXDOH3.js";
2
+ getConfig
3
+ } from "./chunk-EFSO6MGO.js";
4
+
5
+ // src/index.ts
6
+ var syncedMessages = /* @__PURE__ */ new Set();
7
+ var syncedSessions = /* @__PURE__ */ new Set();
8
+ function extractText(content) {
9
+ if (typeof content === "string") return content;
10
+ if (Array.isArray(content)) {
11
+ return content.filter((p) => p.type === "text" && p.text).map((p) => p.text).join("\n");
12
+ }
13
+ return "";
14
+ }
15
+ function getTitle(session) {
16
+ const first = session.messages?.find((m) => m.role === "user");
17
+ if (first) {
18
+ const text = extractText(first.content);
19
+ if (text) return text.slice(0, 100) + (text.length > 100 ? "..." : "");
20
+ }
21
+ return "Untitled Session";
22
+ }
23
+ function doSyncSession(session) {
24
+ try {
25
+ const config = getConfig();
26
+ if (!config?.apiKey || !config?.convexUrl) return;
27
+ const url = config.convexUrl.replace(".convex.cloud", ".convex.site");
28
+ fetch(`${url}/sync/session`, {
29
+ method: "POST",
30
+ headers: { "Content-Type": "application/json", Authorization: `Bearer ${config.apiKey}` },
31
+ body: JSON.stringify({
32
+ externalId: session.id,
33
+ title: session.title || getTitle(session),
34
+ projectPath: session.cwd,
35
+ projectName: session.cwd?.split("/").pop(),
36
+ model: session.model,
37
+ provider: session.provider,
38
+ promptTokens: session.usage?.promptTokens || 0,
39
+ completionTokens: session.usage?.completionTokens || 0,
40
+ cost: session.usage?.cost || 0
41
+ })
42
+ }).catch(() => {
43
+ });
44
+ } catch {
45
+ }
46
+ }
47
+ function doSyncMessage(sessionId, message) {
48
+ try {
49
+ const config = getConfig();
50
+ if (!config?.apiKey || !config?.convexUrl) return;
51
+ const url = config.convexUrl.replace(".convex.cloud", ".convex.site");
52
+ fetch(`${url}/sync/message`, {
53
+ method: "POST",
54
+ headers: { "Content-Type": "application/json", Authorization: `Bearer ${config.apiKey}` },
55
+ body: JSON.stringify({
56
+ sessionExternalId: sessionId,
57
+ externalId: message.id,
58
+ role: message.role,
59
+ textContent: extractText(message.content),
60
+ model: message.model,
61
+ promptTokens: message.usage?.promptTokens,
62
+ completionTokens: message.usage?.completionTokens,
63
+ durationMs: message.duration
64
+ })
65
+ }).catch(() => {
66
+ });
67
+ } catch {
68
+ }
69
+ }
70
+ var OpenCodeSyncPlugin = async (_ctx) => {
71
+ return {
72
+ event: async (input) => {
73
+ try {
74
+ const { event } = input;
75
+ const props = event.properties;
76
+ if (event.type === "session.created" || event.type === "session.updated" || event.type === "session.idle") {
77
+ const session = props;
78
+ if (session?.id) {
79
+ if (event.type === "session.created" && syncedSessions.has(session.id)) return;
80
+ if (event.type === "session.created") syncedSessions.add(session.id);
81
+ doSyncSession(session);
82
+ }
83
+ }
84
+ if (event.type === "message.updated" || event.type === "message.part.updated") {
85
+ const messageProps = props;
86
+ const sessionId = messageProps?.sessionId;
87
+ const message = messageProps?.message;
88
+ if (sessionId && message?.id && !syncedMessages.has(message.id)) {
89
+ if (event.type === "message.part.updated" && message.status !== "completed" && message.role !== "user") {
90
+ return;
91
+ }
92
+ syncedMessages.add(message.id);
93
+ doSyncMessage(sessionId, message);
94
+ }
95
+ }
96
+ } catch {
97
+ }
98
+ }
99
+ };
100
+ };
101
+ var index_default = OpenCodeSyncPlugin;
8
102
  export {
9
- OpenCodeSyncPlugin,
10
- clearConfig,
11
- index_default as default,
12
- getConfig,
13
- setConfig
103
+ index_default as default
14
104
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-sync-plugin",
3
- "version": "0.1.9",
3
+ "version": "0.2.0",
4
4
  "description": "Sync your OpenCode sessions to the cloud",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -15,8 +15,8 @@
15
15
  "opencode-sync": "dist/cli.js"
16
16
  },
17
17
  "scripts": {
18
- "build": "tsup src/index.ts src/cli.ts --format esm --dts --clean",
19
- "dev": "tsup src/index.ts src/cli.ts --format esm --dts --watch"
18
+ "build": "tsup src/index.ts src/cli.ts src/config.ts --format esm --dts --clean",
19
+ "dev": "tsup src/index.ts src/cli.ts src/config.ts --format esm --dts --watch"
20
20
  },
21
21
  "files": [
22
22
  "dist"