@@ -3,8 +3,6 @@ import { Stream } from "node:stream";
33import type { AiAgentActionResponse } from "./AiAgentActionResponse.js" ;
44import type { AiConversationCreationOptions } from "./AiConversationCreationOptions.js" ;
55import type { ConversationResult } from "./ConversationResult.js" ;
6- import type { AiUsage } from "./AiUsage.js" ;
7- import type { AiAgentActionRequest } from "./AiAgentActionRequest.js" ;
86import { RavenCommand } from "../../../../Http/RavenCommand.js" ;
97import { DocumentConventions } from "../../../Conventions/DocumentConventions.js" ;
108import { IRaftCommand } from "../../../../Http/IRaftCommand.js" ;
@@ -90,7 +88,6 @@ class RunConversationCommand<TAnswer>
9088 this . _options = options ;
9189 this . _changeVector = changeVector ;
9290
93- // For new conversation IDs (ending with '|'), we need a raft id
9491 if ( this . _conversationId && this . _conversationId . endsWith ( "|" ) ) {
9592 this . _raftId = RaftIdGenerator . newId ( ) ;
9693 }
@@ -104,80 +101,48 @@ class RunConversationCommand<TAnswer>
104101 return this . _raftId ;
105102 }
106103
107- createRequest ( node : ServerNode ) : HttpRequestParameters {
108- const uriParams = new URLSearchParams ( {
109- conversationId : this . _conversationId ,
110- agentId : this . _agentId ,
111- } ) ;
104+ createRequest ( node : ServerNode ) : HttpRequestParameters {
105+ const uriParams = new URLSearchParams ( {
106+ conversationId : this . _conversationId ,
107+ agentId : this . _agentId ,
108+ } ) ;
112109
113- if ( this . _changeVector ) {
114- uriParams . append ( "changeVector" , this . _changeVector ) ;
115- }
110+ if ( this . _changeVector ) {
111+ uriParams . append ( "changeVector" , this . _changeVector ) ;
112+ }
116113
117- const uri = `${ node . url } /databases/${ node . database } /ai/agent?${ uriParams } ` ;
114+ const uri = `${ node . url } /databases/${ node . database } /ai/agent?${ uriParams } ` ;
118115
119- const bodyObj = {
120- ActionResponses : this . _actionResponses ,
121- UserPrompt : this . _prompt ,
122- CreationOptions : this . _options
123- } ;
116+ const bodyObj = {
117+ ActionResponses : this . _actionResponses ,
118+ UserPrompt : this . _prompt ,
119+ CreationOptions : this . _options
120+ } ;
124121
125- const headers = this . _headers ( ) . typeAppJson ( ) . build ( ) ;
122+ const headers = this . _headers ( ) . typeAppJson ( ) . build ( ) ;
126123
127- // Serialize to PascalCase but ignore the parameters property in CreationOptions
128- const serialized = ObjectUtil . transformObjectKeys ( bodyObj , {
129- defaultTransform : ObjectUtil . pascalCase ,
130- ignorePaths : [
131- new RegExp ( "^CreationOptions\\.Parameters\\..*$" )
132- ]
133- } ) ;
124+ // Serialize properties to PascalCase, except " parameters" in CreationOptions, which must keep user-provided, case-sensitive keys unchanged.
125+ const serialized = ObjectUtil . transformObjectKeys ( bodyObj , {
126+ defaultTransform : ObjectUtil . pascalCase ,
127+ ignorePaths : [
128+ new RegExp ( "^CreationOptions\\.Parameters\\..*$" )
129+ ]
130+ } ) ;
134131
135132
136- return {
137- method : "POST" ,
138- uri,
139- headers,
140- body : JSON . stringify ( serialized )
141- } ;
142- }
133+ return {
134+ method : "POST" ,
135+ uri,
136+ headers,
137+ body : JsonSerializer . getDefault ( ) . serialize ( serialized )
138+ } ;
139+ }
143140
144141 async setResponseAsync ( bodyStream : Stream , fromCache : boolean ) : Promise < string > {
145142 if ( ! bodyStream ) {
146143 this . _throwInvalidResponse ( ) ;
147144 }
148145
149- let body = "" ;
150- const raw = await this . _defaultPipeline < any > ( _ => body = _ ) . process ( bodyStream ) ;
151-
152- // Normalize server PascalCase payload to our camelCase TS interface
153- const result : ConversationResult < TAnswer > = {
154- conversationId : raw ?. ConversationId ?? raw ?. conversationId ,
155- changeVector : raw ?. ChangeVector ?? raw ?. changeVector ,
156- response : ( raw ?. Response ?? raw ?. response ) as TAnswer ,
157- totalUsage : normalizeUsage ( raw ?. TotalUsage ?? raw ?. totalUsage ) ,
158- actionRequests : normalizeActionRequests ( raw ?. ActionRequests ?? raw ?. actionRequests )
159- } ;
160-
161- this . result = result ;
162- return body ;
146+ return this . _parseResponseDefaultAsync ( bodyStream )
163147 }
164- }
165-
166- function normalizeUsage ( u : any ) : AiUsage {
167- if ( ! u ) return null ;
168- return {
169- promptTokens : u . PromptTokens ?? u . promptTokens ,
170- completionTokens : u . CompletionTokens ?? u . completionTokens ,
171- totalTokens : u . TotalTokens ?? u . totalTokens ,
172- cachedTokens : u . CachedTokens ?? u . cachedTokens
173- } as AiUsage ;
174- }
175-
176- function normalizeActionRequests ( list : any [ ] ) : AiAgentActionRequest [ ] {
177- if ( ! Array . isArray ( list ) ) return [ ] ;
178- return list . map ( x => ( {
179- name : x . Name ?? x . name ,
180- toolId : x . ToolId ?? x . toolId ,
181- arguments : x . Arguments ?? x . arguments
182- } as AiAgentActionRequest ) ) ;
183- }
148+ }
0 commit comments