-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
145 lines (137 loc) · 5.62 KB
/
Copy pathplugin.js
File metadata and controls
145 lines (137 loc) · 5.62 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
import Schema from '@deepseek-ai/schemastery'
import { DWClient } from 'dingtalk-stream'
import { DingTalkStreamCore } from './index.js'
import { DshAgentAdapter } from './dsh-agent-adapter.js'
import { bindOfficialDingTalkStreamClient } from './official-stream-adapter.js'
import { createDingTalkWebhookTransport } from './webhook-transport.js'
export const name = 'relayloom'
export const inject = ['agents', 'agentPresets']
export const Config = Schema.object({
enabled: Schema.boolean().default(false),
clientIdEnv: Schema.string().default('DINGTALK_CLIENT_ID'),
clientSecretEnv: Schema.string().default('DINGTALK_CLIENT_SECRET'),
allowedUsers: Schema.array(Schema.string()).default([]),
preset: Schema.string(),
provider: Schema.string(),
model: Schema.string(),
cwd: Schema.string(),
maxTextChars: Schema.number().default(4_000),
deduplicationLimit: Schema.number().default(10_000),
approvalTtlMs: Schema.number().default(600_000),
webhookTimeoutMs: Schema.number().default(15_000),
maxMarkdownChars: Schema.number().default(3_500),
debug: Schema.boolean().default(false),
})
function envName(value, label) {
if (typeof value !== 'string' || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(value)) {
throw new Error(`${label} must be an environment-variable name`)
}
return value
}
function boundedInteger(value, fallback, min, max, label) {
const result = value ?? fallback
if (!Number.isInteger(result) || result < min || result > max) {
throw new Error(`${label} must be an integer between ${min} and ${max}`)
}
return result
}
export function normalizePluginConfig(input = {}) {
const config = {
enabled: input.enabled ?? false,
clientIdEnv: envName(input.clientIdEnv ?? 'DINGTALK_CLIENT_ID', 'clientIdEnv'),
clientSecretEnv: envName(input.clientSecretEnv ?? 'DINGTALK_CLIENT_SECRET', 'clientSecretEnv'),
allowedUsers: [...new Set(input.allowedUsers ?? [])],
preset: input.preset,
provider: input.provider,
model: input.model,
cwd: input.cwd,
maxTextChars: boundedInteger(input.maxTextChars, 4_000, 1, 20_000, 'maxTextChars'),
deduplicationLimit: boundedInteger(input.deduplicationLimit, 10_000, 1, 1_000_000, 'deduplicationLimit'),
approvalTtlMs: boundedInteger(input.approvalTtlMs, 600_000, 10_000, 3_600_000, 'approvalTtlMs'),
webhookTimeoutMs: boundedInteger(input.webhookTimeoutMs, 15_000, 1_000, 120_000, 'webhookTimeoutMs'),
maxMarkdownChars: boundedInteger(input.maxMarkdownChars, 3_500, 200, 10_000, 'maxMarkdownChars'),
debug: input.debug ?? false,
}
if (typeof config.enabled !== 'boolean' || typeof config.debug !== 'boolean') throw new Error('enabled and debug must be booleans')
if (config.enabled && config.allowedUsers.length === 0) throw new Error('enabled bridge requires a non-empty allowedUsers list')
if (config.allowedUsers.some((value) => typeof value !== 'string' || value.length === 0 || /[\0\r\n]/.test(value))) {
throw new Error('allowedUsers must contain non-empty single-line staff ids')
}
if (config.cwd !== undefined && (typeof config.cwd !== 'string' || !config.cwd.startsWith('/'))) {
throw new Error('cwd must be an absolute path')
}
return config
}
export async function mountDingTalkBridge(ctx, inputConfig = {}, dependencies = {}) {
const config = normalizePluginConfig(inputConfig)
if (!config.enabled) return { enabled: false, async dispose() {} }
const env = dependencies.env ?? process.env
const clientId = env[config.clientIdEnv]
const clientSecret = env[config.clientSecretEnv]
if (!clientId || !clientSecret) {
throw new Error(`DingTalk credentials are missing: set ${config.clientIdEnv} and ${config.clientSecretEnv}`)
}
const client = dependencies.client ?? new DWClient({
clientId,
clientSecret,
debug: config.debug,
autoReconnect: true,
maxPendingCallbackHandlers: 100,
})
const transport = dependencies.transport ?? createDingTalkWebhookTransport(client, {
fetchImpl: dependencies.fetchImpl,
timeoutMs: config.webhookTimeoutMs,
maxMarkdownChars: config.maxMarkdownChars,
})
const dsh = dependencies.dshAdapter ?? new DshAgentAdapter(ctx, {
preset: config.preset,
provider: config.provider,
model: config.model,
cwd: config.cwd,
approvalTtlMs: config.approvalTtlMs,
sendApproval: transport.sendApproval,
})
const core = dependencies.core ?? new DingTalkStreamCore({
runTurn: (input) => dsh.runTurn(input),
sendMarkdown: (message, text) => transport.sendMarkdown(message, text),
resolveApproval: (input) => dsh.resolveApproval(input),
cancel: (sessionKey) => dsh.cancel(sessionKey),
}, {
allowedUsers: config.allowedUsers,
maxTextChars: config.maxTextChars,
deduplicationLimit: config.deduplicationLimit,
})
const binding = bindOfficialDingTalkStreamClient(client, core, {
onProtocolError: (error) => ctx.logger?.warn?.(`dingtalk bridge protocol error: ${String(error)}`),
onTaskError: (error) => ctx.logger?.warn?.(`dingtalk bridge task error: ${String(error)}`),
})
try {
await client.connect()
if (client.connected === false) {
throw new Error('DingTalk Stream connection did not become ready')
}
} catch (error) {
binding.dispose()
client.disconnect()
await dsh.dispose()
throw error
}
let disposed = false
return {
enabled: true,
client,
core,
async dispose() {
if (disposed) return
disposed = true
binding.dispose()
client.disconnect()
await dsh.dispose()
},
}
}
export async function apply(ctx, inputConfig) {
const mounted = await mountDingTalkBridge(ctx, inputConfig)
ctx.effect(() => () => mounted.dispose())
}
export default apply