-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathindex.ts
More file actions
142 lines (129 loc) · 4.5 KB
/
index.ts
File metadata and controls
142 lines (129 loc) · 4.5 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
import type { Plugin } from "@opencode-ai/plugin"
import { getConfig } from "./lib/config"
import {
compressDisabledByOpencode,
hasExplicitToolPermission,
type HostPermissionSnapshot,
} from "./lib/host-permissions"
import { Logger } from "./lib/logger"
import { createSessionState } from "./lib/state"
import { createCompressTool } from "./lib/tools"
import { PromptStore } from "./lib/prompts/store"
import {
createChatMessageTransformHandler,
createCommandExecuteHandler,
createSystemPromptHandler,
createTextCompleteHandler,
} from "./lib/hooks"
import { configureClientAuth, isSecureMode } from "./lib/auth"
const plugin: Plugin = (async (ctx) => {
const config = getConfig(ctx)
if (!config.enabled) {
return {}
}
const logger = new Logger(config.debug)
const state = createSessionState()
const prompts = new PromptStore(logger, ctx.directory, config.experimental.customPrompts)
const hostPermissions: HostPermissionSnapshot = {
global: undefined,
agents: {},
}
if (isSecureMode()) {
configureClientAuth(ctx.client)
// logger.info("Secure mode detected, configured client authentication")
}
logger.info("DCP initialized", {
strategies: config.strategies,
})
return {
"experimental.chat.system.transform": createSystemPromptHandler(
state,
logger,
config,
prompts,
),
"experimental.chat.messages.transform": createChatMessageTransformHandler(
ctx.client,
state,
logger,
config,
prompts,
hostPermissions,
) as any,
"chat.message": async (
input: {
sessionID: string
agent?: string
model?: { providerID: string; modelID: string }
messageID?: string
variant?: string
},
_output: any,
) => {
state.variant = input.variant
logger.debug("Cached variant from chat.message hook", { variant: input.variant })
},
"experimental.text.complete": createTextCompleteHandler(),
"command.execute.before": createCommandExecuteHandler(
ctx.client,
state,
logger,
config,
ctx.directory,
hostPermissions,
),
tool: {
...(config.compress.permission !== "deny" && {
compress: createCompressTool({
client: ctx.client,
state,
logger,
config,
workingDirectory: ctx.directory,
prompts,
}),
}),
},
config: async (opencodeConfig) => {
if (config.commands.enabled) {
opencodeConfig.command ??= {}
opencodeConfig.command["dcp"] = {
template: "",
description: "Show available DCP commands",
}
}
if (
config.compress.permission !== "deny" &&
compressDisabledByOpencode(opencodeConfig.permission)
) {
config.compress.permission = "deny"
}
const toolsToAdd: string[] = []
if (config.compress.permission !== "deny" && !config.experimental.allowSubAgents) {
toolsToAdd.push("compress")
}
if (toolsToAdd.length > 0) {
const existingPrimaryTools = opencodeConfig.experimental?.primary_tools ?? []
opencodeConfig.experimental = {
...opencodeConfig.experimental,
primary_tools: [...existingPrimaryTools, ...toolsToAdd],
}
}
if (!hasExplicitToolPermission(opencodeConfig.permission, "compress")) {
const permission = opencodeConfig.permission ?? {}
opencodeConfig.permission = {
...permission,
compress: config.compress.permission,
} as typeof permission
}
hostPermissions.global = opencodeConfig.permission
hostPermissions.agents = Object.fromEntries(
Object.entries(opencodeConfig.agent ?? {}).map(([name, agent]) => [
name,
agent?.permission,
]),
)
},
}
}) satisfies Plugin
export default plugin