@alexsun-top/opencode-chat-room 0.1.3 → 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.
- package/CHANGELOG.md +15 -0
- package/README.md +17 -12
- package/README.zh-CN.md +17 -12
- package/index.ts +2 -0
- package/package.json +9 -4
- package/plugin.ts +129 -321
- package/src/center-client.ts +10 -572
- package/src/center-inbox.ts +106 -0
- package/src/center-wake.ts +118 -0
- package/src/plugin-notifications.ts +163 -0
- package/src/queue-transport.ts +129 -0
- package/src/store.ts +1 -0
- package/src/v1-plugin.ts +55 -0
- package/src/v2-plugin.ts +120 -0
package/src/v2-plugin.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode/plugin";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import type { QueueTransport } from "./center-client";
|
|
4
|
+
|
|
5
|
+
type CenterOptions = { center: string; token: string; directory: string };
|
|
6
|
+
export type V2Runtime = {
|
|
7
|
+
protocol: "v1" | "v2";
|
|
8
|
+
selfServerUrl: string;
|
|
9
|
+
selfDirectory: string;
|
|
10
|
+
transport: QueueTransport;
|
|
11
|
+
center: CenterOptions;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export function createV2Setup(deps: {
|
|
15
|
+
runtime(directory: string, transport: QueueTransport): V2Runtime;
|
|
16
|
+
run<T>(runtime: V2Runtime, callback: () => T): T;
|
|
17
|
+
execute(args: z.infer<ReturnType<typeof roomSchema>>, sessionID: string): Promise<string>;
|
|
18
|
+
observe(runtime: V2Runtime, sessionID: string): Promise<void>;
|
|
19
|
+
poll(sessions: ReadonlySet<string>, transport: QueueTransport, center: CenterOptions): Promise<void>;
|
|
20
|
+
wake(center: CenterOptions, transport: QueueTransport, sessions: ReadonlySet<string>): Promise<void>;
|
|
21
|
+
central: boolean;
|
|
22
|
+
}) {
|
|
23
|
+
return async (context: Plugin.Context): Promise<() => Promise<void>> => {
|
|
24
|
+
const sessions = new Set<string>();
|
|
25
|
+
const directory = context.location.directory;
|
|
26
|
+
const transport: QueueTransport = {
|
|
27
|
+
directory,
|
|
28
|
+
protocol: "v2",
|
|
29
|
+
promptQueue: (sessionID, id, text) => context.session.prompt({
|
|
30
|
+
sessionID, id, text, delivery: "queue", resume: true,
|
|
31
|
+
}),
|
|
32
|
+
};
|
|
33
|
+
const runtime = deps.runtime(directory, transport);
|
|
34
|
+
let toolRegistration: { dispose(): Promise<void> } | undefined;
|
|
35
|
+
let commandRegistration: { dispose(): Promise<void> } | undefined;
|
|
36
|
+
let promptRegistration: { dispose(): Promise<void> } | undefined;
|
|
37
|
+
let pollTimer: ReturnType<typeof setInterval> | undefined;
|
|
38
|
+
let wakeTimer: ReturnType<typeof setInterval> | undefined;
|
|
39
|
+
let polling = false;
|
|
40
|
+
let stopped = false;
|
|
41
|
+
const poll = async () => {
|
|
42
|
+
if (stopped || polling) return;
|
|
43
|
+
polling = true;
|
|
44
|
+
try {
|
|
45
|
+
await deps.run(runtime, () => deps.poll(sessions, transport, runtime.center));
|
|
46
|
+
} catch {
|
|
47
|
+
// Retry transient polling failures on the next interval.
|
|
48
|
+
} finally {
|
|
49
|
+
polling = false;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
promptRegistration = await context.session.hook("prompt", async (input) => {
|
|
55
|
+
sessions.add(input.sessionID);
|
|
56
|
+
await deps.run(runtime, () => deps.observe(runtime, input.sessionID));
|
|
57
|
+
});
|
|
58
|
+
toolRegistration = await context.tool.transform((editor) => editor.add({
|
|
59
|
+
name: "room",
|
|
60
|
+
description: "Chat room operations: create, join, leave and list rooms; send and poll messages; list members",
|
|
61
|
+
options: { codemode: false },
|
|
62
|
+
input: roomSchema(),
|
|
63
|
+
execute: async (args, toolContext) => {
|
|
64
|
+
sessions.add(toolContext.sessionID);
|
|
65
|
+
return deps.run(runtime, async () => {
|
|
66
|
+
await deps.observe(runtime, toolContext.sessionID);
|
|
67
|
+
return { content: [{ type: "text", text: await deps.execute(args, toolContext.sessionID) }] };
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
}));
|
|
71
|
+
commandRegistration = await context.command.transform((editor) => editor.add({
|
|
72
|
+
name: "room",
|
|
73
|
+
description: "Chat room operations",
|
|
74
|
+
execute: async ({ sessionID, prompt, delivery }) => {
|
|
75
|
+
sessions.add(sessionID);
|
|
76
|
+
await deps.run(runtime, () => deps.observe(runtime, sessionID));
|
|
77
|
+
await context.session.prompt({
|
|
78
|
+
sessionID,
|
|
79
|
+
text: `${prompt.text}\n\nThe user invoked /room. Use the room tool to perform this command.`,
|
|
80
|
+
files: prompt.files,
|
|
81
|
+
agents: prompt.agents,
|
|
82
|
+
skills: prompt.skills,
|
|
83
|
+
delivery,
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
}));
|
|
87
|
+
pollTimer = setInterval(() => void poll(), 5000);
|
|
88
|
+
if (deps.central) {
|
|
89
|
+
const wake = () => deps.run(runtime, () => deps.wake(runtime.center, transport, sessions)).catch(() => {});
|
|
90
|
+
void wake();
|
|
91
|
+
wakeTimer = setInterval(() => void wake(), 10000);
|
|
92
|
+
}
|
|
93
|
+
} catch (error) {
|
|
94
|
+
stopped = true;
|
|
95
|
+
if (pollTimer) clearInterval(pollTimer);
|
|
96
|
+
if (wakeTimer) clearInterval(wakeTimer);
|
|
97
|
+
await dispose(toolRegistration, commandRegistration, promptRegistration);
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
return async () => {
|
|
101
|
+
stopped = true;
|
|
102
|
+
if (pollTimer) clearInterval(pollTimer);
|
|
103
|
+
if (wakeTimer) clearInterval(wakeTimer);
|
|
104
|
+
await dispose(toolRegistration, commandRegistration, promptRegistration);
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function roomSchema() {
|
|
110
|
+
return z.object({
|
|
111
|
+
action: z.enum(["create", "join", "leave", "list", "send", "poll", "members"]),
|
|
112
|
+
name: z.string().max(64).optional(),
|
|
113
|
+
roomId: z.string().optional(),
|
|
114
|
+
text: z.string().max(2000).optional(),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function dispose(...registrations: ({ dispose(): Promise<void> } | undefined)[]) {
|
|
119
|
+
await Promise.all(registrations.map((registration) => registration?.dispose()));
|
|
120
|
+
}
|