@fangyb/ahchat-bridge 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -6
- package/src/index.ts +0 -156
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fangyb/ahchat-bridge",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"files": ["dist"],
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "
|
|
7
|
-
"types": "src/index.ts",
|
|
6
|
+
"main": "dist/index.js",
|
|
8
7
|
"bin": {
|
|
9
8
|
"ahchat-bridge": "./dist/cli.js"
|
|
10
9
|
},
|
|
@@ -15,10 +14,8 @@
|
|
|
15
14
|
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
16
15
|
},
|
|
17
16
|
"dependencies": {
|
|
18
|
-
"@ahchat/logger": "workspace:*",
|
|
19
|
-
"@ahchat/shared": "workspace:*",
|
|
20
17
|
"@anthropic-ai/claude-agent-sdk": "^0.2",
|
|
21
|
-
"cac": "^
|
|
18
|
+
"cac": "^6.7.14",
|
|
22
19
|
"ws": "^8.18.0",
|
|
23
20
|
"zod": "^4.0.0"
|
|
24
21
|
},
|
package/src/index.ts
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import type { WSMessage } from '@ahchat/shared'
|
|
2
|
-
|
|
3
|
-
import { AgentManager } from './agentManager'
|
|
4
|
-
import { AskQuestionRegistry } from './askQuestionRegistry'
|
|
5
|
-
import { formatAnswerForSDK } from './askUserQuestionGuard'
|
|
6
|
-
import { HttpAgentRegistry } from './agentRegistry'
|
|
7
|
-
import { GroupRegistry } from './groupRegistry'
|
|
8
|
-
import { loadBridgeConfig, ensureDir } from './config'
|
|
9
|
-
import { ServerConnector } from './connector'
|
|
10
|
-
import { listModels } from './modelQuerier'
|
|
11
|
-
import { acquireLock } from './lockfile'
|
|
12
|
-
import { createModuleLogger } from './logger'
|
|
13
|
-
import { createGroupTaskDispatchHandler, createTaskDispatchHandler } from './messageHandler'
|
|
14
|
-
import { SessionStore } from './sessionStore'
|
|
15
|
-
import { wsMetrics } from './wsMetrics'
|
|
16
|
-
|
|
17
|
-
const logger = createModuleLogger('bridge')
|
|
18
|
-
|
|
19
|
-
async function main(): Promise<void> {
|
|
20
|
-
const config = loadBridgeConfig()
|
|
21
|
-
ensureDir(config.dataDir)
|
|
22
|
-
|
|
23
|
-
acquireLock(config.dataDir)
|
|
24
|
-
|
|
25
|
-
logger.info('Bridge starting', {
|
|
26
|
-
bridgeId: config.bridgeId,
|
|
27
|
-
serverUrl: config.serverUrl,
|
|
28
|
-
serverApiUrl: config.serverApiUrl,
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
wsMetrics.start(5_000)
|
|
32
|
-
|
|
33
|
-
const sessionStore = new SessionStore(config.dataDir)
|
|
34
|
-
const agentRegistry = new HttpAgentRegistry(config.serverApiUrl)
|
|
35
|
-
const groupRegistry = new GroupRegistry(config.serverApiUrl)
|
|
36
|
-
await agentRegistry.refresh()
|
|
37
|
-
await groupRegistry.refresh()
|
|
38
|
-
|
|
39
|
-
let connector: ServerConnector | null = null
|
|
40
|
-
|
|
41
|
-
const emit = (msg: WSMessage): void => {
|
|
42
|
-
connector?.send(msg)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const askQuestionRegistry = new AskQuestionRegistry()
|
|
46
|
-
const agentManager = new AgentManager(sessionStore, emit, {
|
|
47
|
-
queryConfig: config.queryConfig,
|
|
48
|
-
askQuestionRegistry,
|
|
49
|
-
groupRegistry,
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
const taskDispatchHandler = createTaskDispatchHandler(agentManager, agentRegistry, emit)
|
|
53
|
-
const groupTaskDispatchHandler = createGroupTaskDispatchHandler(agentManager, agentRegistry, emit)
|
|
54
|
-
|
|
55
|
-
let statusInterval: ReturnType<typeof setInterval> | null = null
|
|
56
|
-
|
|
57
|
-
connector = new ServerConnector({
|
|
58
|
-
config,
|
|
59
|
-
agentIds: () => agentRegistry.getAll().map((a) => a.id),
|
|
60
|
-
onTaskDispatch: taskDispatchHandler,
|
|
61
|
-
onGroupTaskDispatch: groupTaskDispatchHandler,
|
|
62
|
-
onStopGeneration: async (payload) => {
|
|
63
|
-
await agentManager.cancelReply(payload)
|
|
64
|
-
},
|
|
65
|
-
onConnected: async () => {
|
|
66
|
-
await agentRegistry.refresh()
|
|
67
|
-
await groupRegistry.refresh()
|
|
68
|
-
await agentManager.recoverFromRestart(agentRegistry.getAll())
|
|
69
|
-
},
|
|
70
|
-
onServerPush: async (msg) => {
|
|
71
|
-
switch (msg.type) {
|
|
72
|
-
case 'bridge:list_models_request': {
|
|
73
|
-
const { requestId } = msg.payload
|
|
74
|
-
logger.info('list_models request received', { requestId })
|
|
75
|
-
try {
|
|
76
|
-
const models = await listModels()
|
|
77
|
-
connector?.send({
|
|
78
|
-
type: 'bridge:list_models_response',
|
|
79
|
-
payload: { requestId, models },
|
|
80
|
-
})
|
|
81
|
-
logger.info('list_models response sent', { requestId, count: models.length })
|
|
82
|
-
} catch (e) {
|
|
83
|
-
const err = e instanceof Error ? e.message : String(e)
|
|
84
|
-
connector?.send({
|
|
85
|
-
type: 'bridge:list_models_response',
|
|
86
|
-
payload: { requestId, error: err },
|
|
87
|
-
})
|
|
88
|
-
logger.error('list_models failed', { requestId, error: e })
|
|
89
|
-
}
|
|
90
|
-
break
|
|
91
|
-
}
|
|
92
|
-
case 'agent:terminate':
|
|
93
|
-
await agentManager.terminate(msg.payload.agentId)
|
|
94
|
-
break
|
|
95
|
-
case 'agent:terminate_scope':
|
|
96
|
-
logger.info('agent:terminate_scope received', {
|
|
97
|
-
agentId: msg.payload.agentId,
|
|
98
|
-
scope: msg.payload.scope,
|
|
99
|
-
})
|
|
100
|
-
await agentManager.terminateScope(msg.payload.agentId, msg.payload.scope)
|
|
101
|
-
break
|
|
102
|
-
case 'agent:created':
|
|
103
|
-
case 'agent:updated':
|
|
104
|
-
agentRegistry.upsert(msg.payload.agent)
|
|
105
|
-
break
|
|
106
|
-
case 'agent:deleted':
|
|
107
|
-
agentRegistry.remove(msg.payload.agentId)
|
|
108
|
-
break
|
|
109
|
-
case 'user:answer_question': {
|
|
110
|
-
const p = msg.payload
|
|
111
|
-
const answerText = formatAnswerForSDK(p)
|
|
112
|
-
const ok = askQuestionRegistry.resolve(p.questionId, answerText)
|
|
113
|
-
logger.info('user:answer_question handled', {
|
|
114
|
-
questionId: p.questionId,
|
|
115
|
-
agentId: p.agentId,
|
|
116
|
-
resolved: ok,
|
|
117
|
-
traceId: p.traceId,
|
|
118
|
-
})
|
|
119
|
-
break
|
|
120
|
-
}
|
|
121
|
-
default:
|
|
122
|
-
break
|
|
123
|
-
}
|
|
124
|
-
},
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
connector.connect()
|
|
128
|
-
|
|
129
|
-
statusInterval = setInterval(() => {
|
|
130
|
-
if (!connector?.isConnected) return
|
|
131
|
-
void agentRegistry.refresh().then(() => {
|
|
132
|
-
connector?.send(agentManager.getQueryStatus(config.bridgeId))
|
|
133
|
-
})
|
|
134
|
-
}, config.queryConfig.statusReportIntervalMs)
|
|
135
|
-
|
|
136
|
-
const shutdown = async (signal: string) => {
|
|
137
|
-
logger.info('Shutdown signal received', { signal })
|
|
138
|
-
if (statusInterval) {
|
|
139
|
-
clearInterval(statusInterval)
|
|
140
|
-
statusInterval = null
|
|
141
|
-
}
|
|
142
|
-
wsMetrics.stop()
|
|
143
|
-
connector?.close()
|
|
144
|
-
await agentManager.shutdownAll()
|
|
145
|
-
logger.info('Bridge stopped')
|
|
146
|
-
process.exit(0)
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
process.on('SIGINT', () => void shutdown('SIGINT'))
|
|
150
|
-
process.on('SIGTERM', () => void shutdown('SIGTERM'))
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
void main().catch((e) => {
|
|
154
|
-
logger.error('Bridge failed to start', { error: e })
|
|
155
|
-
process.exit(1)
|
|
156
|
-
})
|