-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
155 lines (134 loc) · 5.14 KB
/
index.ts
File metadata and controls
155 lines (134 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import type { Plugin } from "@opencode-ai/plugin";
import { createOpencodeClient as createV2Client } from "@opencode-ai/sdk/v2";
import { agent as orchestratorAgent } from "./agents/orchestrator.ts";
import { handleStableIdle } from "./child-session-idle-handler.ts";
import { ChildSessionErrorForwarder } from "./child-session-error-forwarder.ts";
import { SessionRegistry } from "./session-registry.ts";
import { PermissionForwardingStore } from "./permission-forwarding-store.ts";
import { createSessionCreateTool } from "./tools/session-create.ts";
import { createSessionListTool } from "./tools/session-list.ts";
import { createSessionPromptTool } from "./tools/session-prompt.ts";
import { createSessionStatusTool } from "./tools/session-status.ts";
import { SessionWorktreeManager } from "./worktrees/session-worktree-manager.ts";
const OpencodeCC: Plugin = async (input) => {
const client = createV2Client({
baseUrl: input.serverUrl.toString(),
directory: input.directory,
fetch: resolveFetchFromPluginClient(input.client),
});
const orchestratorDirectory = input.directory;
const registry = new SessionRegistry();
const permissionStore = new PermissionForwardingStore();
const pendingIdleTimers = new Map<string, ReturnType<typeof setTimeout>>();
const worktreeManager = new SessionWorktreeManager(client);
const errorForwarder = new ChildSessionErrorForwarder({
client,
registry,
defaultOrchestratorDirectory: orchestratorDirectory,
});
const sessionCreateTool = createSessionCreateTool(
client,
registry,
worktreeManager
);
const sessionListTool = createSessionListTool(registry);
const sessionPromptTool = createSessionPromptTool(client, registry);
const sessionStatusTool = createSessionStatusTool(client, registry);
return {
config: async (config) => {
config.agent = config.agent || {};
config.agent["orchestrator"] = orchestratorAgent;
},
event: async ({ event }) => {
if (event.type === "permission.updated") {
permissionStore.capturePermission(event.properties);
return;
}
if (event.type === "permission.replied") {
const permission = permissionStore.getPermission(
event.properties.permissionID
);
if (permission) {
const orchestratorSessionID =
registry.getOrchestratorSessionID(permission.sessionID) ??
permission.sessionID;
permissionStore.captureReply(
orchestratorSessionID,
permission,
event.properties.response
);
}
return;
}
if (event.type === "session.status") {
if (event.properties.status.type !== "busy") return;
const childSessionID = event.properties.sessionID;
const timer = pendingIdleTimers.get(childSessionID);
if (!timer) return;
clearTimeout(timer);
pendingIdleTimers.delete(childSessionID);
return;
}
if (event.type === "session.error") {
const childSessionID = event.properties.sessionID;
if (!childSessionID) return;
await errorForwarder.handleSessionError({
childSessionID,
error: event.properties.error,
});
return;
}
if (event.type !== "session.idle") return;
const childSessionID = event.properties.sessionID;
const orchestratorSessionID =
registry.getOrchestratorSessionID(childSessionID);
if (!orchestratorSessionID) return;
if (!registry.hasPendingForwardRequests(childSessionID)) return;
const existingTimer = pendingIdleTimers.get(childSessionID);
if (existingTimer) clearTimeout(existingTimer);
const timer = setTimeout(() => {
pendingIdleTimers.delete(childSessionID);
const childOrchestratorDirectory =
registry.getOrchestratorDirectory(childSessionID) ?? orchestratorDirectory;
void handleStableIdle({
client,
registry,
childSessionID,
orchestratorSessionID,
orchestratorDirectory: childOrchestratorDirectory,
});
}, 5000);
pendingIdleTimers.set(childSessionID, timer);
},
"permission.ask": async (permission, output) => {
const orchestratorSessionID = registry.getOrchestratorSessionID(
permission.sessionID
);
if (!orchestratorSessionID) return;
const forwardedStatus = permissionStore.getForwardedStatus(
orchestratorSessionID,
permission
);
if (forwardedStatus) output.status = forwardedStatus;
},
tool: {
session_create: sessionCreateTool,
session_list: sessionListTool,
session_prompt: sessionPromptTool,
session_status: sessionStatusTool,
},
};
};
export default OpencodeCC;
function resolveFetchFromPluginClient(
client: unknown
): typeof fetch | undefined {
const anyClient = client as any;
const underscoreFetch = anyClient?._client?.fetch;
if (typeof underscoreFetch === "function")
return underscoreFetch.bind(anyClient._client);
const directFetch = anyClient?.client?.fetch;
if (typeof directFetch === "function")
return directFetch.bind(anyClient.client);
return undefined;
}