初始化Aries Agent REST服务器时遇TypeScript类型错误求助
修复Aries Framework REST服务器初始化的类型错误
问题重现
按照Aries Framework REST文档编写初始化代码后,运行时出现类型错误:
Argument of type 'Agent<any>' is not assignable to parameter of type 'Agent'. Type 'Agent<any>' is missing the following properties from type 'Agent': walletService, routingService, questionAnswer, actionMenu, registerDependenciests(2345)
原代码:
import { startServer } from '@aries-framework/rest' import { Agent } from '@aries-framework/core' import { agentDependencies } from '@aries-framework/node' // The startServer function requires an initialized agent and a port. // An example of how to setup an agent is located in the `samples` directory. const run = async () => { const agentConfig: InitConfig = { label: 'demo-agent-acme', walletConfig: { id: 'mainAcme', key: 'demoagentacme0000000000000000000', }, autoAcceptConnections: true, endpoints: ['http://localhost:3002'], logger: new ConsoleLogger(LogLevel.info), } // A new instance of an agent is created here const agent = new Agent({config: agentConfig, dependencies: agentDependencies}) await startServer(agent, { port: 3000 }) } // A Swagger (OpenAPI) definition is exposed on http://localhost:3000/docs run()
解决方案
错误根源是Agent实例未完成初始化、未注册核心模块,且类型导入不完整,按以下步骤修复:
1. 补全必要的类型与模块导入
从@aries-framework/core导入缺失的类型和核心模块集合:
import { startServer } from '@aries-framework/rest' import { Agent, InitConfig, ConsoleLogger, LogLevel, coreModules } from '@aries-framework/core' import { agentDependencies } from '@aries-framework/node'
2. 为Agent配置添加核心模块
coreModules包含REST服务器依赖的所有核心服务(walletService、routingService等),必须添加到Agent配置中:
const agentConfig: InitConfig = { label: 'demo-agent-acme', walletConfig: { id: 'mainAcme', key: 'demoagentacme0000000000000000000', }, autoAcceptConnections: true, endpoints: ['http://localhost:3002'], logger: new ConsoleLogger(LogLevel.info), modules: coreModules, // 新增核心模块配置 }
3. 初始化Agent实例
创建Agent后必须调用initialize()方法完成内部服务实例化,否则无法满足REST服务器的类型要求:
const agent = new Agent({ config: agentConfig, dependencies: agentDependencies }) await agent.initialize() // 新增初始化步骤 await startServer(agent, { port: 3000 })
完整修正代码
import { startServer } from '@aries-framework/rest' import { Agent, InitConfig, ConsoleLogger, LogLevel, coreModules } from '@aries-framework/core' import { agentDependencies } from '@aries-framework/node' const run = async () => { const agentConfig: InitConfig = { label: 'demo-agent-acme', walletConfig: { id: 'mainAcme', key: 'demoagentacme0000000000000000000', }, autoAcceptConnections: true, endpoints: ['http://localhost:3002'], logger: new ConsoleLogger(LogLevel.info), modules: coreModules, } const agent = new Agent({ config: agentConfig, dependencies: agentDependencies }) await agent.initialize() await startServer(agent, { port: 3000 }) console.log('REST server running on http://localhost:3000') } run().catch(console.error)
关键说明
coreModules是AFJ官方提供的预配置核心模块集合,包含REST API依赖的所有服务组件- Agent必须通过
initialize()完成初始化,否则内部服务不会被实例化,导致类型不匹配 - 确保所有依赖包(
@aries-framework/core、@aries-framework/node、@aries-framework/rest)版本一致,避免跨版本类型冲突
内容的提问来源于stack exchange,提问作者Miroslav Petrovic
相关产品推荐
相关产品推荐

