Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
196 lines (180 loc) · 7 KB
/
Copy pathindex.js
File metadata and controls
196 lines (180 loc) · 7 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* dsh-lark-ctrl: a Lark/Feishu bridge for DeepSeek Harness.
*
* Host-only bundle. It sends run summaries and interactive question/approval
* cards through lark-cli, receives replies over lark-cli's long-lived event
* consumers, and feeds answers back into the same Cordis waterfalls the Web
* client uses.
*/
import { buildNoticeCard, fitCard } from './lib/cards.js'
import { createAnswerer } from './lib/answerer.js'
import { createArchive } from './lib/archive.js'
import { createBtw } from './lib/btw.js'
import { normalizeConfig } from './lib/config.js'
import { createEventBridge } from './lib/event-bridge.js'
import { createLarkCli } from './lib/lark-cli.js'
import { createNotifier } from './lib/notifier.js'
import { createRegistry } from './lib/registry.js'
import { createRouter } from './lib/router.js'
import { createStore } from './lib/store.js'
import { createTransport } from './lib/transport.js'
import { truncate } from './lib/util.js'
/** Loader row identity. */
export const name = 'dsh-lark-ctrl'
/**
* Services this Host half needs. `connection` and `typertGateway` are optional
* at call time: the first enables dsh-btw delegation, the second lets the
* fallback modes notice a live Web client.
*/
export const inject = ['sessions', 'agents', 'sessionController', 'sessionProjections', 'userQuestions']
const STARTUP_RETRY_MS = 60000
/**
* @param {import('@deepseek-ai/cordis').Context} ctx
* @param {unknown} rawConfig
*/
export function apply(ctx, rawConfig) {
const config = normalizeConfig(rawConfig)
const logger = createLogger(ctx, config)
for (const warning of config.warnings) logger.warn(`[lark] config: ${warning}`)
const store = createStore(config, logger)
const cli = createLarkCli(config, logger)
const transport = createTransport({ config, cli, logger })
const registry = createRegistry({ ctx, store, config, logger })
const notifier = createNotifier({ ctx, config, registry, transport, store, logger })
const answerer = createAnswerer({ ctx, config, registry, transport, store, logger })
const btw = createBtw({ ctx, config, registry, logger })
const archive = createArchive({ ctx, config, cli, registry, store, logger })
const router = createRouter({ ctx, config, registry, transport, answerer, btw, archive, store, logger })
let disposed = false
let registered = false
let retryTimer
let bridge
ctx.effect(() => {
return () => {
disposed = true
if (retryTimer !== undefined) clearTimeout(retryTimer)
bridge?.stop()
transport.close()
void store.dispose()
}
}, 'lark: lifecycle')
async function bootstrap() {
if (disposed) return
try {
await store.load()
const status = await cli.status()
if (disposed) return
const target = transport.initialize(status)
if (!registered) {
// Register answerers/notifiers only after the persisted push level is
// loaded, so L0 does not emit a card during the startup window.
notifier.register()
answerer.register()
registered = true
}
logger.info(
`[lark] ${status.version || 'lark-cli'} ready; target=${
target.targetChatId || target.targetUserId || '(none)'
}${target.userName ? ` (${target.userName})` : ''}${status.botReady ? '' : ' [bot identity not ready]'}`,
)
if (target.targetUserId === '' && target.targetChatId === '') {
throw new Error('no target: lark-cli has no logged-in user and targetUserId/targetChatId is empty')
}
if (status.botReady !== true && config.identity === 'bot') {
logger.warn('[lark] bot identity is not ready; sends will fail until the bot scope is authorized')
}
bridge = createEventBridge({
config,
cli,
store,
logger,
onMessage: (event) => router.handleMessage(event),
onCardAction: (event) => router.handleCardAction(event),
onState: (state) => onBridgeState(state),
})
bridge.start()
if (config.startupMessage) {
void sendStartupMessage(status, target).catch((error) =>
logger.warn(`[lark] startup message failed: ${String(error)}`),
)
}
} catch (error) {
logger.warn(`[lark] bootstrap failed: ${String(error)}`)
if (disposed) return
if (retryTimer !== undefined) clearTimeout(retryTimer)
retryTimer = setTimeout(() => {
retryTimer = undefined
void bootstrap()
}, STARTUP_RETRY_MS)
retryTimer.unref?.()
}
}
/**
* @param {{ messageReady: boolean, cardReady: boolean, cardHint: string, lastError: string }} state
*/
function onBridgeState(state) {
if (disposed) return
if (store.pushLevel() < 2) return
if (state.cardReady) {
logger.info('[lark] card callbacks are ready')
return
}
if (state.cardHint !== '' && !store.rememberEvent('card-hint-notified')) {
logger.warn('[lark] card callbacks are blocked; text-reply answering remains available')
void transport
.sendText(
`dsh-lark-ctrl: 飞书卡片回调尚未开通,按钮/表单暂不可用(文字回复答题仍可用)。\n开通:${state.cardHint}`,
)
.catch(() => {})
}
}
/**
* @param {Awaited<ReturnType<typeof cli.status>>} status
* @param {{ targetUserId: string, targetChatId: string, userName: string }} target
*/
async function sendStartupMessage(status, target) {
if (store.pushLevel() < 2) return
const lines = [
'**dsh-lark-ctrl 已上线**',
`- 推送目标:${target.targetChatId || target.targetUserId}${target.userName ? `(${target.userName})` : ''}`,
`- lark-cli:${truncate(status.version || 'unknown', 80)}`,
`- 插件版本:\`0.3.3\``,
`- 推送级别:\`L${store.pushLevel()}\``,
`- 答题模式:\`${config.answerMode}\``,
`- 审批:\`${config.approvalMode}\``,
'',
'发送 `/help` 查看指令;回复任意会话通知即可继续对话。',
]
const card = fitCard(buildNoticeCard(lines.join('\n')))
await transport.sendCard(card, { idempotencyKey: `startup:${process.pid}:${Date.now()}` })
}
// Bootstrap is async; the plugin fiber is registered immediately so the
// Host can compose and dispose the plugin deterministically.
void bootstrap()
}
/**
* @param {import('@deepseek-ai/cordis').Context} ctx
* @param {ReturnType<typeof normalizeConfig>} config
*/
function createLogger(ctx, config) {
const base = ctx.logger
const write = (level, message) => {
try {
if (typeof base?.[level] === 'function') base[level](message)
else if (level === 'error') console.error(message)
else if (level === 'warn') console.warn(message)
else console.log(message)
} catch {
// logging must never break the bridge
}
}
return {
info: (message) => write('info', message),
warn: (message) => write('warn', message),
error: (message) => write('error', message),
debug: (message) => {
if (config.debug) write('debug', message)
},
}
}
export { normalizeConfig }