@autobe/agent 0.30.0-dev.20260315 → 0.30.1
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/LICENSE +661 -661
- package/lib/constants/AutoBeTemplateFileConstant.d.ts +1 -1
- package/lib/factory/consentFunctionCall.js +4 -4
- package/lib/factory/createAutoBeContext.js +24 -24
- package/lib/factory/createAutoBeMessageContent.js +6 -6
- package/lib/index.mjs +41 -41
- package/lib/orchestrate/analyze/histories/transformAnalyzeScenarioHistory.js +18 -18
- package/lib/orchestrate/analyze/histories/transformAnalyzeScenarioReviewHistory.js +13 -13
- package/lib/orchestrate/analyze/histories/transformAnalyzeSectionCrossFileReviewHistory.js +47 -47
- package/lib/orchestrate/analyze/histories/transformAnalyzeSectionReviewHistory.js +66 -66
- package/lib/orchestrate/analyze/histories/transformAnalyzeWriteSectionHistory.js +91 -91
- package/lib/orchestrate/analyze/histories/transformAnalyzeWriteSectionPatchHistory.js +46 -46
- package/lib/orchestrate/analyze/histories/transformAnalyzeWriteUnitHistory.js +72 -72
- package/lib/orchestrate/analyze/orchestrateAnalyzeScenario.js +1 -1
- package/lib/orchestrate/analyze/orchestrateAnalyzeSectionCrossFileReview.js +1 -1
- package/lib/orchestrate/analyze/orchestrateAnalyzeSectionReview.js +1 -1
- package/lib/orchestrate/analyze/orchestrateAnalyzeWriteSection.js +1 -1
- package/lib/orchestrate/analyze/orchestrateAnalyzeWriteSectionPatch.js +1 -1
- package/lib/orchestrate/analyze/orchestrateAnalyzeWriteUnit.js +1 -1
- package/lib/orchestrate/common/histories/transformCommonCorrectCastingHistory.js +5 -5
- package/lib/orchestrate/common/histories/transformPreliminaryHistory.js +44 -44
- package/lib/orchestrate/common/histories/transformPreviousAndLatestCorrectHistory.js +44 -44
- package/lib/orchestrate/common/internal/fixPrelminaryApplication.js +22 -22
- package/lib/orchestrate/common/internal/validatePreliminary.js +116 -116
- package/lib/orchestrate/facade/createAutoBeFacadeController.js +3 -3
- package/lib/orchestrate/facade/structures/transformFacadeStateMessage.js +20 -20
- package/lib/orchestrate/interface/histories/transformInterfaceActionEndpointReviewHistory.js +34 -34
- package/lib/orchestrate/interface/histories/transformInterfaceActionEndpointWriteHistory.js +37 -37
- package/lib/orchestrate/interface/histories/transformInterfaceAuthorizationHistory.js +56 -56
- package/lib/orchestrate/interface/histories/transformInterfaceBaseEndpointReviewHistory.js +26 -26
- package/lib/orchestrate/interface/histories/transformInterfaceBaseEndpointWriteHistory.js +28 -28
- package/lib/orchestrate/interface/histories/transformInterfaceEndpointAuthorizationSection.js +11 -11
- package/lib/orchestrate/interface/histories/transformInterfaceGroupHistory.js +38 -38
- package/lib/orchestrate/interface/histories/transformInterfaceOperationHistory.js +29 -29
- package/lib/orchestrate/interface/histories/transformInterfaceOperationParameterHistory.js +15 -15
- package/lib/orchestrate/interface/histories/transformInterfaceOperationReviewHistory.js +6 -6
- package/lib/orchestrate/interface/histories/transformInterfacePrerequisiteHistory.js +25 -25
- package/lib/orchestrate/interface/histories/transformInterfaceSchemaCastingHistory.js +67 -67
- package/lib/orchestrate/interface/histories/transformInterfaceSchemaComplementHistory.js +60 -60
- package/lib/orchestrate/interface/histories/transformInterfaceSchemaRefineHistory.js +83 -83
- package/lib/orchestrate/interface/histories/transformInterfaceSchemaRenameHistory.js +29 -29
- package/lib/orchestrate/interface/histories/transformInterfaceSchemaReviewHistory.js +74 -74
- package/lib/orchestrate/interface/histories/transformInterfaceSchemaWriteHistory.js +46 -46
- package/lib/orchestrate/interface/orchestrateInterfaceAuthorization.js +1 -1
- package/lib/orchestrate/interface/orchestrateInterfaceEndpointReview.js +1 -1
- package/lib/orchestrate/interface/orchestrateInterfaceEndpointWrite.js +1 -1
- package/lib/orchestrate/interface/orchestrateInterfaceGroup.js +9 -9
- package/lib/orchestrate/interface/orchestrateInterfaceOperation.js +1 -1
- package/lib/orchestrate/interface/orchestrateInterfaceOperationReview.js +1 -1
- package/lib/orchestrate/interface/orchestrateInterfacePrerequisite.js +4 -4
- package/lib/orchestrate/interface/orchestrateInterfaceSchemaCasting.js +1 -1
- package/lib/orchestrate/interface/orchestrateInterfaceSchemaComplement.js +5 -5
- package/lib/orchestrate/interface/orchestrateInterfaceSchemaRefine.js +1 -1
- package/lib/orchestrate/interface/orchestrateInterfaceSchemaReview.js +1 -1
- package/lib/orchestrate/interface/orchestrateInterfaceSchemaWrite.js +1 -1
- package/lib/orchestrate/interface/programmers/AutoBeInterfaceAuthorizationProgrammer.js +90 -90
- package/lib/orchestrate/interface/programmers/AutoBeInterfaceEndpointProgrammer.js +6 -6
- package/lib/orchestrate/interface/programmers/AutoBeInterfaceEndpointReviewProgrammer.js +25 -25
- package/lib/orchestrate/interface/programmers/AutoBeInterfaceOperationProgrammer.js +71 -71
- package/lib/orchestrate/interface/programmers/AutoBeInterfacePrerequisiteProgrammer.js +162 -162
- package/lib/orchestrate/interface/programmers/AutoBeInterfaceSchemaProgrammer.js +11 -11
- package/lib/orchestrate/interface/programmers/AutoBeInterfaceSchemaPropertyReviseProgrammer.js +73 -73
- package/lib/orchestrate/interface/programmers/AutoBeInterfaceSchemaRefineProgrammer.js +35 -35
- package/lib/orchestrate/interface/programmers/AutoBeInterfaceSchemaReviewProgrammer.js +11 -11
- package/lib/orchestrate/interface/utils/AutoBeJsonSchemaFactory.js +4 -4
- package/lib/orchestrate/interface/utils/AutoBeJsonSchemaValidator.js +283 -283
- package/lib/orchestrate/interface/utils/fulfillJsonSchemaErrorMessages.js +76 -76
- package/lib/orchestrate/prisma/histories/transformPrismaAuthorizationHistory.js +76 -76
- package/lib/orchestrate/prisma/histories/transformPrismaAuthorizationReviewHistory.js +51 -51
- package/lib/orchestrate/prisma/histories/transformPrismaComponentReviewHistory.js +54 -54
- package/lib/orchestrate/prisma/histories/transformPrismaComponentsHistory.js +83 -83
- package/lib/orchestrate/prisma/histories/transformPrismaCorrectHistory.js +6 -6
- package/lib/orchestrate/prisma/histories/transformPrismaGroupHistory.js +22 -22
- package/lib/orchestrate/prisma/histories/transformPrismaGroupReviewHistory.js +41 -41
- package/lib/orchestrate/prisma/histories/transformPrismaSchemaHistory.js +39 -39
- package/lib/orchestrate/prisma/histories/transformPrismaSchemaReviewHistory.js +41 -41
- package/lib/orchestrate/prisma/orchestratePrismaAuthorization.js +1 -1
- package/lib/orchestrate/prisma/orchestratePrismaAuthorizationReview.js +1 -1
- package/lib/orchestrate/prisma/orchestratePrismaComponent.js +1 -1
- package/lib/orchestrate/prisma/orchestratePrismaComponentReview.js +1 -1
- package/lib/orchestrate/prisma/orchestratePrismaCorrect.js +1 -1
- package/lib/orchestrate/prisma/orchestratePrismaGroup.js +1 -1
- package/lib/orchestrate/prisma/orchestratePrismaGroupReview.js +1 -1
- package/lib/orchestrate/prisma/orchestratePrismaSchema.js +1 -1
- package/lib/orchestrate/prisma/orchestratePrismaSchemaReview.js +1 -1
- package/lib/orchestrate/prisma/programmers/AutoBeDatabaseAuthorizationProgrammer.js +23 -23
- package/lib/orchestrate/prisma/programmers/AutoBeDatabaseAuthorizationReviewProgrammer.js +7 -7
- package/lib/orchestrate/prisma/programmers/AutoBeDatabaseComponentProgrammer.js +5 -5
- package/lib/orchestrate/prisma/programmers/AutoBeDatabaseComponentReviewProgrammer.js +20 -20
- package/lib/orchestrate/prisma/programmers/AutoBeDatabaseGroupProgrammer.js +13 -13
- package/lib/orchestrate/prisma/programmers/AutoBeDatabaseGroupReviewProgrammer.js +51 -51
- package/lib/orchestrate/prisma/programmers/AutoBeDatabaseSchemaProgrammer.js +25 -25
- package/lib/orchestrate/realize/histories/transformRealizeAuthorizationCorrectHistory.js +28 -28
- package/lib/orchestrate/realize/histories/transformRealizeAuthorizationWriteHistory.js +14 -14
- package/lib/orchestrate/realize/histories/transformRealizeCollectorCorrectHistory.js +42 -42
- package/lib/orchestrate/realize/histories/transformRealizeCollectorPlanHistory.js +27 -27
- package/lib/orchestrate/realize/histories/transformRealizeCollectorWriteHistory.js +57 -57
- package/lib/orchestrate/realize/histories/transformRealizeCorrectCastingHistory.js +35 -35
- package/lib/orchestrate/realize/histories/transformRealizeOperationCorrectHistory.js +14 -14
- package/lib/orchestrate/realize/histories/transformRealizeOperationWriteHistory.js +37 -37
- package/lib/orchestrate/realize/histories/transformRealizeOperationWriteHistory.js.map +1 -1
- package/lib/orchestrate/realize/histories/transformRealizeTransformerCorrectHistory.js +47 -47
- package/lib/orchestrate/realize/histories/transformRealizeTransformerPlanHistory.js +27 -27
- package/lib/orchestrate/realize/histories/transformRealizeTransformerWriteHistory.js +51 -51
- package/lib/orchestrate/realize/orchestrateRealizeAuthorizationCorrect.js +1 -1
- package/lib/orchestrate/realize/orchestrateRealizeAuthorizationWrite.js +1 -1
- package/lib/orchestrate/realize/orchestrateRealizeCollectorCorrectOverall.js +1 -1
- package/lib/orchestrate/realize/orchestrateRealizeCollectorPlan.js +11 -11
- package/lib/orchestrate/realize/orchestrateRealizeCollectorWrite.js +1 -1
- package/lib/orchestrate/realize/orchestrateRealizeOperationCorrectOverall.js +1 -1
- package/lib/orchestrate/realize/orchestrateRealizeOperationWrite.js +1 -1
- package/lib/orchestrate/realize/orchestrateRealizeTransformerCorrectOverall.js +1 -1
- package/lib/orchestrate/realize/orchestrateRealizeTransformerPlan.js +11 -11
- package/lib/orchestrate/realize/orchestrateRealizeTransformerWrite.js +1 -1
- package/lib/orchestrate/realize/programmers/AutoBeRealizeCollectorProgrammer.js +41 -41
- package/lib/orchestrate/realize/programmers/AutoBeRealizeOperationProgrammer.js +21 -21
- package/lib/orchestrate/realize/programmers/AutoBeRealizeTransformerProgrammer.js +57 -57
- package/lib/orchestrate/realize/utils/getRealizeWriteInputType.js +3 -3
- package/lib/orchestrate/realize/utils/printErrorHints.js +5 -5
- package/lib/orchestrate/test/compile/getTestImportStatements.js +9 -9
- package/lib/orchestrate/test/histories/transformTestAuthorizeWriteHistory.js +38 -38
- package/lib/orchestrate/test/histories/transformTestGenerationWriteHistory.js +79 -79
- package/lib/orchestrate/test/histories/transformTestOperationWriteHistory.js +162 -162
- package/lib/orchestrate/test/histories/transformTestPrepareWriteHistory.js +54 -54
- package/lib/orchestrate/test/histories/transformTestScenarioHistory.js +31 -31
- package/lib/orchestrate/test/histories/transformTestScenarioReviewHistory.js +27 -27
- package/lib/orchestrate/test/orchestrateTestScenario.js +1 -1
- package/lib/orchestrate/test/orchestrateTestScenarioReview.js +1 -1
- package/lib/orchestrate/test/programmers/AutoBeTestAuthorizeProgrammer.js +31 -31
- package/lib/orchestrate/test/programmers/AutoBeTestGenerateProgrammer.js +23 -23
- package/lib/orchestrate/test/programmers/AutoBeTestOperationProgrammer.js +6 -6
- package/lib/orchestrate/test/programmers/AutoBeTestPrepareProgrammer.js +34 -34
- package/lib/orchestrate/test/programmers/AutoBeTestScenarioProgrammer.js +39 -39
- package/lib/utils/predicateStateMessage.js +19 -19
- package/lib/utils/validateEmptyCode.js +16 -16
- package/package.json +5 -5
- package/src/AutoBeAgent.ts +374 -374
- package/src/AutoBeAgentBase.ts +126 -126
- package/src/AutoBeMockAgent.ts +254 -254
- package/src/constants/AutoBeConfigConstant.ts +173 -173
- package/src/constants/AutoBeTemplateFileConstant.ts +1 -1
- package/src/context/AutoBeContext.ts +107 -107
- package/src/context/AutoBeState.ts +66 -66
- package/src/context/AutoBeTokenUsage.ts +307 -307
- package/src/context/AutoBeTokenUsageComponent.ts +227 -227
- package/src/describe/describe.ts +47 -47
- package/src/describe/image/histories/transformImageDescribeDraftHistories.ts +16 -16
- package/src/describe/image/orchestrateImageDescribeDraft.ts +200 -200
- package/src/describe/image/structures/IAutoBeImageDescribeDraftApplication.ts +92 -92
- package/src/describe/imageDescribe.ts +64 -64
- package/src/factory/consentFunctionCall.ts +141 -141
- package/src/factory/createAgenticaHistory.ts +71 -71
- package/src/factory/createAutoBeContext.ts +584 -584
- package/src/factory/createAutoBeMessageContent.ts +36 -36
- package/src/factory/createAutoBeState.ts +20 -20
- package/src/factory/getAutoBeGenerated.ts +317 -317
- package/src/factory/getAutoBeRealizeGenerated.ts +31 -31
- package/src/factory/getCriticalCompiler.ts +52 -52
- package/src/factory/index.ts +1 -1
- package/src/factory/mergeSystemMessages.ts +60 -60
- package/src/factory/supportFunctionCallFallback.ts +214 -214
- package/src/factory/supportMistral.ts +138 -138
- package/src/index.ts +16 -16
- package/src/orchestrate/analyze/fillTocDeterministic.ts +280 -280
- package/src/orchestrate/analyze/histories/transformAnalyzeScenarioHistory.ts +55 -55
- package/src/orchestrate/analyze/histories/transformAnalyzeScenarioReviewHistory.ts +74 -74
- package/src/orchestrate/analyze/histories/transformAnalyzeSectionCrossFileReviewHistory.ts +179 -179
- package/src/orchestrate/analyze/histories/transformAnalyzeSectionReviewHistory.ts +163 -163
- package/src/orchestrate/analyze/histories/transformAnalyzeWriteSectionHistory.ts +191 -191
- package/src/orchestrate/analyze/histories/transformAnalyzeWriteSectionPatchHistory.ts +122 -122
- package/src/orchestrate/analyze/histories/transformAnalyzeWriteUnitHistory.ts +161 -161
- package/src/orchestrate/analyze/index.ts +7 -7
- package/src/orchestrate/analyze/orchestrateAnalyze.ts +1693 -1693
- package/src/orchestrate/analyze/orchestrateAnalyzeScenario.ts +267 -267
- package/src/orchestrate/analyze/orchestrateAnalyzeScenarioReview.ts +122 -122
- package/src/orchestrate/analyze/orchestrateAnalyzeSectionCrossFileReview.ts +146 -146
- package/src/orchestrate/analyze/orchestrateAnalyzeSectionReview.ts +154 -154
- package/src/orchestrate/analyze/orchestrateAnalyzeWriteSection.ts +388 -388
- package/src/orchestrate/analyze/orchestrateAnalyzeWriteSectionPatch.ts +407 -407
- package/src/orchestrate/analyze/orchestrateAnalyzeWriteUnit.ts +348 -348
- package/src/orchestrate/analyze/programmers/AutoBeAnalyzeProgrammer.ts +149 -149
- package/src/orchestrate/analyze/structures/FixedAnalyzeTemplate.ts +961 -961
- package/src/orchestrate/analyze/structures/IAutoBeAnalyzeScenarioApplication.ts +141 -141
- package/src/orchestrate/analyze/structures/IAutoBeAnalyzeScenarioReviewApplication.ts +96 -96
- package/src/orchestrate/analyze/structures/IAutoBeAnalyzeSectionCrossFileReviewApplication.ts +160 -160
- package/src/orchestrate/analyze/structures/IAutoBeAnalyzeSectionReviewApplication.ts +192 -192
- package/src/orchestrate/analyze/structures/IAutoBeAnalyzeWriteSectionApplication.ts +142 -142
- package/src/orchestrate/analyze/structures/IAutoBeAnalyzeWriteUnitApplication.ts +123 -123
- package/src/orchestrate/analyze/utils/buildConstraintConsistencyReport.ts +813 -813
- package/src/orchestrate/analyze/utils/buildErrorCodeRegistry.ts +324 -324
- package/src/orchestrate/analyze/utils/buildHardValidators.ts +88 -88
- package/src/orchestrate/analyze/utils/detectInventedEntities.ts +87 -87
- package/src/orchestrate/analyze/utils/detectProseConstraintConflicts.ts +319 -319
- package/src/orchestrate/analyze/utils/repairSectionReviewUtils.ts +181 -181
- package/src/orchestrate/analyze/utils/repairUtils.ts +60 -60
- package/src/orchestrate/analyze/utils/validateScenarioBasics.ts +104 -104
- package/src/orchestrate/common/AutoBePreliminaryController.ts +400 -400
- package/src/orchestrate/common/histories/transformCommonCorrectCastingHistory.ts +32 -32
- package/src/orchestrate/common/histories/transformPreliminaryHistory.ts +742 -742
- package/src/orchestrate/common/histories/transformPreviousAndLatestCorrectHistory.ts +77 -77
- package/src/orchestrate/common/internal/complementPreliminaryCollection.ts +263 -263
- package/src/orchestrate/common/internal/convertToSectionEntries.ts +57 -57
- package/src/orchestrate/common/internal/createPreliminaryCollection.ts +76 -76
- package/src/orchestrate/common/internal/fixPrelminaryApplication.ts +346 -346
- package/src/orchestrate/common/internal/validatePreliminary.ts +671 -671
- package/src/orchestrate/common/orchestrateCommonCorrectCasting.ts +223 -223
- package/src/orchestrate/common/orchestratePreliminary.ts +610 -610
- package/src/orchestrate/common/structures/AutoBePreliminaryRequest.ts +42 -42
- package/src/orchestrate/common/structures/IAnalysisSectionEntry.ts +35 -35
- package/src/orchestrate/common/structures/IAutoBeCommonCorrectCastingApplication.ts +72 -72
- package/src/orchestrate/common/structures/IAutoBeOrchestrateResult.ts +28 -28
- package/src/orchestrate/common/structures/IAutoBePreliminaryCollection.ts +52 -52
- package/src/orchestrate/common/structures/IAutoBePreliminaryGetAnalysisSections.ts +34 -34
- package/src/orchestrate/common/structures/IAutoBePreliminaryGetDatabaseSchemas.ts +31 -31
- package/src/orchestrate/common/structures/IAutoBePreliminaryGetInterfaceOperations.ts +32 -32
- package/src/orchestrate/common/structures/IAutoBePreliminaryGetInterfaceSchemas.ts +31 -31
- package/src/orchestrate/common/structures/IAutoBePreliminaryGetPreviousAnalysisSections.ts +28 -28
- package/src/orchestrate/common/structures/IAutoBePreliminaryGetPreviousDatabaseSchemas.ts +88 -88
- package/src/orchestrate/common/structures/IAutoBePreliminaryGetPreviousInterfaceOperations.ts +102 -102
- package/src/orchestrate/common/structures/IAutoBePreliminaryGetPreviousInterfaceSchemas.ts +105 -105
- package/src/orchestrate/common/structures/IAutoBePreliminaryGetRealizeCollectors.ts +32 -32
- package/src/orchestrate/common/structures/IAutoBePreliminaryGetRealizeTransformers.ts +32 -32
- package/src/orchestrate/facade/createAutoBeFacadeController.ts +118 -118
- package/src/orchestrate/facade/histories/IAutoBeFacadeApplication.ts +139 -139
- package/src/orchestrate/facade/histories/IAutoBeFacadeApplicationProps.ts +8 -8
- package/src/orchestrate/facade/histories/IAutoBeFacadeApplicationResult.ts +9 -9
- package/src/orchestrate/facade/structures/transformFacadeStateMessage.ts +59 -59
- package/src/orchestrate/index.ts +5 -5
- package/src/orchestrate/interface/histories/transformInterfaceActionEndpointReviewHistory.ts +83 -83
- package/src/orchestrate/interface/histories/transformInterfaceActionEndpointWriteHistory.ts +78 -78
- package/src/orchestrate/interface/histories/transformInterfaceAuthorizationHistory.ts +129 -129
- package/src/orchestrate/interface/histories/transformInterfaceBaseEndpointReviewHistory.ts +74 -74
- package/src/orchestrate/interface/histories/transformInterfaceBaseEndpointWriteHistory.ts +68 -68
- package/src/orchestrate/interface/histories/transformInterfaceCommonHistory.ts +64 -64
- package/src/orchestrate/interface/histories/transformInterfaceEndpointAuthorizationSection.ts +42 -42
- package/src/orchestrate/interface/histories/transformInterfaceGroupHistory.ts +103 -103
- package/src/orchestrate/interface/histories/transformInterfaceOperationHistory.ts +81 -81
- package/src/orchestrate/interface/histories/transformInterfaceOperationParameterHistory.ts +53 -53
- package/src/orchestrate/interface/histories/transformInterfaceOperationReviewHistory.ts +49 -49
- package/src/orchestrate/interface/histories/transformInterfacePrerequisiteHistory.ts +94 -94
- package/src/orchestrate/interface/histories/transformInterfaceSchemaCastingHistory.ts +122 -122
- package/src/orchestrate/interface/histories/transformInterfaceSchemaComplementHistory.ts +192 -192
- package/src/orchestrate/interface/histories/transformInterfaceSchemaRefineHistory.ts +189 -189
- package/src/orchestrate/interface/histories/transformInterfaceSchemaRenameHistory.ts +63 -63
- package/src/orchestrate/interface/histories/transformInterfaceSchemaReviewHistory.ts +173 -173
- package/src/orchestrate/interface/histories/transformInterfaceSchemaWriteHistory.ts +88 -88
- package/src/orchestrate/interface/index.ts +6 -6
- package/src/orchestrate/interface/orchestrateInterface.ts +118 -118
- package/src/orchestrate/interface/orchestrateInterfaceActionEndpoint.ts +58 -58
- package/src/orchestrate/interface/orchestrateInterfaceAuthorization.ts +208 -208
- package/src/orchestrate/interface/orchestrateInterfaceBaseEndpoint.ts +55 -55
- package/src/orchestrate/interface/orchestrateInterfaceEndpoint.ts +67 -67
- package/src/orchestrate/interface/orchestrateInterfaceEndpointOverall.ts +80 -80
- package/src/orchestrate/interface/orchestrateInterfaceEndpointReview.ts +212 -212
- package/src/orchestrate/interface/orchestrateInterfaceEndpointWrite.ts +236 -236
- package/src/orchestrate/interface/orchestrateInterfaceGroup.ts +166 -166
- package/src/orchestrate/interface/orchestrateInterfaceOperation.ts +322 -322
- package/src/orchestrate/interface/orchestrateInterfaceOperationReview.ts +245 -245
- package/src/orchestrate/interface/orchestrateInterfacePrerequisite.ts +240 -240
- package/src/orchestrate/interface/orchestrateInterfaceSchema.ts +191 -191
- package/src/orchestrate/interface/orchestrateInterfaceSchemaCasting.ts +274 -274
- package/src/orchestrate/interface/orchestrateInterfaceSchemaComplement.ts +329 -329
- package/src/orchestrate/interface/orchestrateInterfaceSchemaRefine.ts +292 -292
- package/src/orchestrate/interface/orchestrateInterfaceSchemaRename.ts +266 -266
- package/src/orchestrate/interface/orchestrateInterfaceSchemaReview.ts +310 -310
- package/src/orchestrate/interface/orchestrateInterfaceSchemaWrite.ts +283 -283
- package/src/orchestrate/interface/programmers/AutoBeInterfaceAuthorizationProgrammer.ts +260 -260
- package/src/orchestrate/interface/programmers/AutoBeInterfaceEndpointProgrammer.ts +155 -155
- package/src/orchestrate/interface/programmers/AutoBeInterfaceEndpointReviewProgrammer.ts +139 -139
- package/src/orchestrate/interface/programmers/AutoBeInterfaceOperationProgrammer.ts +277 -277
- package/src/orchestrate/interface/programmers/AutoBeInterfacePrerequisiteProgrammer.ts +259 -259
- package/src/orchestrate/interface/programmers/AutoBeInterfaceSchemaProgrammer.ts +152 -152
- package/src/orchestrate/interface/programmers/AutoBeInterfaceSchemaPropertyReviseProgrammer.ts +347 -347
- package/src/orchestrate/interface/programmers/AutoBeInterfaceSchemaRefineProgrammer.ts +183 -183
- package/src/orchestrate/interface/programmers/AutoBeInterfaceSchemaReviewProgrammer.ts +263 -263
- package/src/orchestrate/interface/structures/IAutoBeInterfaceAuthorizationApplication.ts +135 -135
- package/src/orchestrate/interface/structures/IAutoBeInterfaceEndpointReviewApplication.ts +128 -128
- package/src/orchestrate/interface/structures/IAutoBeInterfaceEndpointWriteApplication.ts +133 -133
- package/src/orchestrate/interface/structures/IAutoBeInterfaceGroupApplication.ts +122 -122
- package/src/orchestrate/interface/structures/IAutoBeInterfaceOperationApplication.ts +178 -178
- package/src/orchestrate/interface/structures/IAutoBeInterfaceOperationReviewApplication.ts +175 -175
- package/src/orchestrate/interface/structures/IAutoBeInterfacePrerequisiteApplication.ts +130 -130
- package/src/orchestrate/interface/structures/IAutoBeInterfaceSchemaApplication.ts +121 -121
- package/src/orchestrate/interface/structures/IAutoBeInterfaceSchemaCastingApplication.ts +153 -153
- package/src/orchestrate/interface/structures/IAutoBeInterfaceSchemaComplementApplication.ts +123 -123
- package/src/orchestrate/interface/structures/IAutoBeInterfaceSchemaRefineApplication.ts +181 -181
- package/src/orchestrate/interface/structures/IAutoBeInterfaceSchemaRenameApplication.ts +45 -45
- package/src/orchestrate/interface/structures/IAutoBeInterfaceSchemaReviewApplication.ts +108 -108
- package/src/orchestrate/interface/utils/AutoBeJsonSchemaCollection.ts +42 -42
- package/src/orchestrate/interface/utils/AutoBeJsonSchemaFactory.ts +714 -714
- package/src/orchestrate/interface/utils/AutoBeJsonSchemaNamingConvention.ts +86 -86
- package/src/orchestrate/interface/utils/AutoBeJsonSchemaValidator.ts +725 -725
- package/src/orchestrate/interface/utils/fulfillJsonSchemaErrorMessages.ts +219 -219
- package/src/orchestrate/prisma/histories/transformPrismaAuthorizationHistory.ts +144 -144
- package/src/orchestrate/prisma/histories/transformPrismaAuthorizationReviewHistory.ts +104 -104
- package/src/orchestrate/prisma/histories/transformPrismaComponentReviewHistory.ts +97 -97
- package/src/orchestrate/prisma/histories/transformPrismaComponentsHistory.ts +136 -136
- package/src/orchestrate/prisma/histories/transformPrismaCorrectHistory.ts +41 -41
- package/src/orchestrate/prisma/histories/transformPrismaGroupHistory.ts +63 -63
- package/src/orchestrate/prisma/histories/transformPrismaGroupReviewHistory.ts +79 -79
- package/src/orchestrate/prisma/histories/transformPrismaSchemaHistory.ts +97 -97
- package/src/orchestrate/prisma/histories/transformPrismaSchemaReviewHistory.ts +126 -126
- package/src/orchestrate/prisma/index.ts +7 -7
- package/src/orchestrate/prisma/orchestratePrisma.ts +296 -296
- package/src/orchestrate/prisma/orchestratePrismaAuthorization.ts +173 -173
- package/src/orchestrate/prisma/orchestratePrismaAuthorizationReview.ts +183 -183
- package/src/orchestrate/prisma/orchestratePrismaComponent.ts +205 -205
- package/src/orchestrate/prisma/orchestratePrismaComponentReview.ts +205 -205
- package/src/orchestrate/prisma/orchestratePrismaCorrect.ts +265 -265
- package/src/orchestrate/prisma/orchestratePrismaGroup.ts +121 -121
- package/src/orchestrate/prisma/orchestratePrismaGroupReview.ts +141 -141
- package/src/orchestrate/prisma/orchestratePrismaSchema.ts +218 -218
- package/src/orchestrate/prisma/orchestratePrismaSchemaReview.ts +220 -220
- package/src/orchestrate/prisma/programmers/AutoBeDatabaseAuthorizationProgrammer.ts +86 -86
- package/src/orchestrate/prisma/programmers/AutoBeDatabaseAuthorizationReviewProgrammer.ts +103 -103
- package/src/orchestrate/prisma/programmers/AutoBeDatabaseComponentProgrammer.ts +80 -80
- package/src/orchestrate/prisma/programmers/AutoBeDatabaseComponentReviewProgrammer.ts +168 -168
- package/src/orchestrate/prisma/programmers/AutoBeDatabaseGroupProgrammer.ts +63 -63
- package/src/orchestrate/prisma/programmers/AutoBeDatabaseGroupReviewProgrammer.ts +210 -210
- package/src/orchestrate/prisma/programmers/AutoBeDatabaseModelProgrammer.ts +57 -57
- package/src/orchestrate/prisma/programmers/AutoBeDatabaseSchemaProgrammer.ts +92 -92
- package/src/orchestrate/prisma/structures/IAutoBeDatabaseAuthorizationApplication.ts +147 -147
- package/src/orchestrate/prisma/structures/IAutoBeDatabaseAuthorizationReviewApplication.ts +153 -153
- package/src/orchestrate/prisma/structures/IAutoBeDatabaseComponentApplication.ts +142 -142
- package/src/orchestrate/prisma/structures/IAutoBeDatabaseComponentReviewApplication.ts +151 -151
- package/src/orchestrate/prisma/structures/IAutoBeDatabaseCorrectApplication.ts +157 -157
- package/src/orchestrate/prisma/structures/IAutoBeDatabaseGroupApplication.ts +117 -117
- package/src/orchestrate/prisma/structures/IAutoBeDatabaseGroupReviewApplication.ts +154 -154
- package/src/orchestrate/prisma/structures/IAutoBeDatabaseSchemaApplication.ts +144 -144
- package/src/orchestrate/prisma/structures/IAutoBeDatabaseSchemaReviewApplication.ts +138 -138
- package/src/orchestrate/realize/histories/transformRealizeAuthorizationCorrectHistory.ts +89 -89
- package/src/orchestrate/realize/histories/transformRealizeAuthorizationWriteHistory.ts +45 -45
- package/src/orchestrate/realize/histories/transformRealizeCollectorCorrectHistory.ts +137 -137
- package/src/orchestrate/realize/histories/transformRealizeCollectorPlanHistory.ts +64 -64
- package/src/orchestrate/realize/histories/transformRealizeCollectorWriteHistory.ts +147 -147
- package/src/orchestrate/realize/histories/transformRealizeCorrectCastingHistory.ts +69 -69
- package/src/orchestrate/realize/histories/transformRealizeOperationCorrectHistory.ts +94 -94
- package/src/orchestrate/realize/histories/transformRealizeOperationWriteHistory.ts +127 -127
- package/src/orchestrate/realize/histories/transformRealizeTransformerCorrectHistory.ts +147 -147
- package/src/orchestrate/realize/histories/transformRealizeTransformerPlanHistory.ts +61 -61
- package/src/orchestrate/realize/histories/transformRealizeTransformerWriteHistory.ts +133 -133
- package/src/orchestrate/realize/histories/transformRealizeWriteMembershipHistory.ts +30 -30
- package/src/orchestrate/realize/index.ts +3 -3
- package/src/orchestrate/realize/internal/orchestrateRealizeCorrectCasting.ts +444 -444
- package/src/orchestrate/realize/internal/orchestrateRealizeCorrectOverall.ts +449 -449
- package/src/orchestrate/realize/orchestrateRealize.ts +143 -143
- package/src/orchestrate/realize/orchestrateRealizeAuthorizationCorrect.ts +207 -207
- package/src/orchestrate/realize/orchestrateRealizeAuthorizationWrite.ts +209 -209
- package/src/orchestrate/realize/orchestrateRealizeCollector.ts +41 -41
- package/src/orchestrate/realize/orchestrateRealizeCollectorCorrectCasting.ts +43 -43
- package/src/orchestrate/realize/orchestrateRealizeCollectorCorrectOverall.ts +157 -157
- package/src/orchestrate/realize/orchestrateRealizeCollectorPlan.ts +264 -264
- package/src/orchestrate/realize/orchestrateRealizeCollectorWrite.ts +225 -225
- package/src/orchestrate/realize/orchestrateRealizeOperation.ts +48 -48
- package/src/orchestrate/realize/orchestrateRealizeOperationCorrectCasting.ts +69 -69
- package/src/orchestrate/realize/orchestrateRealizeOperationCorrectOverall.ts +173 -173
- package/src/orchestrate/realize/orchestrateRealizeOperationWrite.ts +262 -262
- package/src/orchestrate/realize/orchestrateRealizeTransformer.ts +41 -41
- package/src/orchestrate/realize/orchestrateRealizeTransformerCorrectCasting.ts +38 -38
- package/src/orchestrate/realize/orchestrateRealizeTransformerCorrectOverall.ts +160 -160
- package/src/orchestrate/realize/orchestrateRealizeTransformerPlan.ts +254 -254
- package/src/orchestrate/realize/orchestrateRealizeTransformerWrite.ts +226 -226
- package/src/orchestrate/realize/programmers/AutoBeRealizeCollectorProgrammer.ts +424 -424
- package/src/orchestrate/realize/programmers/AutoBeRealizeOperationProgrammer.ts +409 -409
- package/src/orchestrate/realize/programmers/AutoBeRealizeTransformerProgrammer.ts +451 -451
- package/src/orchestrate/realize/programmers/compileRealizeFiles.ts +65 -65
- package/src/orchestrate/realize/structures/IAutoBeRealizeAuthorizationCorrectApplication.ts +119 -119
- package/src/orchestrate/realize/structures/IAutoBeRealizeAuthorizationWriteApplication.ts +167 -167
- package/src/orchestrate/realize/structures/IAutoBeRealizeCollectorCorrectApplication.ts +191 -191
- package/src/orchestrate/realize/structures/IAutoBeRealizeCollectorPlanApplication.ts +177 -177
- package/src/orchestrate/realize/structures/IAutoBeRealizeCollectorWriteApplication.ts +207 -207
- package/src/orchestrate/realize/structures/IAutoBeRealizeFunctionFailure.ts +11 -11
- package/src/orchestrate/realize/structures/IAutoBeRealizeOperationCorrectApplication.ts +134 -134
- package/src/orchestrate/realize/structures/IAutoBeRealizeOperationWriteApplication.ts +138 -138
- package/src/orchestrate/realize/structures/IAutoBeRealizeScenarioResult.ts +32 -32
- package/src/orchestrate/realize/structures/IAutoBeRealizeTransformerCorrectApplication.ts +248 -248
- package/src/orchestrate/realize/structures/IAutoBeRealizeTransformerPlanApplication.ts +150 -150
- package/src/orchestrate/realize/structures/IAutoBeRealizeTransformerWriteApplication.ts +278 -278
- package/src/orchestrate/realize/utils/AuthorizationFileSystem.ts +10 -10
- package/src/orchestrate/realize/utils/AutoBeRealizeAuthorizationFileSystem.ts +9 -9
- package/src/orchestrate/realize/utils/AutoBeRealizeAuthorizationReplaceImport.ts +62 -62
- package/src/orchestrate/realize/utils/InternalFileSystem.ts +12 -12
- package/src/orchestrate/realize/utils/ProviderFileSystem.ts +4 -4
- package/src/orchestrate/realize/utils/filterDiagnostics.ts +25 -25
- package/src/orchestrate/realize/utils/generateTS2339Hints.ts +54 -54
- package/src/orchestrate/realize/utils/getRealizeWriteImportStatements.ts +42 -42
- package/src/orchestrate/realize/utils/getRealizeWriteInputType.ts +88 -88
- package/src/orchestrate/realize/utils/printErrorHints.ts +54 -54
- package/src/orchestrate/test/compile/getTestArtifacts.ts +124 -124
- package/src/orchestrate/test/compile/getTestExternalDeclarations.ts +39 -39
- package/src/orchestrate/test/compile/getTestImportStatements.ts +27 -27
- package/src/orchestrate/test/experimental/orchestrateTestCorrect.ast +237 -237
- package/src/orchestrate/test/experimental/orchestrateTestWrite.ast +322 -322
- package/src/orchestrate/test/experimental/transformTestCorrectHistories.ast +52 -52
- package/src/orchestrate/test/histories/transformTestAuthorizeWriteHistory.ts +80 -80
- package/src/orchestrate/test/histories/transformTestCorrectOverallHistory.ts +116 -116
- package/src/orchestrate/test/histories/transformTestGenerationWriteHistory.ts +118 -118
- package/src/orchestrate/test/histories/transformTestOperationWriteHistory.ts +287 -287
- package/src/orchestrate/test/histories/transformTestPrepareWriteHistory.ts +95 -95
- package/src/orchestrate/test/histories/transformTestScenarioHistory.ts +120 -120
- package/src/orchestrate/test/histories/transformTestScenarioReviewHistory.ts +99 -99
- package/src/orchestrate/test/histories/transformTestValidateEvent.ts +11 -11
- package/src/orchestrate/test/index.ts +5 -5
- package/src/orchestrate/test/internal/orchestrateTestCorrectCasting.ts +96 -96
- package/src/orchestrate/test/internal/orchestrateTestCorrectOverall.ts +219 -219
- package/src/orchestrate/test/internal/orchestrateTestCorrectRequest.ts +206 -206
- package/src/orchestrate/test/orchestrateTest.ts +161 -161
- package/src/orchestrate/test/orchestrateTestAuthorize.ts +125 -125
- package/src/orchestrate/test/orchestrateTestAuthorizeWrite.ts +263 -263
- package/src/orchestrate/test/orchestrateTestGenerate.ts +121 -121
- package/src/orchestrate/test/orchestrateTestGenerateWrite.ts +211 -211
- package/src/orchestrate/test/orchestrateTestOperation.ts +133 -133
- package/src/orchestrate/test/orchestrateTestOperationWrite.ts +221 -221
- package/src/orchestrate/test/orchestrateTestPrepare.ts +117 -117
- package/src/orchestrate/test/orchestrateTestPrepareWrite.ts +264 -264
- package/src/orchestrate/test/orchestrateTestScenario.ts +290 -290
- package/src/orchestrate/test/orchestrateTestScenarioReview.ts +262 -262
- package/src/orchestrate/test/programmers/AutoBeTestAuthorizeProgrammer.ts +149 -149
- package/src/orchestrate/test/programmers/AutoBeTestFunctionProgrammer.ts +90 -90
- package/src/orchestrate/test/programmers/AutoBeTestGenerateProgrammer.ts +170 -170
- package/src/orchestrate/test/programmers/AutoBeTestOperationProgrammer.ts +168 -168
- package/src/orchestrate/test/programmers/AutoBeTestPrepareProgrammer.ts +261 -261
- package/src/orchestrate/test/programmers/AutoBeTestScenarioProgrammer.ts +316 -316
- package/src/orchestrate/test/structures/IAutoBeTestArtifacts.ts +8 -8
- package/src/orchestrate/test/structures/IAutoBeTestAuthorizationWriteApplication.ts +113 -113
- package/src/orchestrate/test/structures/IAutoBeTestAuthorizeWriteResult.ts +10 -10
- package/src/orchestrate/test/structures/IAutoBeTestCorrectOverallApplication.ts +134 -134
- package/src/orchestrate/test/structures/IAutoBeTestCorrectRequestApplication.ts +152 -152
- package/src/orchestrate/test/structures/IAutoBeTestFunction.ts +10 -10
- package/src/orchestrate/test/structures/IAutoBeTestFunctionFailure.ts +10 -10
- package/src/orchestrate/test/structures/IAutoBeTestGenerateProcedure.ts +15 -15
- package/src/orchestrate/test/structures/IAutoBeTestGenerationWriteApplication.ts +145 -145
- package/src/orchestrate/test/structures/IAutoBeTestOperationProcedure.ts +17 -17
- package/src/orchestrate/test/structures/IAutoBeTestOperationWriteApplication.ts +162 -162
- package/src/orchestrate/test/structures/IAutoBeTestPrepareCorrectOverallApplication.ts +194 -194
- package/src/orchestrate/test/structures/IAutoBeTestPrepareProcedure.ts +8 -8
- package/src/orchestrate/test/structures/IAutoBeTestPrepareWriteApplication.ts +147 -147
- package/src/orchestrate/test/structures/IAutoBeTestProcedure.ts +10 -10
- package/src/orchestrate/test/structures/IAutoBeTestScenarioApplication.ts +105 -105
- package/src/orchestrate/test/structures/IAutoBeTestScenarioArtifacts.ts +8 -8
- package/src/orchestrate/test/structures/IAutoBeTestScenarioAuthorizationActor.ts +7 -7
- package/src/orchestrate/test/structures/IAutoBeTestScenarioReviewApplication.ts +133 -133
- package/src/orchestrate/test/utils/getPrerequisites.ts +51 -51
- package/src/orchestrate/test/utils/getReferenceIds.ts +26 -26
- package/src/orchestrate/test/utils/insertScriptToTestResult.ts +14 -14
- package/src/structures/IAutoBeConfig.ts +110 -110
- package/src/structures/IAutoBeOrchestrateHistory.ts +44 -44
- package/src/structures/IAutoBeProps.ts +102 -102
- package/src/structures/IAutoBeVendor.ts +113 -113
- package/src/utils/AutoBePreliminaryExhaustedError.ts +30 -30
- package/src/utils/AutoBeTimeoutError.ts +35 -35
- package/src/utils/EmbeddingProvider.ts +4 -4
- package/src/utils/LocalEmbeddingProvider.ts +145 -145
- package/src/utils/RAGRetrieval.ts +411 -411
- package/src/utils/TimedConversation.ts +125 -125
- package/src/utils/backoffRetry.ts +169 -169
- package/src/utils/divideArray.ts +35 -35
- package/src/utils/emplaceMap.ts +31 -31
- package/src/utils/executeCachedBatch.ts +67 -67
- package/src/utils/forceRetry.ts +15 -15
- package/src/utils/getEmbedder.ts +16 -16
- package/src/utils/parseTextFunctionCall.ts +437 -437
- package/src/utils/predicateStateMessage.ts +131 -131
- package/src/utils/validateEmptyCode.ts +73 -73
- package/src/utils/validateEnglishOnly.ts +224 -224
- package/README.md +0 -261
|
@@ -1,437 +1,437 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parses text-based function calls from LLM response content.
|
|
3
|
-
*
|
|
4
|
-
* Some models (e.g., Qwen3) attempt function calling but return the call as
|
|
5
|
-
* plain text/JSON in `message.content` instead of the proper `tool_calls`
|
|
6
|
-
* field. This utility extracts those text-based function calls and normalizes
|
|
7
|
-
* them into a structured format.
|
|
8
|
-
*
|
|
9
|
-
* @author Juntak
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
export interface IParsedFunctionCall {
|
|
13
|
-
/** Function name (must match one of the available tools) */
|
|
14
|
-
name: string;
|
|
15
|
-
/** Arguments as a JSON string */
|
|
16
|
-
arguments: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Extracts function calls from text content.
|
|
21
|
-
*
|
|
22
|
-
* Parsing strategy (in priority order):
|
|
23
|
-
*
|
|
24
|
-
* 1. XML `<tool_call>` / `<function_call>` tags (Qwen-style patterns)
|
|
25
|
-
* 2. Markdown code blocks
|
|
26
|
-
* 3. Raw JSON objects in content
|
|
27
|
-
* 4. JavaScript-style invocations (e.g. `process({...})`)
|
|
28
|
-
*
|
|
29
|
-
* Only returns calls whose function name matches `availableToolNames`.
|
|
30
|
-
*
|
|
31
|
-
* @param content The text content to parse
|
|
32
|
-
* @param availableToolNames List of valid function names from the request tools
|
|
33
|
-
* @returns Array of parsed function calls (empty if none found)
|
|
34
|
-
*/
|
|
35
|
-
export const parseTextFunctionCall = (
|
|
36
|
-
content: string,
|
|
37
|
-
availableToolNames: string[],
|
|
38
|
-
): IParsedFunctionCall[] => {
|
|
39
|
-
if (!content || !availableToolNames.length) return [];
|
|
40
|
-
|
|
41
|
-
const toolNameSet = new Set(availableToolNames);
|
|
42
|
-
let results: IParsedFunctionCall[] = [];
|
|
43
|
-
|
|
44
|
-
// Strategy 1: XML <tool_call> tags
|
|
45
|
-
results = parseXmlToolCalls(content, toolNameSet);
|
|
46
|
-
if (results.length > 0) return results;
|
|
47
|
-
|
|
48
|
-
// Strategy 2: Markdown code blocks
|
|
49
|
-
results = parseMarkdownCodeBlocks(content, toolNameSet);
|
|
50
|
-
if (results.length > 0) return results;
|
|
51
|
-
|
|
52
|
-
// Strategy 3: Raw JSON objects/arrays
|
|
53
|
-
results = parseRawJson(content, toolNameSet);
|
|
54
|
-
if (results.length > 0) return results;
|
|
55
|
-
|
|
56
|
-
// Strategy 4: JavaScript-style function invocations
|
|
57
|
-
results = parseFunctionInvocations(content, toolNameSet);
|
|
58
|
-
return results;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
// ──────────────────────────────────────────────
|
|
62
|
-
// Strategy 1: XML <tool_call> tags
|
|
63
|
-
// ──────────────────────────────────────────────
|
|
64
|
-
|
|
65
|
-
const XML_TOOL_CALL_RE =
|
|
66
|
-
/<(?:tool_call|function_call)>\s*([\s\S]*?)\s*<\/(?:tool_call|function_call)>/g;
|
|
67
|
-
|
|
68
|
-
const parseXmlToolCalls = (
|
|
69
|
-
content: string,
|
|
70
|
-
toolNames: Set<string>,
|
|
71
|
-
): IParsedFunctionCall[] => {
|
|
72
|
-
const results: IParsedFunctionCall[] = [];
|
|
73
|
-
let match: RegExpExecArray | null;
|
|
74
|
-
|
|
75
|
-
while ((match = XML_TOOL_CALL_RE.exec(content)) !== null) {
|
|
76
|
-
const block = match[1]!.trim();
|
|
77
|
-
|
|
78
|
-
// JSON object / array inside XML
|
|
79
|
-
const arrayCalls = tryParseArray(block, toolNames);
|
|
80
|
-
if (arrayCalls.length > 0) {
|
|
81
|
-
results.push(...arrayCalls);
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
const parsed = tryParseCandidate(block, toolNames);
|
|
85
|
-
if (parsed) {
|
|
86
|
-
results.push(parsed);
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// JS-style invocation inside XML
|
|
91
|
-
const invocationCalls = parseFunctionInvocations(block, toolNames);
|
|
92
|
-
if (invocationCalls.length > 0) results.push(...invocationCalls);
|
|
93
|
-
}
|
|
94
|
-
XML_TOOL_CALL_RE.lastIndex = 0;
|
|
95
|
-
|
|
96
|
-
return results;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const MARKDOWN_CODE_BLOCK_RE = /```(?:json)?\s*\n([\s\S]*?)\n\s*```/g;
|
|
100
|
-
|
|
101
|
-
const parseMarkdownCodeBlocks = (
|
|
102
|
-
content: string,
|
|
103
|
-
toolNames: Set<string>,
|
|
104
|
-
): IParsedFunctionCall[] => {
|
|
105
|
-
const results: IParsedFunctionCall[] = [];
|
|
106
|
-
let match: RegExpExecArray | null;
|
|
107
|
-
|
|
108
|
-
while ((match = MARKDOWN_CODE_BLOCK_RE.exec(content)) !== null) {
|
|
109
|
-
const blockContent = match[1]!.trim();
|
|
110
|
-
|
|
111
|
-
// Try as array of calls
|
|
112
|
-
const arrayCalls = tryParseArray(blockContent, toolNames);
|
|
113
|
-
if (arrayCalls.length > 0) {
|
|
114
|
-
results.push(...arrayCalls);
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Try as single call
|
|
119
|
-
const parsed = tryParseCandidate(blockContent, toolNames);
|
|
120
|
-
if (parsed) {
|
|
121
|
-
results.push(parsed);
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Try as JS invocation
|
|
126
|
-
const invocationCalls = parseFunctionInvocations(blockContent, toolNames);
|
|
127
|
-
if (invocationCalls.length > 0) results.push(...invocationCalls);
|
|
128
|
-
}
|
|
129
|
-
MARKDOWN_CODE_BLOCK_RE.lastIndex = 0;
|
|
130
|
-
|
|
131
|
-
return results;
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
const parseRawJson = (
|
|
135
|
-
content: string,
|
|
136
|
-
toolNames: Set<string>,
|
|
137
|
-
): IParsedFunctionCall[] => {
|
|
138
|
-
const trimmed = content.trim();
|
|
139
|
-
|
|
140
|
-
// Try as JSON array first
|
|
141
|
-
if (trimmed.startsWith("[")) {
|
|
142
|
-
const arrayCalls = tryParseArray(trimmed, toolNames);
|
|
143
|
-
if (arrayCalls.length > 0) return arrayCalls;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// Extract individual JSON objects via brace-matching
|
|
147
|
-
const jsonCandidates = extractJsonObjects(trimmed);
|
|
148
|
-
const results: IParsedFunctionCall[] = [];
|
|
149
|
-
|
|
150
|
-
for (const candidate of jsonCandidates) {
|
|
151
|
-
const parsed = tryParseCandidate(candidate, toolNames);
|
|
152
|
-
if (parsed) results.push(parsed);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return results;
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
const parseFunctionInvocations = (
|
|
159
|
-
content: string,
|
|
160
|
-
toolNames: Set<string>,
|
|
161
|
-
): IParsedFunctionCall[] => {
|
|
162
|
-
const results: IParsedFunctionCall[] = [];
|
|
163
|
-
const names = [...toolNames]
|
|
164
|
-
.map(escapeRegExp)
|
|
165
|
-
.sort((a, b) => b.length - a.length)
|
|
166
|
-
.join("|");
|
|
167
|
-
if (!names) return results;
|
|
168
|
-
|
|
169
|
-
// Matches e.g. process({...}) / process ( {...} )
|
|
170
|
-
const pattern = new RegExp(`\\b(${names})\\s*\\(`, "g");
|
|
171
|
-
let match: RegExpExecArray | null;
|
|
172
|
-
while ((match = pattern.exec(content)) !== null) {
|
|
173
|
-
const name = match[1]!;
|
|
174
|
-
const openIndex = content.indexOf("(", match.index + name.length);
|
|
175
|
-
if (openIndex === -1) continue;
|
|
176
|
-
const closeIndex = findMatchingParen(content, openIndex);
|
|
177
|
-
if (closeIndex === -1) continue;
|
|
178
|
-
|
|
179
|
-
const inner = content.slice(openIndex + 1, closeIndex).trim();
|
|
180
|
-
if (!inner) continue;
|
|
181
|
-
|
|
182
|
-
// Common case: single object argument
|
|
183
|
-
const objectCandidates = extractJsonObjects(inner);
|
|
184
|
-
if (objectCandidates.length > 0) {
|
|
185
|
-
const candidate = objectCandidates[0]!;
|
|
186
|
-
if (candidate.trim().length > 0) {
|
|
187
|
-
results.push({
|
|
188
|
-
name,
|
|
189
|
-
arguments: candidate,
|
|
190
|
-
});
|
|
191
|
-
pattern.lastIndex = closeIndex + 1;
|
|
192
|
-
continue;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// If the entire inner text is valid JSON (rare but possible), use it directly.
|
|
197
|
-
try {
|
|
198
|
-
JSON.parse(inner);
|
|
199
|
-
results.push({ name, arguments: inner });
|
|
200
|
-
pattern.lastIndex = closeIndex + 1;
|
|
201
|
-
continue;
|
|
202
|
-
} catch {}
|
|
203
|
-
|
|
204
|
-
pattern.lastIndex = closeIndex + 1;
|
|
205
|
-
}
|
|
206
|
-
return results;
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Attempts to parse a JSON string as a function call candidate. Handles field
|
|
211
|
-
* name normalization and OpenAI-style nesting.
|
|
212
|
-
*/
|
|
213
|
-
const tryParseCandidate = (
|
|
214
|
-
text: string,
|
|
215
|
-
toolNames: Set<string>,
|
|
216
|
-
): IParsedFunctionCall | null => {
|
|
217
|
-
let obj: Record<string, unknown>;
|
|
218
|
-
try {
|
|
219
|
-
obj = JSON.parse(text.trim());
|
|
220
|
-
} catch {
|
|
221
|
-
return null;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (typeof obj !== "object" || obj === null || Array.isArray(obj))
|
|
225
|
-
return null;
|
|
226
|
-
|
|
227
|
-
return normalizeToFunctionCall(obj, toolNames);
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
/** Tries to parse a JSON array of function call candidates. */
|
|
231
|
-
const tryParseArray = (
|
|
232
|
-
text: string,
|
|
233
|
-
toolNames: Set<string>,
|
|
234
|
-
): IParsedFunctionCall[] => {
|
|
235
|
-
let arr: unknown[];
|
|
236
|
-
try {
|
|
237
|
-
arr = JSON.parse(text.trim());
|
|
238
|
-
} catch {
|
|
239
|
-
return [];
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
if (!Array.isArray(arr)) return [];
|
|
243
|
-
|
|
244
|
-
const results: IParsedFunctionCall[] = [];
|
|
245
|
-
for (const item of arr) {
|
|
246
|
-
if (typeof item === "object" && item !== null && !Array.isArray(item)) {
|
|
247
|
-
const parsed = normalizeToFunctionCall(
|
|
248
|
-
item as Record<string, unknown>,
|
|
249
|
-
toolNames,
|
|
250
|
-
);
|
|
251
|
-
if (parsed) results.push(parsed);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return results;
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Normalizes a parsed JSON object into a function call.
|
|
259
|
-
*
|
|
260
|
-
* Handles:
|
|
261
|
-
*
|
|
262
|
-
* - Direct fields: { name: "fn", arguments: {...} }
|
|
263
|
-
* - OpenAI nesting: { function: { name: "fn", arguments: {...} } }
|
|
264
|
-
* - Various field names: name/function/function_name/tool_name,
|
|
265
|
-
* arguments/parameters/params/input
|
|
266
|
-
*/
|
|
267
|
-
const normalizeToFunctionCall = (
|
|
268
|
-
obj: Record<string, unknown>,
|
|
269
|
-
toolNames: Set<string>,
|
|
270
|
-
): IParsedFunctionCall | null => {
|
|
271
|
-
// OpenAI-like envelope: { tool_calls: [{ function: { name, arguments } }] }
|
|
272
|
-
if (Array.isArray(obj.tool_calls)) {
|
|
273
|
-
for (const item of obj.tool_calls) {
|
|
274
|
-
if (typeof item === "object" && item !== null && !Array.isArray(item)) {
|
|
275
|
-
const parsed = normalizeToFunctionCall(
|
|
276
|
-
item as Record<string, unknown>,
|
|
277
|
-
toolNames,
|
|
278
|
-
);
|
|
279
|
-
if (parsed) return parsed;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
return null;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// Envelope variant: { function_call: { name, arguments } }
|
|
286
|
-
if (
|
|
287
|
-
typeof obj.function_call === "object" &&
|
|
288
|
-
obj.function_call !== null &&
|
|
289
|
-
!Array.isArray(obj.function_call)
|
|
290
|
-
) {
|
|
291
|
-
return normalizeToFunctionCall(
|
|
292
|
-
obj.function_call as Record<string, unknown>,
|
|
293
|
-
toolNames,
|
|
294
|
-
);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
// Unwrap OpenAI-style nesting: { function: { name, arguments } }
|
|
298
|
-
if (
|
|
299
|
-
typeof obj.function === "object" &&
|
|
300
|
-
obj.function !== null &&
|
|
301
|
-
!Array.isArray(obj.function)
|
|
302
|
-
) {
|
|
303
|
-
const inner = obj.function as Record<string, unknown>;
|
|
304
|
-
if (typeof inner.name === "string") {
|
|
305
|
-
obj = { ...inner };
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// Extract function name
|
|
310
|
-
const name = extractString(obj, NAME_KEYS);
|
|
311
|
-
if (!name || !toolNames.has(name)) return null;
|
|
312
|
-
|
|
313
|
-
// Extract arguments
|
|
314
|
-
const args = extractArguments(obj);
|
|
315
|
-
|
|
316
|
-
return { name, arguments: args };
|
|
317
|
-
};
|
|
318
|
-
|
|
319
|
-
const NAME_KEYS = ["name", "function", "function_name", "tool_name"] as const;
|
|
320
|
-
const ARGS_KEYS = ["arguments", "parameters", "params", "input"] as const;
|
|
321
|
-
|
|
322
|
-
const extractString = (
|
|
323
|
-
obj: Record<string, unknown>,
|
|
324
|
-
keys: readonly string[],
|
|
325
|
-
): string | null => {
|
|
326
|
-
for (const key of keys) {
|
|
327
|
-
const val = obj[key];
|
|
328
|
-
if (typeof val === "string" && val.length > 0) return val;
|
|
329
|
-
}
|
|
330
|
-
return null;
|
|
331
|
-
};
|
|
332
|
-
|
|
333
|
-
const extractArguments = (obj: Record<string, unknown>): string => {
|
|
334
|
-
for (const key of ARGS_KEYS) {
|
|
335
|
-
const val = obj[key];
|
|
336
|
-
if (val === undefined || val === null) continue;
|
|
337
|
-
|
|
338
|
-
// Already a JSON string
|
|
339
|
-
if (typeof val === "string") {
|
|
340
|
-
// Check if it's a stringified JSON — if so, keep as-is
|
|
341
|
-
try {
|
|
342
|
-
JSON.parse(val);
|
|
343
|
-
return val;
|
|
344
|
-
} catch {
|
|
345
|
-
// Not valid JSON string, wrap it
|
|
346
|
-
return JSON.stringify(val);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
// Object or array — stringify it
|
|
351
|
-
if (typeof val === "object") {
|
|
352
|
-
return JSON.stringify(val);
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
return "{}";
|
|
357
|
-
};
|
|
358
|
-
|
|
359
|
-
const findMatchingParen = (text: string, openIndex: number): number => {
|
|
360
|
-
let depth = 0;
|
|
361
|
-
let inString = false;
|
|
362
|
-
let escape = false;
|
|
363
|
-
|
|
364
|
-
for (let i = openIndex; i < text.length; i++) {
|
|
365
|
-
const ch = text[i]!;
|
|
366
|
-
|
|
367
|
-
if (escape) {
|
|
368
|
-
escape = false;
|
|
369
|
-
continue;
|
|
370
|
-
}
|
|
371
|
-
if (ch === "\\") {
|
|
372
|
-
escape = true;
|
|
373
|
-
continue;
|
|
374
|
-
}
|
|
375
|
-
if (ch === '"') {
|
|
376
|
-
inString = !inString;
|
|
377
|
-
continue;
|
|
378
|
-
}
|
|
379
|
-
if (inString) continue;
|
|
380
|
-
|
|
381
|
-
if (ch === "(") depth++;
|
|
382
|
-
else if (ch === ")") {
|
|
383
|
-
depth--;
|
|
384
|
-
if (depth === 0) return i;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
return -1;
|
|
388
|
-
};
|
|
389
|
-
|
|
390
|
-
const escapeRegExp = (value: string): string =>
|
|
391
|
-
value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
392
|
-
|
|
393
|
-
/**
|
|
394
|
-
* Extracts complete JSON objects from text using brace-matching. Handles
|
|
395
|
-
* strings containing braces by tracking quote state.
|
|
396
|
-
*/
|
|
397
|
-
const extractJsonObjects = (text: string): string[] => {
|
|
398
|
-
const results: string[] = [];
|
|
399
|
-
let depth = 0;
|
|
400
|
-
let start = -1;
|
|
401
|
-
let inString = false;
|
|
402
|
-
let escape = false;
|
|
403
|
-
|
|
404
|
-
for (let i = 0; i < text.length; i++) {
|
|
405
|
-
const ch = text[i]!;
|
|
406
|
-
|
|
407
|
-
if (escape) {
|
|
408
|
-
escape = false;
|
|
409
|
-
continue;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
if (ch === "\\") {
|
|
413
|
-
escape = true;
|
|
414
|
-
continue;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
if (ch === '"') {
|
|
418
|
-
inString = !inString;
|
|
419
|
-
continue;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
if (inString) continue;
|
|
423
|
-
|
|
424
|
-
if (ch === "{") {
|
|
425
|
-
if (depth === 0) start = i;
|
|
426
|
-
depth++;
|
|
427
|
-
} else if (ch === "}") {
|
|
428
|
-
depth--;
|
|
429
|
-
if (depth === 0 && start !== -1) {
|
|
430
|
-
results.push(text.slice(start, i + 1));
|
|
431
|
-
start = -1;
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
return results;
|
|
437
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Parses text-based function calls from LLM response content.
|
|
3
|
+
*
|
|
4
|
+
* Some models (e.g., Qwen3) attempt function calling but return the call as
|
|
5
|
+
* plain text/JSON in `message.content` instead of the proper `tool_calls`
|
|
6
|
+
* field. This utility extracts those text-based function calls and normalizes
|
|
7
|
+
* them into a structured format.
|
|
8
|
+
*
|
|
9
|
+
* @author Juntak
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export interface IParsedFunctionCall {
|
|
13
|
+
/** Function name (must match one of the available tools) */
|
|
14
|
+
name: string;
|
|
15
|
+
/** Arguments as a JSON string */
|
|
16
|
+
arguments: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Extracts function calls from text content.
|
|
21
|
+
*
|
|
22
|
+
* Parsing strategy (in priority order):
|
|
23
|
+
*
|
|
24
|
+
* 1. XML `<tool_call>` / `<function_call>` tags (Qwen-style patterns)
|
|
25
|
+
* 2. Markdown code blocks
|
|
26
|
+
* 3. Raw JSON objects in content
|
|
27
|
+
* 4. JavaScript-style invocations (e.g. `process({...})`)
|
|
28
|
+
*
|
|
29
|
+
* Only returns calls whose function name matches `availableToolNames`.
|
|
30
|
+
*
|
|
31
|
+
* @param content The text content to parse
|
|
32
|
+
* @param availableToolNames List of valid function names from the request tools
|
|
33
|
+
* @returns Array of parsed function calls (empty if none found)
|
|
34
|
+
*/
|
|
35
|
+
export const parseTextFunctionCall = (
|
|
36
|
+
content: string,
|
|
37
|
+
availableToolNames: string[],
|
|
38
|
+
): IParsedFunctionCall[] => {
|
|
39
|
+
if (!content || !availableToolNames.length) return [];
|
|
40
|
+
|
|
41
|
+
const toolNameSet = new Set(availableToolNames);
|
|
42
|
+
let results: IParsedFunctionCall[] = [];
|
|
43
|
+
|
|
44
|
+
// Strategy 1: XML <tool_call> tags
|
|
45
|
+
results = parseXmlToolCalls(content, toolNameSet);
|
|
46
|
+
if (results.length > 0) return results;
|
|
47
|
+
|
|
48
|
+
// Strategy 2: Markdown code blocks
|
|
49
|
+
results = parseMarkdownCodeBlocks(content, toolNameSet);
|
|
50
|
+
if (results.length > 0) return results;
|
|
51
|
+
|
|
52
|
+
// Strategy 3: Raw JSON objects/arrays
|
|
53
|
+
results = parseRawJson(content, toolNameSet);
|
|
54
|
+
if (results.length > 0) return results;
|
|
55
|
+
|
|
56
|
+
// Strategy 4: JavaScript-style function invocations
|
|
57
|
+
results = parseFunctionInvocations(content, toolNameSet);
|
|
58
|
+
return results;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// ──────────────────────────────────────────────
|
|
62
|
+
// Strategy 1: XML <tool_call> tags
|
|
63
|
+
// ──────────────────────────────────────────────
|
|
64
|
+
|
|
65
|
+
const XML_TOOL_CALL_RE =
|
|
66
|
+
/<(?:tool_call|function_call)>\s*([\s\S]*?)\s*<\/(?:tool_call|function_call)>/g;
|
|
67
|
+
|
|
68
|
+
const parseXmlToolCalls = (
|
|
69
|
+
content: string,
|
|
70
|
+
toolNames: Set<string>,
|
|
71
|
+
): IParsedFunctionCall[] => {
|
|
72
|
+
const results: IParsedFunctionCall[] = [];
|
|
73
|
+
let match: RegExpExecArray | null;
|
|
74
|
+
|
|
75
|
+
while ((match = XML_TOOL_CALL_RE.exec(content)) !== null) {
|
|
76
|
+
const block = match[1]!.trim();
|
|
77
|
+
|
|
78
|
+
// JSON object / array inside XML
|
|
79
|
+
const arrayCalls = tryParseArray(block, toolNames);
|
|
80
|
+
if (arrayCalls.length > 0) {
|
|
81
|
+
results.push(...arrayCalls);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const parsed = tryParseCandidate(block, toolNames);
|
|
85
|
+
if (parsed) {
|
|
86
|
+
results.push(parsed);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// JS-style invocation inside XML
|
|
91
|
+
const invocationCalls = parseFunctionInvocations(block, toolNames);
|
|
92
|
+
if (invocationCalls.length > 0) results.push(...invocationCalls);
|
|
93
|
+
}
|
|
94
|
+
XML_TOOL_CALL_RE.lastIndex = 0;
|
|
95
|
+
|
|
96
|
+
return results;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const MARKDOWN_CODE_BLOCK_RE = /```(?:json)?\s*\n([\s\S]*?)\n\s*```/g;
|
|
100
|
+
|
|
101
|
+
const parseMarkdownCodeBlocks = (
|
|
102
|
+
content: string,
|
|
103
|
+
toolNames: Set<string>,
|
|
104
|
+
): IParsedFunctionCall[] => {
|
|
105
|
+
const results: IParsedFunctionCall[] = [];
|
|
106
|
+
let match: RegExpExecArray | null;
|
|
107
|
+
|
|
108
|
+
while ((match = MARKDOWN_CODE_BLOCK_RE.exec(content)) !== null) {
|
|
109
|
+
const blockContent = match[1]!.trim();
|
|
110
|
+
|
|
111
|
+
// Try as array of calls
|
|
112
|
+
const arrayCalls = tryParseArray(blockContent, toolNames);
|
|
113
|
+
if (arrayCalls.length > 0) {
|
|
114
|
+
results.push(...arrayCalls);
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Try as single call
|
|
119
|
+
const parsed = tryParseCandidate(blockContent, toolNames);
|
|
120
|
+
if (parsed) {
|
|
121
|
+
results.push(parsed);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Try as JS invocation
|
|
126
|
+
const invocationCalls = parseFunctionInvocations(blockContent, toolNames);
|
|
127
|
+
if (invocationCalls.length > 0) results.push(...invocationCalls);
|
|
128
|
+
}
|
|
129
|
+
MARKDOWN_CODE_BLOCK_RE.lastIndex = 0;
|
|
130
|
+
|
|
131
|
+
return results;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const parseRawJson = (
|
|
135
|
+
content: string,
|
|
136
|
+
toolNames: Set<string>,
|
|
137
|
+
): IParsedFunctionCall[] => {
|
|
138
|
+
const trimmed = content.trim();
|
|
139
|
+
|
|
140
|
+
// Try as JSON array first
|
|
141
|
+
if (trimmed.startsWith("[")) {
|
|
142
|
+
const arrayCalls = tryParseArray(trimmed, toolNames);
|
|
143
|
+
if (arrayCalls.length > 0) return arrayCalls;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Extract individual JSON objects via brace-matching
|
|
147
|
+
const jsonCandidates = extractJsonObjects(trimmed);
|
|
148
|
+
const results: IParsedFunctionCall[] = [];
|
|
149
|
+
|
|
150
|
+
for (const candidate of jsonCandidates) {
|
|
151
|
+
const parsed = tryParseCandidate(candidate, toolNames);
|
|
152
|
+
if (parsed) results.push(parsed);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return results;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const parseFunctionInvocations = (
|
|
159
|
+
content: string,
|
|
160
|
+
toolNames: Set<string>,
|
|
161
|
+
): IParsedFunctionCall[] => {
|
|
162
|
+
const results: IParsedFunctionCall[] = [];
|
|
163
|
+
const names = [...toolNames]
|
|
164
|
+
.map(escapeRegExp)
|
|
165
|
+
.sort((a, b) => b.length - a.length)
|
|
166
|
+
.join("|");
|
|
167
|
+
if (!names) return results;
|
|
168
|
+
|
|
169
|
+
// Matches e.g. process({...}) / process ( {...} )
|
|
170
|
+
const pattern = new RegExp(`\\b(${names})\\s*\\(`, "g");
|
|
171
|
+
let match: RegExpExecArray | null;
|
|
172
|
+
while ((match = pattern.exec(content)) !== null) {
|
|
173
|
+
const name = match[1]!;
|
|
174
|
+
const openIndex = content.indexOf("(", match.index + name.length);
|
|
175
|
+
if (openIndex === -1) continue;
|
|
176
|
+
const closeIndex = findMatchingParen(content, openIndex);
|
|
177
|
+
if (closeIndex === -1) continue;
|
|
178
|
+
|
|
179
|
+
const inner = content.slice(openIndex + 1, closeIndex).trim();
|
|
180
|
+
if (!inner) continue;
|
|
181
|
+
|
|
182
|
+
// Common case: single object argument
|
|
183
|
+
const objectCandidates = extractJsonObjects(inner);
|
|
184
|
+
if (objectCandidates.length > 0) {
|
|
185
|
+
const candidate = objectCandidates[0]!;
|
|
186
|
+
if (candidate.trim().length > 0) {
|
|
187
|
+
results.push({
|
|
188
|
+
name,
|
|
189
|
+
arguments: candidate,
|
|
190
|
+
});
|
|
191
|
+
pattern.lastIndex = closeIndex + 1;
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// If the entire inner text is valid JSON (rare but possible), use it directly.
|
|
197
|
+
try {
|
|
198
|
+
JSON.parse(inner);
|
|
199
|
+
results.push({ name, arguments: inner });
|
|
200
|
+
pattern.lastIndex = closeIndex + 1;
|
|
201
|
+
continue;
|
|
202
|
+
} catch {}
|
|
203
|
+
|
|
204
|
+
pattern.lastIndex = closeIndex + 1;
|
|
205
|
+
}
|
|
206
|
+
return results;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Attempts to parse a JSON string as a function call candidate. Handles field
|
|
211
|
+
* name normalization and OpenAI-style nesting.
|
|
212
|
+
*/
|
|
213
|
+
const tryParseCandidate = (
|
|
214
|
+
text: string,
|
|
215
|
+
toolNames: Set<string>,
|
|
216
|
+
): IParsedFunctionCall | null => {
|
|
217
|
+
let obj: Record<string, unknown>;
|
|
218
|
+
try {
|
|
219
|
+
obj = JSON.parse(text.trim());
|
|
220
|
+
} catch {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (typeof obj !== "object" || obj === null || Array.isArray(obj))
|
|
225
|
+
return null;
|
|
226
|
+
|
|
227
|
+
return normalizeToFunctionCall(obj, toolNames);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
/** Tries to parse a JSON array of function call candidates. */
|
|
231
|
+
const tryParseArray = (
|
|
232
|
+
text: string,
|
|
233
|
+
toolNames: Set<string>,
|
|
234
|
+
): IParsedFunctionCall[] => {
|
|
235
|
+
let arr: unknown[];
|
|
236
|
+
try {
|
|
237
|
+
arr = JSON.parse(text.trim());
|
|
238
|
+
} catch {
|
|
239
|
+
return [];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (!Array.isArray(arr)) return [];
|
|
243
|
+
|
|
244
|
+
const results: IParsedFunctionCall[] = [];
|
|
245
|
+
for (const item of arr) {
|
|
246
|
+
if (typeof item === "object" && item !== null && !Array.isArray(item)) {
|
|
247
|
+
const parsed = normalizeToFunctionCall(
|
|
248
|
+
item as Record<string, unknown>,
|
|
249
|
+
toolNames,
|
|
250
|
+
);
|
|
251
|
+
if (parsed) results.push(parsed);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return results;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Normalizes a parsed JSON object into a function call.
|
|
259
|
+
*
|
|
260
|
+
* Handles:
|
|
261
|
+
*
|
|
262
|
+
* - Direct fields: { name: "fn", arguments: {...} }
|
|
263
|
+
* - OpenAI nesting: { function: { name: "fn", arguments: {...} } }
|
|
264
|
+
* - Various field names: name/function/function_name/tool_name,
|
|
265
|
+
* arguments/parameters/params/input
|
|
266
|
+
*/
|
|
267
|
+
const normalizeToFunctionCall = (
|
|
268
|
+
obj: Record<string, unknown>,
|
|
269
|
+
toolNames: Set<string>,
|
|
270
|
+
): IParsedFunctionCall | null => {
|
|
271
|
+
// OpenAI-like envelope: { tool_calls: [{ function: { name, arguments } }] }
|
|
272
|
+
if (Array.isArray(obj.tool_calls)) {
|
|
273
|
+
for (const item of obj.tool_calls) {
|
|
274
|
+
if (typeof item === "object" && item !== null && !Array.isArray(item)) {
|
|
275
|
+
const parsed = normalizeToFunctionCall(
|
|
276
|
+
item as Record<string, unknown>,
|
|
277
|
+
toolNames,
|
|
278
|
+
);
|
|
279
|
+
if (parsed) return parsed;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Envelope variant: { function_call: { name, arguments } }
|
|
286
|
+
if (
|
|
287
|
+
typeof obj.function_call === "object" &&
|
|
288
|
+
obj.function_call !== null &&
|
|
289
|
+
!Array.isArray(obj.function_call)
|
|
290
|
+
) {
|
|
291
|
+
return normalizeToFunctionCall(
|
|
292
|
+
obj.function_call as Record<string, unknown>,
|
|
293
|
+
toolNames,
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Unwrap OpenAI-style nesting: { function: { name, arguments } }
|
|
298
|
+
if (
|
|
299
|
+
typeof obj.function === "object" &&
|
|
300
|
+
obj.function !== null &&
|
|
301
|
+
!Array.isArray(obj.function)
|
|
302
|
+
) {
|
|
303
|
+
const inner = obj.function as Record<string, unknown>;
|
|
304
|
+
if (typeof inner.name === "string") {
|
|
305
|
+
obj = { ...inner };
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Extract function name
|
|
310
|
+
const name = extractString(obj, NAME_KEYS);
|
|
311
|
+
if (!name || !toolNames.has(name)) return null;
|
|
312
|
+
|
|
313
|
+
// Extract arguments
|
|
314
|
+
const args = extractArguments(obj);
|
|
315
|
+
|
|
316
|
+
return { name, arguments: args };
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
const NAME_KEYS = ["name", "function", "function_name", "tool_name"] as const;
|
|
320
|
+
const ARGS_KEYS = ["arguments", "parameters", "params", "input"] as const;
|
|
321
|
+
|
|
322
|
+
const extractString = (
|
|
323
|
+
obj: Record<string, unknown>,
|
|
324
|
+
keys: readonly string[],
|
|
325
|
+
): string | null => {
|
|
326
|
+
for (const key of keys) {
|
|
327
|
+
const val = obj[key];
|
|
328
|
+
if (typeof val === "string" && val.length > 0) return val;
|
|
329
|
+
}
|
|
330
|
+
return null;
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
const extractArguments = (obj: Record<string, unknown>): string => {
|
|
334
|
+
for (const key of ARGS_KEYS) {
|
|
335
|
+
const val = obj[key];
|
|
336
|
+
if (val === undefined || val === null) continue;
|
|
337
|
+
|
|
338
|
+
// Already a JSON string
|
|
339
|
+
if (typeof val === "string") {
|
|
340
|
+
// Check if it's a stringified JSON — if so, keep as-is
|
|
341
|
+
try {
|
|
342
|
+
JSON.parse(val);
|
|
343
|
+
return val;
|
|
344
|
+
} catch {
|
|
345
|
+
// Not valid JSON string, wrap it
|
|
346
|
+
return JSON.stringify(val);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Object or array — stringify it
|
|
351
|
+
if (typeof val === "object") {
|
|
352
|
+
return JSON.stringify(val);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
return "{}";
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
const findMatchingParen = (text: string, openIndex: number): number => {
|
|
360
|
+
let depth = 0;
|
|
361
|
+
let inString = false;
|
|
362
|
+
let escape = false;
|
|
363
|
+
|
|
364
|
+
for (let i = openIndex; i < text.length; i++) {
|
|
365
|
+
const ch = text[i]!;
|
|
366
|
+
|
|
367
|
+
if (escape) {
|
|
368
|
+
escape = false;
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
if (ch === "\\") {
|
|
372
|
+
escape = true;
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
if (ch === '"') {
|
|
376
|
+
inString = !inString;
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (inString) continue;
|
|
380
|
+
|
|
381
|
+
if (ch === "(") depth++;
|
|
382
|
+
else if (ch === ")") {
|
|
383
|
+
depth--;
|
|
384
|
+
if (depth === 0) return i;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return -1;
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
const escapeRegExp = (value: string): string =>
|
|
391
|
+
value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Extracts complete JSON objects from text using brace-matching. Handles
|
|
395
|
+
* strings containing braces by tracking quote state.
|
|
396
|
+
*/
|
|
397
|
+
const extractJsonObjects = (text: string): string[] => {
|
|
398
|
+
const results: string[] = [];
|
|
399
|
+
let depth = 0;
|
|
400
|
+
let start = -1;
|
|
401
|
+
let inString = false;
|
|
402
|
+
let escape = false;
|
|
403
|
+
|
|
404
|
+
for (let i = 0; i < text.length; i++) {
|
|
405
|
+
const ch = text[i]!;
|
|
406
|
+
|
|
407
|
+
if (escape) {
|
|
408
|
+
escape = false;
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (ch === "\\") {
|
|
413
|
+
escape = true;
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (ch === '"') {
|
|
418
|
+
inString = !inString;
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (inString) continue;
|
|
423
|
+
|
|
424
|
+
if (ch === "{") {
|
|
425
|
+
if (depth === 0) start = i;
|
|
426
|
+
depth++;
|
|
427
|
+
} else if (ch === "}") {
|
|
428
|
+
depth--;
|
|
429
|
+
if (depth === 0 && start !== -1) {
|
|
430
|
+
results.push(text.slice(start, i + 1));
|
|
431
|
+
start = -1;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
return results;
|
|
437
|
+
};
|