Repository navigation
Expand file tree
/
Copy pathplugin.ts
More file actions
171 lines (158 loc) · 5.96 KB
/
Copy pathplugin.ts
File metadata and controls
171 lines (158 loc) · 5.96 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import type { Plugin } from "@opencode-ai/plugin";
import { parseAutopilotCommand } from "./command/parse.ts";
import { loadAutopilotConfig } from "./config/autopilot-config.ts";
import { runAction } from "./controller/actions.ts";
import { createCore, type NoticeVariant } from "./controller/core.ts";
import {
handleExecutionFailed,
handleExecutionInterrupted,
handleExecutionSucceeded,
handleSessionDeleted,
} from "./controller/reply.ts";
import { createEventHandler } from "./hooks/event-handler.ts";
import { createPermissionHook } from "./hooks/permission.ts";
import { buildAutopilotContextText } from "./hooks/system-transform.ts";
import { stripAutopilotMarker } from "./prompts/index.ts";
import { AutopilotRpc } from "./rpc.ts";
import { StorageStateStore } from "./state/persistence.ts";
import {
AUTOPILOT_TOOL_DESCRIPTION,
AUTOPILOT_TOOL_NAME,
autopilotToolInput,
normalizeAutopilotToolInput,
} from "./tools/autopilot.ts";
type Registration = { readonly dispose: () => Promise<void> };
export async function createAutopilotPlugin(ctx: Plugin.Context): Promise<Plugin.Cleanup> {
const directory = ctx.location.directory;
const config = await loadAutopilotConfig(directory);
const store = new StorageStateStore(ctx.storage);
// -- RPC: status method + notice/card events -------------------------------
// Feedback must never break the controller, so every emit is wrapped.
let rpc: Awaited<ReturnType<typeof ctx.rpc.register<typeof AutopilotRpc>>> | undefined;
const notice = (
sessionID: string,
title: string,
message: string,
variant: NoticeVariant,
): void => {
try {
void rpc?.events.emit("notice", { sessionID, title, message, variant }).catch(() => {});
} catch {
// Ignore feedback failures.
}
};
const card = (sessionID: string, title: string, text: string): void => {
try {
void rpc?.events.emit("card", { sessionID, title, text }).catch(() => {});
} catch {
// Ignore feedback failures.
}
};
const core = createCore({ config, directory, session: ctx.session, store, notice });
await core.recover();
try {
rpc = await ctx.rpc.register(AutopilotRpc, {
status: async (input) => {
// JSON-schema method inputs are untyped at the boundary.
const { sessionID } = input as { sessionID: string };
return {
text: core.statusText(sessionID),
active: core.getState(sessionID)?.mode === "ENABLED",
};
},
});
} catch {
// RPC is a feedback channel only; the controller works without it.
}
const registrations: Registration[] = [];
// -- /autopilot command: parse and act (start dispatches immediately) ------
registrations.push(
await ctx.command.transform((editor) => {
editor.add({
name: AUTOPILOT_TOOL_NAME,
description: AUTOPILOT_TOOL_DESCRIPTION,
execute: async ({ sessionID, prompt }) => {
const args = parseAutopilotCommand(prompt.text);
const result = await runAction(core, sessionID, args);
if (args.action === "status" || args.action === "help") {
card(sessionID, "Autopilot", result);
}
},
});
}),
);
// -- autopilot tool (same runAction) ---------------------------------------
registrations.push(
await ctx.tool.transform((editor) => {
editor.add({
name: AUTOPILOT_TOOL_NAME,
description: AUTOPILOT_TOOL_DESCRIPTION,
input: autopilotToolInput,
execute: async (input, context) => {
const args = normalizeAutopilotToolInput(input);
const result = await runAction(core, context.sessionID, args);
// Strip autopilot status markers inline so the model does not echo them.
return { content: stripAutopilotMarker(result) };
},
});
}),
);
// -- context hook: inject guidance while enabled ---------------------------
registrations.push(
await ctx.session.hook("context", (event) => {
const state = core.getState(event.sessionID);
if (!state) return;
const text = buildAutopilotContextText({
state,
history: core.getHistory(event.sessionID),
config,
});
if (text) event.system.push({ type: "text", text });
}),
);
// -- permission hook -------------------------------------------------------
const permissionHook = createPermissionHook({
getState: core.getState,
getPermissionMode: core.getPermissionMode,
onPermissionDenied: (sessionID, permission) => {
const tracking = core.getTracking(sessionID);
if (!tracking) return;
tracking.blockedByPermission = true;
tracking.permissionBlockMessage =
`Denied ${permission.action} ${permission.resources.join(", ")}`.trim();
core.recordHistory(sessionID, tracking.permissionBlockMessage);
},
});
registrations.push(await ctx.permission.hook("evaluate", permissionHook));
// -- event loop ------------------------------------------------------------
const handleEvent = createEventHandler({
onExecutionSucceeded: (sessionID, created) =>
handleExecutionSucceeded(core, sessionID, created),
onExecutionFailed: (sessionID, message) => handleExecutionFailed(core, sessionID, message),
onExecutionInterrupted: (sessionID, reason) =>
handleExecutionInterrupted(core, sessionID, reason),
onSessionDeleted: (sessionID) => handleSessionDeleted(core, sessionID),
});
const abort = new AbortController();
const loop = (async () => {
try {
for await (const event of ctx.event.subscribe({ signal: abort.signal })) {
try {
await handleEvent(event);
} catch {
// One bad event must not kill the loop.
}
}
} catch {
// Subscription ended (abort/shutdown).
}
})();
return async () => {
abort.abort();
await loop.catch(() => {});
for (const registration of registrations) {
await registration.dispose().catch(() => {});
}
await rpc?.dispose().catch(() => {});
};
}