nestjs-a2a
Version:
NestJS module for creating Google Agent to Agent Server
141 lines (140 loc) • 8.13 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createA2AController = createA2AController;
const common_1 = require("@nestjs/common");
const common_2 = require("@nestjs/common");
const constant_1 = require("./constant");
const a2a_exception_1 = require("./interfaces/a2a.exception");
const a2a_types_1 = require("./interfaces/a2a.types");
const a2a_executor_1 = require("./services/a2a.executor");
const a2a_registry_1 = require("./services/a2a.registry");
function isValidJsonRpcRequest(body) {
return (typeof body === 'object' &&
body !== null &&
body.jsonrpc === '2.0' &&
typeof body.method === 'string' &&
(body.id === null ||
typeof body.id === 'string' ||
typeof body.id === 'number') &&
(body.params === undefined ||
typeof body.params === 'object' ||
Array.isArray(body.params)));
}
function createA2AController(basePath, guards) {
var A2AController_1;
let A2AController = A2AController_1 = class A2AController {
constructor(a2aOptions, a2aRegistry, a2aExecutor) {
this.a2aOptions = a2aOptions;
this.a2aRegistry = a2aRegistry;
this.a2aExecutor = a2aExecutor;
this.logger = new common_1.Logger(A2AController_1.name);
}
getAgentCard(request) {
var _a;
return Object.assign(Object.assign({}, this.a2aOptions.card), { url: (_a = this.a2aOptions.card.url) !== null && _a !== void 0 ? _a : this.getServerUrl(request), skills: this.a2aRegistry
.getAvailableSkills()
.map((skill) => skill.skill) });
}
getServerUrl(request) {
if (request.headers['x-forwarded-proto'] &&
request.headers['x-forwarded-host']) {
return (request.headers['x-forwarded-proto'] +
'://' +
request.headers['x-forwarded-host']);
}
return request.protocol + '://' + request.get('host');
}
agentHandler(body, response) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
try {
if (!isValidJsonRpcRequest(body)) {
throw a2a_exception_1.A2AException.invalidRequest('Invalid JSON-RPC request structure.');
}
switch (body.method) {
case a2a_types_1.TaskMethod.SEND:
return yield this.a2aExecutor.handleTaskSend(body);
case a2a_types_1.TaskMethod.SEND_SUBSCRIBE:
if ((_c = (_b = (_a = this.a2aOptions) === null || _a === void 0 ? void 0 : _a.card) === null || _b === void 0 ? void 0 : _b.capabilities) === null || _c === void 0 ? void 0 : _c.streaming) {
yield this.a2aExecutor.handleTaskSendSubscribe(body, response);
return Promise.resolve(null);
}
else {
throw a2a_exception_1.A2AException.unsupportedOperation(a2a_types_1.TaskMethod.SEND_SUBSCRIBE);
}
case a2a_types_1.TaskMethod.GET:
return yield this.a2aExecutor.handleTaskGet(body);
case a2a_types_1.TaskMethod.CANCEL:
return yield this.a2aExecutor.handleTaskCancel(body);
case a2a_types_1.TaskMethod.GET_PUSH_NOTIFICATION:
if ((_f = (_e = (_d = this.a2aOptions) === null || _d === void 0 ? void 0 : _d.card) === null || _e === void 0 ? void 0 : _e.capabilities) === null || _f === void 0 ? void 0 : _f.pushNotifications) {
return yield this.a2aExecutor.handleTaskGetPushNotification(body);
}
else {
throw a2a_exception_1.A2AException.unsupportedOperation(a2a_types_1.TaskMethod.GET_PUSH_NOTIFICATION);
}
case a2a_types_1.TaskMethod.RESUBSCRIBE:
if ((_j = (_h = (_g = this.a2aOptions) === null || _g === void 0 ? void 0 : _g.card) === null || _h === void 0 ? void 0 : _h.capabilities) === null || _j === void 0 ? void 0 : _j.streaming) {
return yield this.a2aExecutor.handleTaskResubscribe(body);
}
else {
throw a2a_exception_1.A2AException.unsupportedOperation(a2a_types_1.TaskMethod.RESUBSCRIBE);
}
default:
throw a2a_exception_1.A2AException.methodNotFound(body.method);
}
}
catch (error) {
if (!(error instanceof a2a_exception_1.A2AException)) {
error = a2a_exception_1.A2AException.internalError((_k = error.message) !== null && _k !== void 0 ? _k : 'Unknown error');
}
return a2a_types_1.JSONRPCResponse.error(error.toJSONRPCError());
}
});
}
};
__decorate([
(0, common_1.Get)('/.well-known/agent.json'),
__param(0, (0, common_1.Req)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Object)
], A2AController.prototype, "getAgentCard", null);
__decorate([
(0, common_1.UseGuards)(...(guards !== null && guards !== void 0 ? guards : [])),
(0, common_1.Get)(basePath !== null && basePath !== void 0 ? basePath : constant_1.DEFAULT_A2A_BASE_PATH),
__param(0, (0, common_1.Body)()),
__param(1, (0, common_1.Res)({ passthrough: true })),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], A2AController.prototype, "agentHandler", null);
A2AController = A2AController_1 = __decorate([
(0, common_2.Controller)(),
__param(0, (0, common_1.Inject)(constant_1.A2A_OPTIONS_TOKEN)),
__metadata("design:paramtypes", [Object, a2a_registry_1.A2ARegistry,
a2a_executor_1.A2AExecutor])
], A2AController);
return A2AController;
}