|
| 1 | +import { IMaintenanceOperation, OperationResultType } from "../../OperationAbstractions.js"; |
| 2 | +import { Stream } from "node:stream"; |
| 3 | +import type { AiAgentConfiguration } from "./config/AiAgentConfiguration.js"; |
| 4 | +import type { AiAgentConfigurationResult } from "./AiAgentConfigurationResult.js"; |
| 5 | +import { RavenCommand } from "../../../../Http/RavenCommand.js"; |
| 6 | +import { DocumentConventions } from "../../../Conventions/DocumentConventions.js"; |
| 7 | +import { IRaftCommand } from "../../../../Http/IRaftCommand.js"; |
| 8 | +import { ServerNode } from "../../../../Http/ServerNode.js"; |
| 9 | +import { HttpRequestParameters } from "../../../../Primitives/Http.js"; |
| 10 | +import { RaftIdGenerator } from "../../../../Utility/RaftIdGenerator.js"; |
| 11 | +import { throwError } from "../../../../Exceptions/index.js"; |
| 12 | +import { ObjectUtil } from "../../../../Utility/ObjectUtil.js"; |
| 13 | +import { JsonSerializer } from "../../../../Mapping/Json/Serializer.js"; |
| 14 | +import { HeadersBuilder } from "../../../../Utility/HttpUtil.js"; |
| 15 | + |
| 16 | +function hasNoSampleObjectOrSchema(configuration: AiAgentConfiguration) { |
| 17 | + return (!configuration.outputSchema || configuration.outputSchema.trim() === "") |
| 18 | + && (!configuration.sampleObject || configuration.sampleObject.trim() === ""); |
| 19 | +} |
| 20 | + |
| 21 | +export class AddOrUpdateAiAgentOperation implements IMaintenanceOperation<AiAgentConfigurationResult> { |
| 22 | + private readonly _configuration: AiAgentConfiguration; |
| 23 | + private readonly _sampleObject?: unknown; |
| 24 | + |
| 25 | + public constructor(configuration: AiAgentConfiguration, schemaType?: any) { |
| 26 | + if (!configuration) { |
| 27 | + throwError("InvalidArgumentException", "configuration cannot be null or undefined."); |
| 28 | + } |
| 29 | + |
| 30 | + if (!configuration.outputSchema && !configuration.sampleObject && !schemaType) { |
| 31 | + throwError("InvalidArgumentException", "Please provide a non-empty value for either outputSchema or sampleObject."); |
| 32 | + } |
| 33 | + this._configuration = configuration; |
| 34 | + if (schemaType) { |
| 35 | + this._sampleObject = schemaType; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public get resultType(): OperationResultType { |
| 40 | + return "CommandResult"; |
| 41 | + } |
| 42 | + |
| 43 | + public getCommand(conventions: DocumentConventions): RavenCommand<AiAgentConfigurationResult> { |
| 44 | + return new AddOrUpdateAiAgentCommand(this._configuration, this._sampleObject, conventions); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +class AddOrUpdateAiAgentCommand extends RavenCommand<AiAgentConfigurationResult> implements IRaftCommand { |
| 50 | + private readonly _configuration: AiAgentConfiguration; |
| 51 | + private readonly _conventions: DocumentConventions; |
| 52 | + private readonly _sampleSchema?: any; |
| 53 | + |
| 54 | + public constructor(configuration: AiAgentConfiguration, sampleSchema: any, conventions: DocumentConventions) { |
| 55 | + super(); |
| 56 | + if (hasNoSampleObjectOrSchema(configuration)) { |
| 57 | + throw new Error("Please provide a non-empty value for either outputSchema or sampleObject."); |
| 58 | + } |
| 59 | + this._configuration = configuration; |
| 60 | + this._sampleSchema = sampleSchema; |
| 61 | + this._conventions = conventions; |
| 62 | + } |
| 63 | + |
| 64 | + get isReadRequest(): boolean { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + getRaftUniqueRequestId(): string { |
| 69 | + return RaftIdGenerator.newId(); |
| 70 | + } |
| 71 | + |
| 72 | + createRequest(node: ServerNode): HttpRequestParameters { |
| 73 | + const uri = node.url + "/databases/" + node.database + "/admin/ai/agent"; |
| 74 | + const configToSend = {...this._configuration}; |
| 75 | + |
| 76 | + if (!configToSend.sampleObject && this._sampleSchema) { |
| 77 | + configToSend.sampleObject = this._sampleSchema; |
| 78 | + } |
| 79 | + |
| 80 | + const bodyToSerialize = { |
| 81 | + ...configToSend, |
| 82 | + parameters: configToSend.parameters ? Array.from(configToSend.parameters) : [] |
| 83 | + }; |
| 84 | + |
| 85 | + const bodyJson = ObjectUtil.transformObjectKeys(bodyToSerialize, { |
| 86 | + defaultTransform: ObjectUtil.pascal |
| 87 | + }); |
| 88 | + |
| 89 | + const body = JsonSerializer.getDefault().serialize(bodyJson); |
| 90 | + |
| 91 | + const headers = HeadersBuilder |
| 92 | + .create() |
| 93 | + .typeAppJson() |
| 94 | + .build(); |
| 95 | + |
| 96 | + console.log(uri); |
| 97 | + return { |
| 98 | + method: "PUT", |
| 99 | + uri, |
| 100 | + body, |
| 101 | + headers |
| 102 | + } as HttpRequestParameters; |
| 103 | + } |
| 104 | + |
| 105 | + async setResponseAsync(bodyStream: Stream, fromCache: boolean): Promise<string> { |
| 106 | + if (!bodyStream) { |
| 107 | + this._throwInvalidResponse(); |
| 108 | + } |
| 109 | + let body = ""; |
| 110 | + const data = await this._defaultPipeline<any>(_ => body = _).process(bodyStream); |
| 111 | + this.result = { |
| 112 | + identifier: data?.Identifier ?? data?.identifier, |
| 113 | + raftCommandIndex: data?.RaftCommandIndex ?? data?.raftCommandIndex |
| 114 | + } as AiAgentConfigurationResult; |
| 115 | + return body; |
| 116 | + } |
| 117 | +} |
0 commit comments