@fonoster/sdk 0.17.4 → 0.18.0

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.
@@ -16,7 +16,7 @@
16
16
  * See the License for the specific language governing permissions and
17
17
  * limitations under the License.
18
18
  */
19
- import { Application, BaseApiObject, CreateApplicationRequest, EvaluateIntelligenceRequest, EvaluateIntelligenceResponse, ListApplicationsRequest, ListApplicationsResponse, UpdateApplicationRequest } from "@fonoster/types";
19
+ import { Application, BaseApiObject, CreateApplicationRequest, EvaluateIntelligenceEvent, EvaluateIntelligenceRequest, ListApplicationsRequest, ListApplicationsResponse, UpdateApplicationRequest } from "@fonoster/types";
20
20
  import { FonosterClient } from "./client/types";
21
21
  import { TestTokenResponse } from "./generated/node/applications_pb";
22
22
  /**
@@ -213,62 +213,18 @@ declare class Applications {
213
213
  * @param {EvaluateIntelligenceRequest} request - The request object that contains the necessary information to evaluate the intelligence of an application
214
214
  * @param {string} request.intelligence.productRef - The product reference of the intelligence engine (e.g., llm.groq)
215
215
  * @param {object} request.intelligence.config - The configuration object for the intelligence engine
216
- * @return {Promise<ScenarioEvaluationReport>} - The response object that contains the evaluation report
216
+ * @return {AsyncGenerator<EvaluateIntelligenceEvent>} - Stream of evaluation events (step results, scenario summaries, or errors)
217
217
  * @example
218
- * const apps = new SDK.Applications(client); // Existing client object
219
- *
220
- * const request = {
221
- * intelligence: {
222
- * productRef: "llm.groq",
223
- * config: {
224
- * conversationSettings: {
225
- * firstMessage: "Hello, how can I help you today?",
226
- * systemPrompt: "You are a helpful assistant.",
227
- * systemErrorMessage: "I'm sorry, I didn't catch that. Can you say that again?",
228
- * goodbyeMessage: "Thank you for calling. Have a great day!",
229
- * languageModel: {
230
- * provider: "openai",
231
- * model: "gpt-4o"
232
- * },
233
- * testCases: {
234
- * evalsLanguageModel: {
235
- * provider: "openai",
236
- * model: "gpt-4o"
237
- * },
238
- * scenarios: [
239
- * {
240
- * ref: "Scenario 1",
241
- * description: "Scenario 1 description",
242
- * telephonyContext: {
243
- * callDirection: "FROM_PSTN",
244
- * ingressNumber: "1234567890",
245
- * callerNumber: "1234567890"
246
- * },
247
- * conversation: [
248
- * {
249
- * userInput: "Hello, how can I help you today?",
250
- * expected: {
251
- * text: {
252
- * type: "EXACT",
253
- * response: "Hello, how can I help you today?"
254
- * }
255
- * }
256
- * }
257
- * ]
258
- * }
259
- * ]
260
- * }
261
- * }
262
- * }
263
- * }
264
- * };
265
- *
266
- * apps
267
- * .evaluateIntelligence(request)
268
- * .then(console.log) // successful response
269
- * .catch(console.error); // an error occurred
218
+ * const apps = new SDK.Applications(client);
219
+ * for await (const event of apps.evaluateIntelligence(request)) {
220
+ * if (event.type === "stepResult") console.log(event.stepResult);
221
+ * if (event.type === "scenarioSummary") console.log(event.scenarioRef, event.overallPassed);
222
+ * if (event.type === "evalError") console.error(event.message);
223
+ * }
270
224
  */
271
- evaluateIntelligence(request: EvaluateIntelligenceRequest): Promise<EvaluateIntelligenceResponse>;
225
+ evaluateIntelligence(request: EvaluateIntelligenceRequest): AsyncGenerator<EvaluateIntelligenceEvent>;
226
+ private evaluateIntelligenceStreamGenerator;
227
+ private mapEvalEventFromPb;
272
228
  /**
273
229
  * Creates an Ephemeral Agent token for test calls.
274
230
  *
@@ -293,81 +293,95 @@ class Applications {
293
293
  * @param {EvaluateIntelligenceRequest} request - The request object that contains the necessary information to evaluate the intelligence of an application
294
294
  * @param {string} request.intelligence.productRef - The product reference of the intelligence engine (e.g., llm.groq)
295
295
  * @param {object} request.intelligence.config - The configuration object for the intelligence engine
296
- * @return {Promise<ScenarioEvaluationReport>} - The response object that contains the evaluation report
296
+ * @return {AsyncGenerator<EvaluateIntelligenceEvent>} - Stream of evaluation events (step results, scenario summaries, or errors)
297
297
  * @example
298
- * const apps = new SDK.Applications(client); // Existing client object
299
- *
300
- * const request = {
301
- * intelligence: {
302
- * productRef: "llm.groq",
303
- * config: {
304
- * conversationSettings: {
305
- * firstMessage: "Hello, how can I help you today?",
306
- * systemPrompt: "You are a helpful assistant.",
307
- * systemErrorMessage: "I'm sorry, I didn't catch that. Can you say that again?",
308
- * goodbyeMessage: "Thank you for calling. Have a great day!",
309
- * languageModel: {
310
- * provider: "openai",
311
- * model: "gpt-4o"
312
- * },
313
- * testCases: {
314
- * evalsLanguageModel: {
315
- * provider: "openai",
316
- * model: "gpt-4o"
317
- * },
318
- * scenarios: [
319
- * {
320
- * ref: "Scenario 1",
321
- * description: "Scenario 1 description",
322
- * telephonyContext: {
323
- * callDirection: "FROM_PSTN",
324
- * ingressNumber: "1234567890",
325
- * callerNumber: "1234567890"
326
- * },
327
- * conversation: [
328
- * {
329
- * userInput: "Hello, how can I help you today?",
330
- * expected: {
331
- * text: {
332
- * type: "EXACT",
333
- * response: "Hello, how can I help you today?"
334
- * }
335
- * }
336
- * }
337
- * ]
338
- * }
339
- * ]
340
- * }
341
- * }
342
- * }
343
- * }
344
- * };
345
- *
346
- * apps
347
- * .evaluateIntelligence(request)
348
- * .then(console.log) // successful response
349
- * .catch(console.error); // an error occurred
298
+ * const apps = new SDK.Applications(client);
299
+ * for await (const event of apps.evaluateIntelligence(request)) {
300
+ * if (event.type === "stepResult") console.log(event.stepResult);
301
+ * if (event.type === "scenarioSummary") console.log(event.scenarioRef, event.overallPassed);
302
+ * if (event.type === "evalError") console.error(event.message);
303
+ * }
350
304
  */
351
- async evaluateIntelligence(request) {
305
+ evaluateIntelligence(request) {
352
306
  const applicationsClient = this.client.getApplicationsClient();
353
- const response = await (0, makeRpcRequest_1.makeRpcRequest)({
354
- method: applicationsClient.evaluateIntelligence.bind(applicationsClient),
355
- requestPBObjectConstructor: applications_pb_1.EvaluateIntelligenceRequest,
356
- metadata: this.client.getMetadata(),
357
- request: {
358
- intelligence: {
359
- productRef: request.intelligence.productRef,
360
- config: struct_pb_1.Struct.fromJavaScript(request.intelligence.config)
361
- }
362
- },
363
- enumMapping: [["ExpectedTextType", types_1.ExpectedTextType]],
364
- objectMapping: [["intelligence", applications_pb_1.ProductContainer]],
365
- repeatableObjectMapping: [
366
- ["resultsList", applications_pb_1.ScenarioEvaluationReport],
367
- ["stepsList", applications_pb_1.StepEvaluationReport]
368
- ]
307
+ const requestPB = new applications_pb_1.EvaluateIntelligenceRequest();
308
+ const productContainer = new applications_pb_1.ProductContainer();
309
+ productContainer.setProductRef(request.intelligence.productRef);
310
+ productContainer.setConfig(struct_pb_1.Struct.fromJavaScript(request.intelligence.config));
311
+ requestPB.setIntelligence(productContainer);
312
+ const metadata = this.client.getMetadata();
313
+ const call = applicationsClient.evaluateIntelligence(requestPB, metadata);
314
+ return this.evaluateIntelligenceStreamGenerator(call);
315
+ }
316
+ async *evaluateIntelligenceStreamGenerator(call) {
317
+ const queue = [];
318
+ let done = false;
319
+ let streamError = null;
320
+ call.on("data", (chunk) => {
321
+ const event = this.mapEvalEventFromPb(chunk);
322
+ if (event)
323
+ queue.push(event);
369
324
  });
370
- return response;
325
+ call.on("end", () => {
326
+ done = true;
327
+ });
328
+ call.on("error", (err) => {
329
+ streamError = err instanceof Error ? err : new Error(String(err));
330
+ done = true;
331
+ });
332
+ while (!done || queue.length > 0) {
333
+ if (streamError)
334
+ throw streamError;
335
+ if (queue.length > 0) {
336
+ const next = queue.shift();
337
+ yield next;
338
+ }
339
+ else {
340
+ await new Promise((r) => setTimeout(r, 50));
341
+ }
342
+ }
343
+ }
344
+ mapEvalEventFromPb(msg) {
345
+ if (msg.getStepResult?.()) {
346
+ const sr = msg.getStepResult();
347
+ const report = sr.getReport();
348
+ const toolList = report.getToolEvaluationsList?.() ?? [];
349
+ return {
350
+ type: "stepResult",
351
+ scenarioRef: sr.getScenarioRef(),
352
+ stepResult: {
353
+ humanInput: report.getHumanInput(),
354
+ expectedResponse: report.getExpectedResponse(),
355
+ aiResponse: report.getAiResponse(),
356
+ evaluationType: report.getEvaluationType() === 0
357
+ ? types_1.ExpectedTextType.EXACT
358
+ : types_1.ExpectedTextType.SIMILAR,
359
+ passed: report.getPassed(),
360
+ errorMessage: report.getErrorMessage() || undefined,
361
+ toolEvaluations: toolList.map((t) => ({
362
+ expectedTool: t.getExpectedTool(),
363
+ actualTool: t.getActualTool(),
364
+ passed: t.getPassed(),
365
+ expectedParameters: t.getExpectedParameters?.()?.toJavaScript() ?? undefined,
366
+ actualParameters: t.getActualParameters?.()?.toJavaScript() ?? undefined,
367
+ errorMessage: t.getErrorMessage?.() || undefined
368
+ }))
369
+ }
370
+ };
371
+ }
372
+ if (msg.getScenarioSummary?.()) {
373
+ const ss = msg.getScenarioSummary();
374
+ return {
375
+ type: "scenarioSummary",
376
+ scenarioRef: ss.getScenarioRef(),
377
+ overallPassed: ss.getOverallPassed()
378
+ };
379
+ }
380
+ if (msg.getEvalError?.()) {
381
+ const e = msg.getEvalError();
382
+ return { type: "evalError", message: e.getMessage() };
383
+ }
384
+ return null;
371
385
  }
372
386
  /**
373
387
  * Creates an Ephemeral Agent token for test calls.
@@ -17,15 +17,16 @@
17
17
  * limitations under the License.
18
18
  */
19
19
  import { Empty } from "google-protobuf/google/protobuf/empty_pb";
20
- import { Application, CreateApplicationRequest, CreateApplicationResponse, DeleteApplicationRequest, DeleteApplicationResponse, EvaluateIntelligenceRequest, EvaluateIntelligenceResponse, GetApplicationRequest, ListApplicationsRequest, ListApplicationsResponse, TestTokenResponse, UpdateApplicationRequest, UpdateApplicationResponse } from "../../generated/web/applications_pb";
20
+ import { Application, CreateApplicationRequest, CreateApplicationResponse, DeleteApplicationRequest, DeleteApplicationResponse, EvaluateIntelligenceEvent, EvaluateIntelligenceRequest, GetApplicationRequest, ListApplicationsRequest, ListApplicationsResponse, TestTokenResponse, UpdateApplicationRequest, UpdateApplicationResponse } from "../../generated/web/applications_pb";
21
21
  import { ClientFunction } from "./common";
22
+ import type { ClientReadableStream } from "grpc-web";
22
23
  type ApplicationsClient = {
23
24
  createApplication: ClientFunction<CreateApplicationRequest, CreateApplicationResponse>;
24
25
  getApplication: ClientFunction<GetApplicationRequest, Application>;
25
26
  updateApplication: ClientFunction<UpdateApplicationRequest, UpdateApplicationResponse>;
26
27
  listApplications: ClientFunction<ListApplicationsRequest, ListApplicationsResponse>;
27
28
  deleteApplication: ClientFunction<DeleteApplicationRequest, DeleteApplicationResponse>;
28
- evaluateIntelligence: ClientFunction<EvaluateIntelligenceRequest, EvaluateIntelligenceResponse>;
29
+ evaluateIntelligence: (request: EvaluateIntelligenceRequest, metadata?: Record<string, string> | null) => ClientReadableStream<EvaluateIntelligenceEvent>;
29
30
  createTestToken: ClientFunction<Empty, TestTokenResponse>;
30
31
  };
31
32
  export { ApplicationsClient };
@@ -79,26 +79,26 @@ function deserialize_fonoster_applications_v1beta2_DeleteApplicationResponse(buf
79
79
  return applications_pb.DeleteApplicationResponse.deserializeBinary(new Uint8Array(buffer_arg));
80
80
  }
81
81
 
82
- function serialize_fonoster_applications_v1beta2_EvaluateIntelligenceRequest(arg) {
83
- if (!(arg instanceof applications_pb.EvaluateIntelligenceRequest)) {
84
- throw new Error('Expected argument of type fonoster.applications.v1beta2.EvaluateIntelligenceRequest');
82
+ function serialize_fonoster_applications_v1beta2_EvaluateIntelligenceEvent(arg) {
83
+ if (!(arg instanceof applications_pb.EvaluateIntelligenceEvent)) {
84
+ throw new Error('Expected argument of type fonoster.applications.v1beta2.EvaluateIntelligenceEvent');
85
85
  }
86
86
  return Buffer.from(arg.serializeBinary());
87
87
  }
88
88
 
89
- function deserialize_fonoster_applications_v1beta2_EvaluateIntelligenceRequest(buffer_arg) {
90
- return applications_pb.EvaluateIntelligenceRequest.deserializeBinary(new Uint8Array(buffer_arg));
89
+ function deserialize_fonoster_applications_v1beta2_EvaluateIntelligenceEvent(buffer_arg) {
90
+ return applications_pb.EvaluateIntelligenceEvent.deserializeBinary(new Uint8Array(buffer_arg));
91
91
  }
92
92
 
93
- function serialize_fonoster_applications_v1beta2_EvaluateIntelligenceResponse(arg) {
94
- if (!(arg instanceof applications_pb.EvaluateIntelligenceResponse)) {
95
- throw new Error('Expected argument of type fonoster.applications.v1beta2.EvaluateIntelligenceResponse');
93
+ function serialize_fonoster_applications_v1beta2_EvaluateIntelligenceRequest(arg) {
94
+ if (!(arg instanceof applications_pb.EvaluateIntelligenceRequest)) {
95
+ throw new Error('Expected argument of type fonoster.applications.v1beta2.EvaluateIntelligenceRequest');
96
96
  }
97
97
  return Buffer.from(arg.serializeBinary());
98
98
  }
99
99
 
100
- function deserialize_fonoster_applications_v1beta2_EvaluateIntelligenceResponse(buffer_arg) {
101
- return applications_pb.EvaluateIntelligenceResponse.deserializeBinary(new Uint8Array(buffer_arg));
100
+ function deserialize_fonoster_applications_v1beta2_EvaluateIntelligenceRequest(buffer_arg) {
101
+ return applications_pb.EvaluateIntelligenceRequest.deserializeBinary(new Uint8Array(buffer_arg));
102
102
  }
103
103
 
104
104
  function serialize_fonoster_applications_v1beta2_GetApplicationRequest(arg) {
@@ -241,17 +241,17 @@ deleteApplication: {
241
241
  responseSerialize: serialize_fonoster_applications_v1beta2_DeleteApplicationResponse,
242
242
  responseDeserialize: deserialize_fonoster_applications_v1beta2_DeleteApplicationResponse,
243
243
  },
244
- // Evaluate the intelligence for an Autopilot application
244
+ // Evaluate the intelligence for an Autopilot application (server streaming)
245
245
  evaluateIntelligence: {
246
246
  path: '/fonoster.applications.v1beta2.Applications/EvaluateIntelligence',
247
247
  requestStream: false,
248
- responseStream: false,
248
+ responseStream: true,
249
249
  requestType: applications_pb.EvaluateIntelligenceRequest,
250
- responseType: applications_pb.EvaluateIntelligenceResponse,
250
+ responseType: applications_pb.EvaluateIntelligenceEvent,
251
251
  requestSerialize: serialize_fonoster_applications_v1beta2_EvaluateIntelligenceRequest,
252
252
  requestDeserialize: deserialize_fonoster_applications_v1beta2_EvaluateIntelligenceRequest,
253
- responseSerialize: serialize_fonoster_applications_v1beta2_EvaluateIntelligenceResponse,
254
- responseDeserialize: deserialize_fonoster_applications_v1beta2_EvaluateIntelligenceResponse,
253
+ responseSerialize: serialize_fonoster_applications_v1beta2_EvaluateIntelligenceEvent,
254
+ responseDeserialize: deserialize_fonoster_applications_v1beta2_EvaluateIntelligenceEvent,
255
255
  },
256
256
  // Create an Ephemeral Agent to perform test calls to an application
257
257
  createTestToken: {