@flutchai/flutch-sdk 0.1.6 → 0.1.8

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.
package/dist/index.d.cts CHANGED
@@ -211,9 +211,13 @@ type CitationValue = {
211
211
 
212
212
  interface IReasoningStep {
213
213
  index: number;
214
- type: "text" | "tool_call" | "tool_result" | "thinking" | "tool_use";
214
+ type: "text" | "tool_use";
215
215
  text?: string;
216
216
  metadata?: Record<string, any>;
217
+ name?: string;
218
+ id?: string;
219
+ input?: string;
220
+ output?: string;
217
221
  }
218
222
  interface IReasoningChain {
219
223
  steps: IReasoningStep[];
@@ -1120,11 +1124,14 @@ interface StreamAccumulator {
1120
1124
  traceEvents: IGraphTraceEvent[];
1121
1125
  traceStartedAt: number | null;
1122
1126
  traceCompletedAt: number | null;
1127
+ currentReasoningSteps: IReasoningStep[];
1128
+ currentToolUse: IReasoningStep | null;
1123
1129
  }
1124
1130
  declare class EventProcessor {
1125
1131
  private readonly logger;
1126
1132
  createAccumulator(): StreamAccumulator;
1127
1133
  private normalizeContentBlocks;
1134
+ private mapReasoningSteps;
1128
1135
  processEvent(acc: StreamAccumulator, event: any, onPartial?: (chunk: string) => void): void;
1129
1136
  getResult(acc: StreamAccumulator): {
1130
1137
  content: IStoredMessageContent;
@@ -1155,9 +1162,9 @@ declare class EventProcessor {
1155
1162
 
1156
1163
  declare class LangGraphEngine implements IGraphEngine {
1157
1164
  private readonly eventProcessor;
1158
- private readonly configService;
1165
+ private readonly configService?;
1159
1166
  private readonly logger;
1160
- constructor(eventProcessor: EventProcessor, configService: ConfigService);
1167
+ constructor(eventProcessor: EventProcessor, configService?: ConfigService);
1161
1168
  invokeGraph(graph: any, config: any, signal?: AbortSignal): Promise<any>;
1162
1169
  streamGraph(graph: any, config: any, onPartial: (chunk: string) => void, signal?: AbortSignal): Promise<any>;
1163
1170
  private sendMetricsWebhook;
package/dist/index.d.ts CHANGED
@@ -211,9 +211,13 @@ type CitationValue = {
211
211
 
212
212
  interface IReasoningStep {
213
213
  index: number;
214
- type: "text" | "tool_call" | "tool_result" | "thinking" | "tool_use";
214
+ type: "text" | "tool_use";
215
215
  text?: string;
216
216
  metadata?: Record<string, any>;
217
+ name?: string;
218
+ id?: string;
219
+ input?: string;
220
+ output?: string;
217
221
  }
218
222
  interface IReasoningChain {
219
223
  steps: IReasoningStep[];
@@ -1120,11 +1124,14 @@ interface StreamAccumulator {
1120
1124
  traceEvents: IGraphTraceEvent[];
1121
1125
  traceStartedAt: number | null;
1122
1126
  traceCompletedAt: number | null;
1127
+ currentReasoningSteps: IReasoningStep[];
1128
+ currentToolUse: IReasoningStep | null;
1123
1129
  }
1124
1130
  declare class EventProcessor {
1125
1131
  private readonly logger;
1126
1132
  createAccumulator(): StreamAccumulator;
1127
1133
  private normalizeContentBlocks;
1134
+ private mapReasoningSteps;
1128
1135
  processEvent(acc: StreamAccumulator, event: any, onPartial?: (chunk: string) => void): void;
1129
1136
  getResult(acc: StreamAccumulator): {
1130
1137
  content: IStoredMessageContent;
@@ -1155,9 +1162,9 @@ declare class EventProcessor {
1155
1162
 
1156
1163
  declare class LangGraphEngine implements IGraphEngine {
1157
1164
  private readonly eventProcessor;
1158
- private readonly configService;
1165
+ private readonly configService?;
1159
1166
  private readonly logger;
1160
- constructor(eventProcessor: EventProcessor, configService: ConfigService);
1167
+ constructor(eventProcessor: EventProcessor, configService?: ConfigService);
1161
1168
  invokeGraph(graph: any, config: any, signal?: AbortSignal): Promise<any>;
1162
1169
  streamGraph(graph: any, config: any, onPartial: (chunk: string) => void, signal?: AbortSignal): Promise<any>;
1163
1170
  private sendMetricsWebhook;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import 'reflect-metadata';
2
- import { Injectable, Post, UseGuards, Req, Controller, Inject, Get, Body, Res, Param, Logger, Module, UnauthorizedException, Optional, Headers, NotFoundException, InternalServerErrorException, ForbiddenException, ValidationPipe, HttpException, HttpStatus } from '@nestjs/common';
2
+ import { Injectable, Post, UseGuards, Req, Controller, Inject, Get, Body, Res, Param, Logger, Optional, Module, UnauthorizedException, Headers, NotFoundException, InternalServerErrorException, ForbiddenException, ValidationPipe, HttpException, HttpStatus } from '@nestjs/common';
3
3
  import { ApiOperation, ApiResponse, ApiTags, ApiParam, DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
4
4
  import * as fs from 'fs';
5
5
  import * as path2 from 'path';
@@ -3047,9 +3047,6 @@ var _AbstractGraphBuilder = class _AbstractGraphBuilder {
3047
3047
  if (this.manifest?.companySlug && this.manifest?.name) {
3048
3048
  return `${this.manifest.companySlug}.${this.manifest.name}::${this.version}`;
3049
3049
  }
3050
- console.log(
3051
- `DEBUG graphType: manifest=${!!this.manifest}, companySlug=${this.manifest?.companySlug}, name=${this.manifest?.name}, version=${this.version}`
3052
- );
3053
3050
  return `unknown::${this.version}`;
3054
3051
  }
3055
3052
  /**
@@ -3381,6 +3378,9 @@ var UniversalGraphService = class {
3381
3378
  this.engine = engine;
3382
3379
  this.endpointRegistry = endpointRegistry;
3383
3380
  this.logger.log("UniversalGraphService initialized");
3381
+ if (!this.engine) {
3382
+ this.logger.error("GRAPH_ENGINE is not properly injected!");
3383
+ }
3384
3384
  }
3385
3385
  logger = new Logger(UniversalGraphService.name);
3386
3386
  /**
@@ -4376,11 +4376,11 @@ function sanitizeTraceError(error, options) {
4376
4376
  raw: sanitizeTraceData(error, 0, /* @__PURE__ */ new WeakSet(), options)
4377
4377
  };
4378
4378
  }
4379
- var GraphEngineType = /* @__PURE__ */ ((GraphEngineType2) => {
4380
- GraphEngineType2["LANGGRAPH"] = "langgraph";
4381
- GraphEngineType2["LANGFLOW"] = "langflow";
4382
- GraphEngineType2["FLOWISE"] = "flowise";
4383
- return GraphEngineType2;
4379
+ var GraphEngineType = /* @__PURE__ */ ((GraphEngineType3) => {
4380
+ GraphEngineType3["LANGGRAPH"] = "langgraph";
4381
+ GraphEngineType3["LANGFLOW"] = "langflow";
4382
+ GraphEngineType3["FLOWISE"] = "flowise";
4383
+ return GraphEngineType3;
4384
4384
  })(GraphEngineType || {});
4385
4385
  var GraphEngineFactory = class {
4386
4386
  constructor(langgraph) {
@@ -4419,7 +4419,9 @@ var EventProcessor = class {
4419
4419
  llmCalls: [],
4420
4420
  traceEvents: [],
4421
4421
  traceStartedAt: null,
4422
- traceCompletedAt: null
4422
+ traceCompletedAt: null,
4423
+ currentReasoningSteps: [],
4424
+ currentToolUse: null
4423
4425
  };
4424
4426
  }
4425
4427
  /**
@@ -4447,6 +4449,51 @@ var EventProcessor = class {
4447
4449
  }
4448
4450
  return [];
4449
4451
  }
4452
+ /**
4453
+ * Groups tool_use and input_json_delta into proper structure
4454
+ * tool_use.input → output (tool execution result)
4455
+ * input_json_delta.input → output (tool execution result, accumulated)
4456
+ */
4457
+ mapReasoningSteps(rawSteps) {
4458
+ const steps = [];
4459
+ let currentToolUse = null;
4460
+ for (const raw of rawSteps) {
4461
+ if (raw.type === "tool_use" || raw.type === "tool_call") {
4462
+ if (currentToolUse) {
4463
+ steps.push(currentToolUse);
4464
+ }
4465
+ currentToolUse = {
4466
+ index: raw.index || 0,
4467
+ type: "tool_use",
4468
+ name: raw.name,
4469
+ id: raw.id,
4470
+ input: "",
4471
+ // Parameters (IN) - filled separately or empty
4472
+ output: raw.input || ""
4473
+ // Result (OUT) - comes in tool_use.input
4474
+ };
4475
+ } else if (raw.type === "input_json_delta") {
4476
+ if (currentToolUse) {
4477
+ currentToolUse.output = (currentToolUse.output || "") + (raw.input || "");
4478
+ }
4479
+ } else {
4480
+ if (currentToolUse) {
4481
+ steps.push(currentToolUse);
4482
+ currentToolUse = null;
4483
+ }
4484
+ steps.push({
4485
+ index: raw.index || 0,
4486
+ type: raw.type,
4487
+ text: raw.text || "",
4488
+ metadata: raw.metadata
4489
+ });
4490
+ }
4491
+ }
4492
+ if (currentToolUse) {
4493
+ steps.push(currentToolUse);
4494
+ }
4495
+ return steps;
4496
+ }
4450
4497
  /**
4451
4498
  * Process a LangGraph stream event
4452
4499
  * Mutates accumulator to collect data from different channels
@@ -4469,8 +4516,44 @@ var EventProcessor = class {
4469
4516
  if (event.event === "on_chat_model_stream" && event.metadata?.stream_channel === "processing" /* PROCESSING */ && event.data?.chunk?.content) {
4470
4517
  const chunk = event.data.chunk.content;
4471
4518
  const blocks = this.normalizeContentBlocks(chunk);
4472
- if (blocks.length > 0 && onPartial) {
4473
- onPartial(JSON.stringify({ processing: blocks }));
4519
+ for (const block of blocks) {
4520
+ if (block.type === "tool_use" || block.type === "tool_call") {
4521
+ if (acc.currentToolUse) {
4522
+ acc.currentReasoningSteps.push(acc.currentToolUse);
4523
+ }
4524
+ acc.currentToolUse = {
4525
+ index: acc.currentReasoningSteps.length,
4526
+ type: "tool_use",
4527
+ name: block.name,
4528
+ id: block.id,
4529
+ input: block.input || "",
4530
+ output: ""
4531
+ };
4532
+ if (onPartial) {
4533
+ onPartial(
4534
+ JSON.stringify({
4535
+ processing_delta: {
4536
+ type: "step_started",
4537
+ step: acc.currentToolUse
4538
+ }
4539
+ })
4540
+ );
4541
+ }
4542
+ } else if (block.type === "input_json_delta") {
4543
+ if (acc.currentToolUse && onPartial) {
4544
+ const chunk2 = block.input || "";
4545
+ acc.currentToolUse.output += chunk2;
4546
+ onPartial(
4547
+ JSON.stringify({
4548
+ processing_delta: {
4549
+ type: "output_chunk",
4550
+ stepId: acc.currentToolUse.id,
4551
+ chunk: chunk2
4552
+ }
4553
+ })
4554
+ );
4555
+ }
4556
+ }
4474
4557
  }
4475
4558
  return;
4476
4559
  }
@@ -4507,29 +4590,59 @@ var EventProcessor = class {
4507
4590
  );
4508
4591
  }
4509
4592
  if (event.metadata?.stream_channel === "processing" /* PROCESSING */) {
4510
- const stepsRaw = output?.content || // AIMessageChunk object (direct)
4511
- output?.kwargs?.content || // Serialized LangChain format
4512
- event.data?.chunk?.content || // Older version
4513
- [];
4514
- let steps;
4515
- if (Array.isArray(stepsRaw)) {
4516
- steps = stepsRaw;
4517
- } else if (typeof stepsRaw === "string" && stepsRaw.trim().length > 0) {
4518
- steps = [
4519
- {
4520
- index: 0,
4521
- type: "text",
4522
- text: stepsRaw.trim()
4523
- }
4524
- ];
4525
- } else {
4526
- steps = [];
4593
+ if (acc.currentToolUse) {
4594
+ acc.currentReasoningSteps.push(acc.currentToolUse);
4595
+ acc.currentToolUse = null;
4527
4596
  }
4528
- if (steps.length > 0) {
4597
+ if (acc.currentReasoningSteps.length > 0) {
4529
4598
  acc.reasoningChains.push({
4530
- steps,
4599
+ steps: acc.currentReasoningSteps,
4531
4600
  isComplete: true
4532
4601
  });
4602
+ if (onPartial) {
4603
+ onPartial(
4604
+ JSON.stringify({
4605
+ processing_delta: {
4606
+ type: "chain_completed"
4607
+ }
4608
+ })
4609
+ );
4610
+ }
4611
+ acc.currentReasoningSteps = [];
4612
+ } else {
4613
+ const stepsRaw = output?.content || // AIMessageChunk object (direct)
4614
+ output?.kwargs?.content || // Serialized LangChain format
4615
+ event.data?.chunk?.content || // Older version
4616
+ [];
4617
+ let steps;
4618
+ if (Array.isArray(stepsRaw)) {
4619
+ steps = this.mapReasoningSteps(stepsRaw);
4620
+ } else if (typeof stepsRaw === "string" && stepsRaw.trim().length > 0) {
4621
+ steps = [
4622
+ {
4623
+ index: 0,
4624
+ type: "text",
4625
+ text: stepsRaw.trim()
4626
+ }
4627
+ ];
4628
+ } else {
4629
+ steps = [];
4630
+ }
4631
+ if (steps.length > 0) {
4632
+ acc.reasoningChains.push({
4633
+ steps,
4634
+ isComplete: true
4635
+ });
4636
+ if (onPartial) {
4637
+ onPartial(
4638
+ JSON.stringify({
4639
+ processing_delta: {
4640
+ type: "chain_completed"
4641
+ }
4642
+ })
4643
+ );
4644
+ }
4645
+ }
4533
4646
  }
4534
4647
  }
4535
4648
  return;
@@ -4713,6 +4826,9 @@ var LangGraphEngine = class {
4713
4826
  constructor(eventProcessor, configService) {
4714
4827
  this.eventProcessor = eventProcessor;
4715
4828
  this.configService = configService;
4829
+ if (!eventProcessor) {
4830
+ this.logger.error("EventProcessor is undefined/null!");
4831
+ }
4716
4832
  }
4717
4833
  logger = new Logger(LangGraphEngine.name);
4718
4834
  /**
@@ -4815,8 +4931,8 @@ var LangGraphEngine = class {
4815
4931
  */
4816
4932
  async sendMetricsWebhook(payload) {
4817
4933
  try {
4818
- const backendUrl = this.configService.get("API_URL") || "http://amelie-service";
4819
- const internalToken = this.configService.get("INTERNAL_API_TOKEN");
4934
+ const backendUrl = this.configService?.get("API_URL") || "http://amelie-service";
4935
+ const internalToken = this.configService?.get("INTERNAL_API_TOKEN");
4820
4936
  if (!internalToken) {
4821
4937
  this.logger.warn(
4822
4938
  "[METRICS-WEBHOOK] INTERNAL_API_TOKEN not configured, skipping webhook"
@@ -4855,8 +4971,8 @@ var LangGraphEngine = class {
4855
4971
  }
4856
4972
  async sendTraceEventsBatch(payload) {
4857
4973
  try {
4858
- const backendUrl = this.configService.get("API_URL") || "http://amelie-service";
4859
- const internalToken = this.configService.get("INTERNAL_API_TOKEN");
4974
+ const backendUrl = this.configService?.get("API_URL") || "http://amelie-service";
4975
+ const internalToken = this.configService?.get("INTERNAL_API_TOKEN");
4860
4976
  if (!internalToken) {
4861
4977
  this.logger.warn(
4862
4978
  "[TRACE-EVENTS-BATCH] INTERNAL_API_TOKEN not configured, skipping batch webhook"
@@ -4938,7 +5054,8 @@ var LangGraphEngine = class {
4938
5054
  }
4939
5055
  };
4940
5056
  LangGraphEngine = __decorateClass([
4941
- Injectable()
5057
+ Injectable(),
5058
+ __decorateParam(1, Optional())
4942
5059
  ], LangGraphEngine);
4943
5060
 
4944
5061
  // src/core/universal-graph.module.ts
@@ -5014,9 +5131,16 @@ var UniversalGraphModule = class {
5014
5131
  // Discovery services from @nestjs/core
5015
5132
  MetadataScanner,
5016
5133
  // Event processor for stream handling
5017
- EventProcessor,
5134
+ {
5135
+ provide: EventProcessor,
5136
+ useFactory: () => new EventProcessor()
5137
+ },
5018
5138
  // Graph engines
5019
- LangGraphEngine,
5139
+ {
5140
+ provide: LangGraphEngine,
5141
+ useFactory: (eventProcessor) => new LangGraphEngine(eventProcessor, void 0),
5142
+ inject: [EventProcessor]
5143
+ },
5020
5144
  BuilderRegistryService,
5021
5145
  GraphEngineFactory,
5022
5146
  VersionedGraphService,
@@ -5122,12 +5246,8 @@ var UniversalGraphModule = class {
5122
5246
  },
5123
5247
  {
5124
5248
  provide: "GRAPH_ENGINE",
5125
- useFactory: (factory) => {
5126
- return factory.getEngine(
5127
- options.engineType || "langgraph" /* LANGGRAPH */
5128
- );
5129
- },
5130
- inject: [GraphEngineFactory]
5249
+ useFactory: (langGraphEngine) => langGraphEngine,
5250
+ inject: [LangGraphEngine]
5131
5251
  },
5132
5252
  {
5133
5253
  provide: "GRAPH_BUILDERS",