@yrpri/api 9.0.103 → 9.0.104
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/package.json +1 -1
- package/dist/active-citizen/controllers/activities.cjs +0 -174
- package/dist/active-citizen/controllers/news_feeds.cjs +0 -96
- package/dist/active-citizen/controllers/notifications.cjs +0 -243
- package/dist/active-citizen/controllers/recommendations.cjs +0 -280
- package/dist/active-citizen/engine/allOurIdeas/aiHelper.js +0 -204
- package/dist/active-citizen/engine/allOurIdeas/explainAnswersAssistant.js +0 -66
- package/dist/active-citizen/engine/allOurIdeas/iconGenerator.js +0 -38
- package/dist/active-citizen/engine/analytics/export_anon_community_activities.js +0 -334
- package/dist/active-citizen/engine/analytics/importer.js +0 -308
- package/dist/active-citizen/engine/analytics/manager.cjs +0 -377
- package/dist/active-citizen/engine/analytics/plausible/manager.cjs +0 -500
- package/dist/active-citizen/engine/analytics/statsCalc.cjs +0 -194
- package/dist/active-citizen/engine/analytics/utils.cjs +0 -369
- package/dist/active-citizen/engine/marketing/campaign.cjs +0 -90
- package/dist/active-citizen/engine/moderation/fraud/CreateFraudAuditReport.cjs +0 -313
- package/dist/active-citizen/engine/moderation/fraud/FraudBase.cjs +0 -239
- package/dist/active-citizen/engine/moderation/fraud/FraudDeleteBase.cjs +0 -211
- package/dist/active-citizen/engine/moderation/fraud/FraudDeleteEndorsements.cjs +0 -120
- package/dist/active-citizen/engine/moderation/fraud/FraudDeletePointQualities.cjs +0 -114
- package/dist/active-citizen/engine/moderation/fraud/FraudDeletePoints.cjs +0 -101
- package/dist/active-citizen/engine/moderation/fraud/FraudDeletePosts.cjs +0 -84
- package/dist/active-citizen/engine/moderation/fraud/FraudDeleteRatings.cjs +0 -15
- package/dist/active-citizen/engine/moderation/fraud/FraudGetBase.cjs +0 -133
- package/dist/active-citizen/engine/moderation/fraud/FraudGetEndorsements.cjs +0 -185
- package/dist/active-citizen/engine/moderation/fraud/FraudGetPointQualities.cjs +0 -184
- package/dist/active-citizen/engine/moderation/fraud/FraudGetPoints.cjs +0 -170
- package/dist/active-citizen/engine/moderation/fraud/FraudGetPosts.cjs +0 -109
- package/dist/active-citizen/engine/moderation/fraud/FraudGetRatings.cjs +0 -140
- package/dist/active-citizen/engine/moderation/fraud/FraudScannerNotifier.cjs +0 -279
- package/dist/active-citizen/engine/moderation/get_moderation_items.cjs +0 -386
- package/dist/active-citizen/engine/moderation/image_labeling/CommunityLabeling.cjs +0 -49
- package/dist/active-citizen/engine/moderation/image_labeling/GroupLabeling.cjs +0 -68
- package/dist/active-citizen/engine/moderation/image_labeling/ImageLabelingBase.cjs +0 -288
- package/dist/active-citizen/engine/moderation/image_labeling/PointLabeling.cjs +0 -33
- package/dist/active-citizen/engine/moderation/image_labeling/PostLabeling.cjs +0 -56
- package/dist/active-citizen/engine/moderation/perspective_api_client.cjs +0 -106
- package/dist/active-citizen/engine/moderation/process_moderation_items.cjs +0 -344
- package/dist/active-citizen/engine/moderation/toxicity_analysis.cjs +0 -810
- package/dist/active-citizen/engine/news_feeds/activity_and_item_index_definitions.cjs +0 -15
- package/dist/active-citizen/engine/news_feeds/generate_dynamically.cjs +0 -362
- package/dist/active-citizen/engine/news_feeds/generate_from_notifications.cjs +0 -268
- package/dist/active-citizen/engine/news_feeds/news_feeds_utils.cjs +0 -439
- package/dist/active-citizen/engine/notifications/emails_utils.cjs +0 -569
- package/dist/active-citizen/engine/notifications/generate_point_notifications.cjs +0 -233
- package/dist/active-citizen/engine/notifications/generate_post_notifications.cjs +0 -118
- package/dist/active-citizen/engine/notifications/generate_post_status_change_notifications.cjs +0 -41
- package/dist/active-citizen/engine/notifications/notifications_utils.cjs +0 -148
- package/dist/active-citizen/engine/notifications/point_delivery.cjs +0 -54
- package/dist/active-citizen/engine/notifications/post_delivery.cjs +0 -31
- package/dist/active-citizen/engine/notifications/process_delayed_notifications.cjs +0 -471
- package/dist/active-citizen/engine/notifications/process_general_notifications.cjs +0 -212
- package/dist/active-citizen/engine/old/exporters/categories_dataset.js +0 -153
- package/dist/active-citizen/engine/old/exporters/dataset_tools.js +0 -80
- package/dist/active-citizen/engine/old/exporters/sentiment_dataset.js +0 -157
- package/dist/active-citizen/engine/recommendations/events_importer.cjs +0 -139
- package/dist/active-citizen/engine/recommendations/events_manager.cjs +0 -212
- package/dist/active-citizen/engine/reports/add_points_to_sheet.cjs +0 -83
- package/dist/active-citizen/engine/reports/commonUtils.js +0 -75
- package/dist/active-citizen/engine/reports/common_utils.cjs +0 -740
- package/dist/active-citizen/engine/reports/docx_group_report.cjs +0 -596
- package/dist/active-citizen/engine/reports/xlsAllOurIdeasExport.js +0 -232
- package/dist/active-citizen/engine/reports/xls_community_users_report.cjs +0 -277
- package/dist/active-citizen/engine/reports/xls_group_report.cjs +0 -718
- package/dist/active-citizen/llms/baseChatBot.js +0 -183
- package/dist/active-citizen/llms/imageGeneration/chatGptImageGenerator.js +0 -56
- package/dist/active-citizen/llms/imageGeneration/collectionImageGenerator.js +0 -109
- package/dist/active-citizen/llms/imageGeneration/dalleImageGenerator.js +0 -84
- package/dist/active-citizen/llms/imageGeneration/fluxImageGenerator.js +0 -49
- package/dist/active-citizen/llms/imageGeneration/iImageGenerator.js +0 -1
- package/dist/active-citizen/llms/imageGeneration/imageProcessorService.js +0 -64
- package/dist/active-citizen/llms/imageGeneration/imagenImageGenerator.js +0 -107
- package/dist/active-citizen/llms/imageGeneration/s3Service.js +0 -110
- package/dist/active-citizen/llms/llmTranslation.js +0 -472
- package/dist/active-citizen/models/ac_activity.cjs +0 -216
- package/dist/active-citizen/models/ac_background_job.cjs +0 -109
- package/dist/active-citizen/models/ac_campaign.cjs +0 -97
- package/dist/active-citizen/models/ac_client_activity.cjs +0 -23
- package/dist/active-citizen/models/ac_delayed_notification.cjs +0 -43
- package/dist/active-citizen/models/ac_following.cjs +0 -43
- package/dist/active-citizen/models/ac_list.cjs +0 -68
- package/dist/active-citizen/models/ac_list_users.cjs +0 -19
- package/dist/active-citizen/models/ac_mute.cjs +0 -27
- package/dist/active-citizen/models/ac_news_feed_item.cjs +0 -57
- package/dist/active-citizen/models/ac_news_feed_processed_range.cjs +0 -59
- package/dist/active-citizen/models/ac_notification.cjs +0 -292
- package/dist/active-citizen/models/ac_translation_cache.cjs +0 -750
- package/dist/active-citizen/models/ac_watching.cjs +0 -31
- package/dist/active-citizen/scripts/analytics/setup_all_plausible_goals.cjs +0 -13
- package/dist/active-citizen/scripts/fix_old_delayed_notifications.js +0 -63
- package/dist/active-citizen/scripts/kue_status.js +0 -31
- package/dist/active-citizen/scripts/kue_watch_stuck_jobs.js +0 -24
- package/dist/active-citizen/scripts/translation_clear_language.js +0 -117
- package/dist/active-citizen/scripts/translation_delete.js +0 -27
- package/dist/active-citizen/scripts/translation_replace_text_from_url.js +0 -180
- package/dist/active-citizen/scripts/translation_update.js +0 -28
- package/dist/active-citizen/scripts/translations_list.js +0 -27
- package/dist/active-citizen/utils/airbrake.cjs +0 -17
- package/dist/active-citizen/utils/get_anonymous_system_user.cjs +0 -21
- package/dist/active-citizen/utils/i18n.cjs +0 -3
- package/dist/active-citizen/utils/logger.cjs +0 -25
- package/dist/active-citizen/utils/redisConnection.cjs +0 -29
- package/dist/active-citizen/utils/to_json.cjs +0 -9
- package/dist/active-citizen/utils/translation_cloning.cjs +0 -171
- package/dist/active-citizen/utils/translation_helpers.cjs +0 -534
- package/dist/active-citizen/utils/truncate_text.cjs +0 -21
- package/dist/active-citizen/utils/updateAllLocalesFromEn.js +0 -253
- package/dist/active-citizen/utils/updateLocaleFolders.js +0 -34
- package/dist/active-citizen/workers/activity.cjs +0 -189
- package/dist/active-citizen/workers/anonymizations.cjs +0 -734
- package/dist/active-citizen/workers/bulk_status_update.cjs +0 -458
- package/dist/active-citizen/workers/delayed_jobs.cjs +0 -244
- package/dist/active-citizen/workers/deletions.cjs +0 -1911
- package/dist/active-citizen/workers/email.cjs +0 -9
- package/dist/active-citizen/workers/fraud_management.cjs +0 -109
- package/dist/active-citizen/workers/generativeAi.js +0 -56
- package/dist/active-citizen/workers/main.cjs +0 -89
- package/dist/active-citizen/workers/marketing.cjs +0 -25
- package/dist/active-citizen/workers/moderation.cjs +0 -73
- package/dist/active-citizen/workers/notification_delivery.cjs +0 -368
- package/dist/active-citizen/workers/notification_news_feed.cjs +0 -142
- package/dist/active-citizen/workers/queue.cjs +0 -99
- package/dist/active-citizen/workers/recount.cjs +0 -74
- package/dist/active-citizen/workers/reports.cjs +0 -42
- package/dist/active-citizen/workers/similarities.cjs +0 -21
- package/dist/active-citizen/workers/speech_to_text.cjs +0 -482
- package/dist/agents/assistants/agentAssistant.js +0 -88
- package/dist/agents/assistants/baseAssistant.js +0 -888
- package/dist/agents/assistants/baseAssistantWithVoice.js +0 -150
- package/dist/agents/assistants/modes/agentDirectConnection.js +0 -84
- package/dist/agents/assistants/modes/agentSelectionMode.js +0 -44
- package/dist/agents/assistants/modes/baseAssistantMode.js +0 -54
- package/dist/agents/assistants/modes/tools/agentTools.js +0 -447
- package/dist/agents/assistants/modes/tools/baseTools.js +0 -58
- package/dist/agents/assistants/modes/tools/loginTools.js +0 -156
- package/dist/agents/assistants/modes/tools/models/agents.js +0 -146
- package/dist/agents/assistants/modes/tools/models/subscriptions.js +0 -332
- package/dist/agents/assistants/modes/tools/models/users.js +0 -11
- package/dist/agents/assistants/modes/tools/navigationTools.js +0 -166
- package/dist/agents/assistants/modes/tools/subscriptionTools.js +0 -323
- package/dist/agents/assistants/modes/tools/workflowConverstationTools.js +0 -112
- package/dist/agents/assistants/voiceAssistant.js +0 -619
- package/dist/agents/controllers/agentProductController.js +0 -103
- package/dist/agents/controllers/agentSubscriptionController.js +0 -261
- package/dist/agents/controllers/assistantsController.js +0 -511
- package/dist/agents/controllers/policySynthAgents.js +0 -395
- package/dist/agents/managers/agentProductManager.js +0 -91
- package/dist/agents/managers/emailInvitesManager.js +0 -55
- package/dist/agents/managers/emailTemplateRenderer.js +0 -362
- package/dist/agents/managers/newAiModelSetup.js +0 -650
- package/dist/agents/managers/notificationAgentQueueManager.js +0 -510
- package/dist/agents/managers/subscriptionManager.js +0 -535
- package/dist/agents/managers/workflowConversationManager.js +0 -79
- package/dist/agents/models/agentProduct.js +0 -116
- package/dist/agents/models/agentProductBoosterPurchase.js +0 -58
- package/dist/agents/models/agentProductBundle.js +0 -68
- package/dist/agents/models/agentProductRun.js +0 -52
- package/dist/agents/models/discount.js +0 -88
- package/dist/agents/models/subscription.js +0 -79
- package/dist/agents/models/subscriptionPlan.js +0 -46
- package/dist/agents/models/subscriptionUser.js +0 -27
- package/dist/agents/models/testData/createEvolyAgentProduct.js +0 -477
- package/dist/agents/models/testData/old/updateAgentWorkflowConfiguration.js +0 -230
- package/dist/agents/models/testData/setupEvolyAgentProductConfig.js +0 -233
- package/dist/agents/models/testData/updateAgentWorkflowConfiguration.js +0 -230
- package/dist/agents/models/workflowConversation.js +0 -53
- package/dist/agents/tools/setTemplateWorkflowCommunityId.js +0 -46
- package/dist/app.js +0 -943
- package/dist/authorization.cjs +0 -1860
- package/dist/bot_control.js +0 -1930
- package/dist/config/config.cjs +0 -14
- package/dist/config/config.js +0 -14
- package/dist/controllers/allOurIdeas.js +0 -696
- package/dist/controllers/audios.cjs +0 -100
- package/dist/controllers/bulkStatusUpdates.cjs +0 -202
- package/dist/controllers/categories.cjs +0 -199
- package/dist/controllers/communities.cjs +0 -2996
- package/dist/controllers/domains.cjs +0 -1341
- package/dist/controllers/externalIds.cjs +0 -223
- package/dist/controllers/groups.cjs +0 -4309
- package/dist/controllers/images.cjs +0 -499
- package/dist/controllers/index.cjs +0 -449
- package/dist/controllers/legacyPages.cjs +0 -35
- package/dist/controllers/legacyPosts.cjs +0 -56
- package/dist/controllers/legacyUsers.cjs +0 -36
- package/dist/controllers/nonSpa.cjs +0 -574
- package/dist/controllers/organizations.cjs +0 -250
- package/dist/controllers/points.cjs +0 -1137
- package/dist/controllers/posts.cjs +0 -2036
- package/dist/controllers/ratings.cjs +0 -234
- package/dist/controllers/users.cjs +0 -2255
- package/dist/controllers/videos.cjs +0 -226
- package/dist/deleteUnwantedDeclerations.cjs +0 -55
- package/dist/migrations/agentAuditLogs.cjs +0 -46
- package/dist/migrations/agentClasses.cjs +0 -60
- package/dist/migrations/agentConnectorClasses.cjs +0 -61
- package/dist/migrations/agentConnectors.cjs +0 -50
- package/dist/migrations/agentEvals.cjs +0 -45
- package/dist/migrations/agentRegistries.cjs +0 -40
- package/dist/migrations/agents.cjs +0 -54
- package/dist/migrations/aiModels.cjs +0 -49
- package/dist/migrations/apiUsage.cjs +0 -47
- package/dist/migrations/apis.cjs +0 -49
- package/dist/migrations/groupPrivateData.cjs +0 -30
- package/dist/migrations/modelUsage.cjs +0 -60
- package/dist/migrations/oldMigrations/2019/20181030020612-AddActivitiesIndex.js +0 -23
- package/dist/migrations/oldMigrations/2019/20181102210612-AddFirstVideoFeatures.js +0 -360
- package/dist/migrations/oldMigrations/2019/20181212210612-ModerationFeatures.js +0 -29
- package/dist/migrations/oldMigrations/2019/2019010610612-CommunityFolders.js +0 -43
- package/dist/migrations/oldMigrations/2019/20190117020612-AddMissingIndexes.js +0 -24
- package/dist/migrations/oldMigrations/2019/20190117020612-RemoveUnusedIndexes.js +0 -29
- package/dist/migrations/oldMigrations/2019/20190127020612-RemoveUnusedIndexesPartThree.js +0 -22
- package/dist/migrations/oldMigrations/2019/20190127020612-RemoveUnusedIndexesPartTwo.js +0 -23
- package/dist/migrations/oldMigrations/2019/20190223020612-AddPrivateProfileDataToUsers.js +0 -18
- package/dist/migrations/oldMigrations/2019/20190706210612-AddCustomRatings.js +0 -43
- package/dist/migrations/oldMigrations/2019/20190829210612-AddGeneralStore.js +0 -36
- package/dist/migrations/oldMigrations/2019/20192811210612-AddAcClientActivities.js +0 -41
- package/dist/migrations/oldMigrations/2020/20190527020612-WorkOnIndexes.js +0 -88
- package/dist/migrations/oldMigrations/2020/20200409020612-AddBackgroundJob.js +0 -33
- package/dist/migrations/oldMigrations/2020/20200716210612-AddDataToCollections.js +0 -38
- package/dist/migrations/oldMigrations/2022/20220215100612-AddDataToEndorsements.js +0 -19
- package/dist/migrations/oldMigrations/2022/20220220100612-AddDataForFraudDetection.js +0 -19
- package/dist/migrations/oldMigrations/2022/20220903100612-AddPromotionFeatures.js +0 -127
- package/dist/migrations/oldMigrations/2022/onHold/20200527020612-AddCampaigns.js +0 -68
- package/dist/migrations/oldMigrations/2024/20241304175112-AddMediaSupportForHtmlGroups.cjs +0 -63
- package/dist/migrations/oldMigrations/older/20160511172514-AddNotificationFeatures.js +0 -14
- package/dist/migrations/oldMigrations/older/20161030020612-AddBulkStatusUpdate.js +0 -71
- package/dist/migrations/oldMigrations/older/20170514035258-add-metadata-to-invites.js +0 -12
- package/dist/migrations/oldMigrations/older/20180216020612-AddTranslationCaches.js +0 -46
- package/dist/migrations/oldMigrations/older/20180218210612-AddTranslationAndLanguages.js +0 -46
- package/dist/migrations/privateAccessStore.cjs +0 -55
- package/dist/migrations/zzz_associations.cjs +0 -154
- package/dist/migrations/zzzz_createUsersAndAdminsForClasses.cjs +0 -100
- package/dist/migrations/zzzzz_create_agent_runs.cjs +0 -606
- package/dist/migrations/zzzzzz_create_agent_runs_fix.cjs +0 -11
- package/dist/migrations/zzzzzzz_create_trees.cjs +0 -81
- package/dist/models/audio.cjs +0 -430
- package/dist/models/bulk_status_update.cjs +0 -58
- package/dist/models/campaign.cjs +0 -78
- package/dist/models/category.cjs +0 -94
- package/dist/models/community.cjs +0 -337
- package/dist/models/domain.cjs +0 -486
- package/dist/models/endorsement.cjs +0 -39
- package/dist/models/general_data_store.cjs +0 -20
- package/dist/models/group.cjs +0 -728
- package/dist/models/image.cjs +0 -579
- package/dist/models/index.cjs +0 -186
- package/dist/models/invite.cjs +0 -48
- package/dist/models/iso_country.cjs +0 -16
- package/dist/models/organization.cjs +0 -122
- package/dist/models/page.cjs +0 -273
- package/dist/models/point.cjs +0 -622
- package/dist/models/point_quality.cjs +0 -39
- package/dist/models/point_revision.cjs +0 -47
- package/dist/models/post.cjs +0 -680
- package/dist/models/post_revision.cjs +0 -38
- package/dist/models/post_status_change.cjs +0 -35
- package/dist/models/promotion.cjs +0 -34
- package/dist/models/rating.cjs +0 -51
- package/dist/models/relationship.cjs +0 -19
- package/dist/models/request_to_join.cjs +0 -20
- package/dist/models/user.cjs +0 -604
- package/dist/models/user_legacy_password.cjs +0 -13
- package/dist/models/video.cjs +0 -1137
- package/dist/publish.js +0 -40
- package/dist/repack.js +0 -53
- package/dist/scripts/addRatingUsersToGroup.js +0 -51
- package/dist/scripts/addUserToOrganization.js +0 -71
- package/dist/scripts/analyseRatingsForCommunity.js +0 -150
- package/dist/scripts/analyzeAndFixBrokenPointUsers.js +0 -28
- package/dist/scripts/analyzeEndorsementsByCountry.js +0 -70
- package/dist/scripts/analyzePostsForCommunity.js +0 -185
- package/dist/scripts/bulkStatusUpdates/listUpdates.js +0 -14
- package/dist/scripts/bulkStatusUpdates/mergeLatestPostsToUpdate.js +0 -110
- package/dist/scripts/bulkStatusUpdates/performUpdate.js +0 -116
- package/dist/scripts/bulkStatusUpdates/performUpdateForGroup.cjs +0 -124
- package/dist/scripts/bulkStatusUpdates/performUpdateForStatus.js +0 -141
- package/dist/scripts/change/changeVideoAspectTo.js +0 -34
- package/dist/scripts/change/setUseNewVersion.cjs +0 -22
- package/dist/scripts/changeCommunityGroupcount.js +0 -30
- package/dist/scripts/changeCommunityPostCount.js +0 -30
- package/dist/scripts/changeGroupPostCount.js +0 -30
- package/dist/scripts/changeLanguage.js +0 -50
- package/dist/scripts/changeOfficalStatus.js +0 -30
- package/dist/scripts/cleanups/deleteAnonNotifications.cjs +0 -91
- package/dist/scripts/cleanups/deleteLinkGroupsWithDeletedCommunities.js +0 -62
- package/dist/scripts/cleanups/deleteYearOldNotifications.cjs +0 -72
- package/dist/scripts/cleanups/removeAllUsersFromHiddenPublicGroup.js +0 -43
- package/dist/scripts/clearAllEndorsementInGroup.js +0 -50
- package/dist/scripts/cloning/clearUsersForCommunitiesFromUrl.js +0 -129
- package/dist/scripts/cloning/cloneFromUrlScript.js +0 -65
- package/dist/scripts/cloning/cloneWBFromUrlScriptAndCreateLinks.js +0 -140
- package/dist/scripts/cloning/cloneWBFromUrlScriptNoUsersOrPoints.js +0 -140
- package/dist/scripts/cloning/cloneWBSerbianFromUrlScriptAndCreateLinks.js +0 -131
- package/dist/scripts/cloning/copyCommunityConfigAndTranslationsFromURL.js +0 -173
- package/dist/scripts/cloning/copyCommunityOneGroupToDomainNoUsersNoEndorsements.js +0 -18
- package/dist/scripts/cloning/copyCommunityToDomainNoUsersNoEndorsements.js +0 -17
- package/dist/scripts/cloning/copyCommunityToDomainWithEverything.js +0 -17
- package/dist/scripts/cloning/copyCommunityToDomainWithOnlyGroups.js +0 -26
- package/dist/scripts/cloning/copyGroupConfigAndTranslationsFromURL.js +0 -205
- package/dist/scripts/cloning/copyPostToGroupOld.js +0 -397
- package/dist/scripts/cloning/copyPostVideosFromURL.js +0 -236
- package/dist/scripts/cloning/copyPostWithOutAnyVotingOrActivities.js +0 -17
- package/dist/scripts/cloning/deepCloneSerbianWBFromUrlScriptAndCreateLinks.js +0 -131
- package/dist/scripts/cloning/deepCloneWBFromUrlScriptAndCreateLinks.js +0 -139
- package/dist/scripts/cloning/setAdminsFromURL.js +0 -161
- package/dist/scripts/cloning/setExternalIdsFromURL.js +0 -129
- package/dist/scripts/countCommunity.js +0 -291
- package/dist/scripts/countCommunityUsers.js +0 -152
- package/dist/scripts/countDelayedNotifications.js +0 -18
- package/dist/scripts/countGroup.js +0 -246
- package/dist/scripts/countStuff.js +0 -67
- package/dist/scripts/countUniqueVotersInAGroup.js +0 -48
- package/dist/scripts/createInvitesAndShow.js +0 -75
- package/dist/scripts/database/sync_database.js +0 -14
- package/dist/scripts/database/sync_dev_database.js +0 -17
- package/dist/scripts/debugNotifications.js +0 -58
- package/dist/scripts/deleteAllNewsFeeds.js +0 -10
- package/dist/scripts/deleteCategory.js +0 -13
- package/dist/scripts/deleteOldAppActivities.js +0 -40
- package/dist/scripts/deletePostContactDataForCommunity.js +0 -53
- package/dist/scripts/destroy/destroy_all_but_one_domain.js +0 -1026
- package/dist/scripts/displayAuthorForPost.js +0 -16
- package/dist/scripts/endorsementFraudDetection/analyseEndorsementsForCommunity.js +0 -183
- package/dist/scripts/endorsementFraudDetection/bulkDeleteDuplicateEndorsmentsFromUrl.js +0 -208
- package/dist/scripts/exportAllStatusChanges.js +0 -36
- package/dist/scripts/exportClientAcitivity.js +0 -36
- package/dist/scripts/exportEndorsementsForCommunity.js +0 -79
- package/dist/scripts/exportPointQualitiesForCommunity.js +0 -84
- package/dist/scripts/exportPostsAndPointsForCommunity.js +0 -147
- package/dist/scripts/exportPostsDataSetForDomain.js +0 -244
- package/dist/scripts/exportPostsForGroup.js +0 -173
- package/dist/scripts/exportRatingsForPost.js +0 -15
- package/dist/scripts/exportUserEndorsementsWithUserAnalysis.js +0 -123
- package/dist/scripts/exportUsersForCommunity.js +0 -24
- package/dist/scripts/exportUsersForDomain.js +0 -24
- package/dist/scripts/exportUsersForGroup.js +0 -24
- package/dist/scripts/exports/ratingDistribution.js +0 -71
- package/dist/scripts/exports/whoEndorsedWhatByCommunity.js +0 -56
- package/dist/scripts/findUnusedClientImports.js +0 -56
- package/dist/scripts/fixAllPostPointCounts.js +0 -22
- package/dist/scripts/fixAnonNotificationsSettings.js +0 -48
- package/dist/scripts/fixCountKopavogur.js +0 -9
- package/dist/scripts/fixEndorsementsAfterCopyPostToGroup.js +0 -190
- package/dist/scripts/fixExternalUserId.js +0 -24
- package/dist/scripts/fixGroupAccess.js +0 -16
- package/dist/scripts/fixGroupIdeasAndPointsCount.js +0 -49
- package/dist/scripts/fixNotificationSettings.js +0 -39
- package/dist/scripts/fixSurveyRadioBakedInSubCodes.js +0 -64
- package/dist/scripts/fixWrongUserIdForStatusUpdates.js +0 -49
- package/dist/scripts/gallery/exportGalleryData.js +0 -40
- package/dist/scripts/gallery/importGalleryForCommunity.js +0 -168
- package/dist/scripts/gallery/readJsonAndDownloadImagesVersion2.js +0 -55
- package/dist/scripts/gallery/refreshAcApiPostIdsForCommunity.js +0 -58
- package/dist/scripts/genderAnalysis.js +0 -63
- package/dist/scripts/genderAnalysisByStatus.js +0 -62
- package/dist/scripts/importAllLocalesFromLocalFolders.js +0 -55
- package/dist/scripts/importDomain.js +0 -1652
- package/dist/scripts/keys/addOidcToDomain.cjs +0 -61
- package/dist/scripts/landUseGame/export3Ddata.js +0 -162
- package/dist/scripts/listLanguagesForGroup.js +0 -54
- package/dist/scripts/loadTestCreateDummyContentForGroup.js +0 -27
- package/dist/scripts/makeRecursiveMapData.js +0 -103
- package/dist/scripts/mapping/community_map_csv.js +0 -145
- package/dist/scripts/moveCommunityToDomain.js +0 -22
- package/dist/scripts/moveGroupToCommunity.js +0 -23
- package/dist/scripts/movePostToGroup.js +0 -101
- package/dist/scripts/movePostsToGroupsRecountGroupFromUrl.js +0 -297
- package/dist/scripts/oldMovePostToGroup.js +0 -153
- package/dist/scripts/processCsvForPdfUrls.js +0 -37
- package/dist/scripts/processCsvForTranslationAndToxicity.js +0 -125
- package/dist/scripts/recount/recount_recursive_communities.js +0 -125
- package/dist/scripts/recountALLCommunityGroupCounts.js +0 -37
- package/dist/scripts/recountAll.js +0 -97
- package/dist/scripts/recountCommunitesFromUrl.js +0 -58
- package/dist/scripts/recountCommunity.js +0 -19
- package/dist/scripts/recountGroup.js +0 -218
- package/dist/scripts/recountGroupNoUserChange.js +0 -219
- package/dist/scripts/resetAllEndorsementsForGroup.js +0 -57
- package/dist/scripts/resetEnTranslationForGroup.js +0 -45
- package/dist/scripts/setAdminOnAll.cjs +0 -107
- package/dist/scripts/setDomainAdmin.cjs +0 -43
- package/dist/scripts/setDomainLocales.js +0 -33
- package/dist/scripts/setEarlQuestionIdOnGroup.cjs +0 -29
- package/dist/scripts/setLanguageOnGroupCommunitesFromUrl.js +0 -86
- package/dist/scripts/setMemberOfAll.js +0 -101
- package/dist/scripts/setNewUserForContentOfCommunity.js +0 -189
- package/dist/scripts/setOfficialStatusOnAllPostsForCommunity.js +0 -35
- package/dist/scripts/setUserOnAll.js +0 -101
- package/dist/scripts/showCategoryForGroup.js +0 -18
- package/dist/scripts/showOldActivityTypes.js +0 -14
- package/dist/scripts/showPostsMissingCategoryForGroup.js +0 -17
- package/dist/scripts/showStatuses.js +0 -17
- package/dist/scripts/showUniqueVotersInCommunity.js +0 -61
- package/dist/scripts/showUserAgentsAndIpsForEmails.js +0 -66
- package/dist/scripts/simpleExportForGroupsForCommunity.js +0 -46
- package/dist/scripts/simpleExportForPointsForCommunity.js +0 -82
- package/dist/scripts/simpleExportForPostsForCommunity.js +0 -61
- package/dist/scripts/testForEndorsments.js +0 -21
- package/dist/scripts/undeleteGroupAndAllContent.js +0 -151
- package/dist/scripts/undeletePost.js +0 -135
- package/dist/scripts/unlinkSsn.js +0 -23
- package/dist/scripts/updateFromAlthingi.js +0 -303
- package/dist/server.js +0 -3
- package/dist/utils/airbrake.cjs +0 -17
- package/dist/utils/cjsCodeReview.js +0 -99
- package/dist/utils/community_mapping_tools.cjs +0 -124
- package/dist/utils/copy_utils.cjs +0 -1399
- package/dist/utils/docx_utils.cjs +0 -464
- package/dist/utils/export_utils.cjs +0 -491
- package/dist/utils/i18n.cjs +0 -17
- package/dist/utils/is_valid_db_id.cjs +0 -28
- package/dist/utils/logger.cjs +0 -25
- package/dist/utils/loggerTs.js +0 -26
- package/dist/utils/manifest_generator.cjs +0 -104
- package/dist/utils/parse_domain.cjs +0 -16
- package/dist/utils/recount_utils.cjs +0 -415
- package/dist/utils/sharing_parameters.cjs +0 -111
- package/dist/utils/sitemap_generator.cjs +0 -286
- package/dist/utils/to_json.cjs +0 -14
- package/dist/utils/ypLanguages.js +0 -747
- package/dist/webSockets.js +0 -77
|
@@ -1,888 +0,0 @@
|
|
|
1
|
-
import { OpenAI } from "openai";
|
|
2
|
-
import { EventEmitter } from "events";
|
|
3
|
-
import { YpBaseChatBot } from "../../active-citizen/llms/baseChatBot.js";
|
|
4
|
-
import { YpAgentProduct } from "../models/agentProduct.js";
|
|
5
|
-
import { YpSubscription } from "../models/subscription.js";
|
|
6
|
-
import { YpSubscriptionPlan } from "../models/subscriptionPlan.js";
|
|
7
|
-
import { YpAgentProductRun } from "../models/agentProductRun.js";
|
|
8
|
-
/**
|
|
9
|
-
* Common modes that implementations might use
|
|
10
|
-
*/
|
|
11
|
-
export var CommonModes;
|
|
12
|
-
(function (CommonModes) {
|
|
13
|
-
CommonModes["ErrorRecovery"] = "error_recovery";
|
|
14
|
-
})(CommonModes || (CommonModes = {}));
|
|
15
|
-
export class YpBaseAssistant extends YpBaseChatBot {
|
|
16
|
-
constructor(wsClientId, wsClients, redis, redisKey, domainId) {
|
|
17
|
-
super(wsClientId, wsClients, redis, redisKey);
|
|
18
|
-
this.persistMemory = true;
|
|
19
|
-
this.DEBUG = false;
|
|
20
|
-
this.modes = new Map();
|
|
21
|
-
this.availableTools = new Map();
|
|
22
|
-
this.toolCallTimeout = 30000; // 30 seconds
|
|
23
|
-
this.maxModeTransitions = 10;
|
|
24
|
-
this.modelName = process.env.OPENAI_STREAMING_MODEL_NAME || "gpt-4o-2024-11-20";
|
|
25
|
-
this.defaultSystemPrompt = `<coreImportantSystemInstructions>
|
|
26
|
-
You are a helpful, witty, and friendly AI. Act like a human, but remember that you aren't a human and that you can't do human things in the real world.
|
|
27
|
-
Your voice and personality should be warm and engaging, with a lively and playful tone. Talk quickly.
|
|
28
|
-
If interacting in a non-English language, start by using the standard accent or dialect familiar to the user.
|
|
29
|
-
You should always call a function/tool if you can to help the user with their request.
|
|
30
|
-
Never try to start workflows without using functions/tools, using some tools will unlock other function/tools that might help the user with their request.
|
|
31
|
-
Functions/tools will become available to you as the user progresses through the workflow.
|
|
32
|
-
Make sure that the user logged in before connecting him to an agent.
|
|
33
|
-
Never list out the available agents using text or markdown, the user already sees the list of agents available for subscription in the UI.
|
|
34
|
-
You will not be able to start the workflow for the user except using tools/functions at each step.
|
|
35
|
-
Never engage in longish back and fourth conversations with the user if a workflow has not started, lead the user towards using the tools available as much as possible, politely.
|
|
36
|
-
Never engage in off topic conversations, always politely steer the conversation back to the workflow.
|
|
37
|
-
</coreImportantSystemInstructions>
|
|
38
|
-
|
|
39
|
-
<typicalWorkflowStepsForStartingAnAgentWorkflow>
|
|
40
|
-
1) The agent presents the user with a list of agents available for connection.
|
|
41
|
-
2) The user logs in or creates an account, if the user is not logged in already.
|
|
42
|
-
3) The user chooses an agent to connect to using the direct connect to the agent the user chooses.
|
|
43
|
-
4) The agent offers to show the user a workflow overview, with a tool.
|
|
44
|
-
5) The user fills out the configuration UI widget.
|
|
45
|
-
6) The user submits from the configuration UI widget.
|
|
46
|
-
7) The user starts the workflow run.
|
|
47
|
-
</typicalWorkflowStepsForStartingAnAgentWorkflow>
|
|
48
|
-
`;
|
|
49
|
-
this.voiceEnabled = false;
|
|
50
|
-
this.domainId = domainId;
|
|
51
|
-
if (!domainId) {
|
|
52
|
-
throw new Error("Domain ID is required");
|
|
53
|
-
}
|
|
54
|
-
this.redis = redis;
|
|
55
|
-
this.openaiClient = new OpenAI({
|
|
56
|
-
apiKey: process.env.OPENAI_API_KEY,
|
|
57
|
-
});
|
|
58
|
-
this.eventEmitter = new EventEmitter();
|
|
59
|
-
this.clientSystemMessageListener = this.handleClientSystemMessage.bind(this);
|
|
60
|
-
this.setupClientSystemMessageListener();
|
|
61
|
-
this.on("update-ai-model-session", this.updateAiModelSession.bind(this));
|
|
62
|
-
}
|
|
63
|
-
destroy() {
|
|
64
|
-
// remove the WebSocket “message” listener
|
|
65
|
-
this.removeClientSystemMessageListener();
|
|
66
|
-
// remove all other event listeners on the assistant’s own EventEmitter
|
|
67
|
-
this.eventEmitter.removeAllListeners();
|
|
68
|
-
// clear references
|
|
69
|
-
this.wsClientSocket = undefined;
|
|
70
|
-
}
|
|
71
|
-
removeClientSystemMessageListener() {
|
|
72
|
-
this.wsClientSocket.removeListener("message", this.clientSystemMessageListener);
|
|
73
|
-
}
|
|
74
|
-
handleClientSystemMessage(data) {
|
|
75
|
-
try {
|
|
76
|
-
const message = JSON.parse(data.toString());
|
|
77
|
-
switch (message.type) {
|
|
78
|
-
case "client_system_message":
|
|
79
|
-
console.log("WebSockets: Processing client_system_message:", message);
|
|
80
|
-
this.processClientSystemMessage(message);
|
|
81
|
-
break;
|
|
82
|
-
default:
|
|
83
|
-
//console.log('Unhandled message type:', message.type);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
catch (error) {
|
|
87
|
-
console.error("Error processing message:", error);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
setupClientSystemMessageListener() {
|
|
91
|
-
console.log("WebSockets: setupClientSystemMessageListener called for wsClientId:", this.wsClientId, this.clientSystemMessageListener);
|
|
92
|
-
this.wsClientSocket.on("message", this.clientSystemMessageListener);
|
|
93
|
-
const listenerCountAfter = this.wsClientSocket.listenerCount("message");
|
|
94
|
-
console.log('Number of "message" listeners after adding:', listenerCountAfter);
|
|
95
|
-
}
|
|
96
|
-
async getCurrentAgentProduct() {
|
|
97
|
-
if (this.memory.currentAgentStatus?.subscriptionPlanId) {
|
|
98
|
-
const subscriptionPlan = await YpSubscriptionPlan.findOne({
|
|
99
|
-
where: {
|
|
100
|
-
id: this.memory.currentAgentStatus.subscriptionPlanId,
|
|
101
|
-
},
|
|
102
|
-
include: [
|
|
103
|
-
{
|
|
104
|
-
model: YpAgentProduct,
|
|
105
|
-
as: "AgentProduct",
|
|
106
|
-
},
|
|
107
|
-
],
|
|
108
|
-
});
|
|
109
|
-
return subscriptionPlan?.AgentProduct;
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
console.error("--------------------> No subscription plan found");
|
|
113
|
-
}
|
|
114
|
-
return undefined;
|
|
115
|
-
}
|
|
116
|
-
async getCurrentSubscriptionPlan() {
|
|
117
|
-
if (this.memory.currentAgentStatus?.subscriptionPlanId) {
|
|
118
|
-
return (await YpSubscriptionPlan.findOne({
|
|
119
|
-
where: { id: this.memory.currentAgentStatus.subscriptionPlanId },
|
|
120
|
-
include: [
|
|
121
|
-
{
|
|
122
|
-
model: YpAgentProduct,
|
|
123
|
-
as: "AgentProduct",
|
|
124
|
-
},
|
|
125
|
-
],
|
|
126
|
-
}));
|
|
127
|
-
}
|
|
128
|
-
return undefined;
|
|
129
|
-
}
|
|
130
|
-
async getCurrentSubscription() {
|
|
131
|
-
if (this.memory.currentAgentStatus?.subscriptionId) {
|
|
132
|
-
return (await YpSubscription.findOne({
|
|
133
|
-
where: { id: this.memory.currentAgentStatus.subscriptionId },
|
|
134
|
-
}));
|
|
135
|
-
}
|
|
136
|
-
return undefined;
|
|
137
|
-
}
|
|
138
|
-
async getCurrentAgentRun() {
|
|
139
|
-
if (this.memory.currentAgentStatus?.activeAgentRunId) {
|
|
140
|
-
return (await YpAgentProductRun.findOne({
|
|
141
|
-
where: { id: this.memory.currentAgentStatus.activeAgentRunId },
|
|
142
|
-
}));
|
|
143
|
-
}
|
|
144
|
-
else {
|
|
145
|
-
console.error("No active agent run found");
|
|
146
|
-
}
|
|
147
|
-
return undefined;
|
|
148
|
-
}
|
|
149
|
-
async updateAiModelSession(message) {
|
|
150
|
-
console.log(`updateAiModelSession: ${message}`);
|
|
151
|
-
}
|
|
152
|
-
async maybeSendTextResponse(message) {
|
|
153
|
-
if (!this.voiceEnabled) {
|
|
154
|
-
this.sendToClient("assistant", message, "message");
|
|
155
|
-
await this.addAssistantMessage(message);
|
|
156
|
-
console.debug(`Sent text message to client: ${message}`);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
async processClientSystemMessage(clientEvent) {
|
|
160
|
-
console.log(`processClientSystemMessage: ${JSON.stringify(clientEvent, null, 2)}`);
|
|
161
|
-
await this.loadMemoryAsync();
|
|
162
|
-
if (clientEvent.message === "user_logged_in") {
|
|
163
|
-
console.log(`user_logged_in emitting`);
|
|
164
|
-
this.emit("update-ai-model-session", "User is logged in, lets move to the next step");
|
|
165
|
-
await this.maybeSendTextResponse("Logged in, ready to move on to the next step.");
|
|
166
|
-
}
|
|
167
|
-
else if (clientEvent.message === "agent_configuration_submitted") {
|
|
168
|
-
console.log(`agent_configuration_submitted emitting`);
|
|
169
|
-
try {
|
|
170
|
-
if (!this.memory.currentAgentStatus.subscriptionId) {
|
|
171
|
-
throw new Error("No subscription found");
|
|
172
|
-
}
|
|
173
|
-
const subscription = await YpSubscription.findOne({
|
|
174
|
-
where: {
|
|
175
|
-
id: this.memory.currentAgentStatus.subscriptionId,
|
|
176
|
-
status: "active",
|
|
177
|
-
},
|
|
178
|
-
});
|
|
179
|
-
if (!subscription) {
|
|
180
|
-
throw new Error("No subscription found");
|
|
181
|
-
}
|
|
182
|
-
await this.updateCurrentAgentProductPlan(this.memory.currentAgentStatus.subscriptionPlanId, subscription.id);
|
|
183
|
-
const html = `<yp-configuration-submitted></yp-configuration-submitted>`;
|
|
184
|
-
this.wsClientSocket.send(JSON.stringify({
|
|
185
|
-
sender: "assistant",
|
|
186
|
-
type: "html",
|
|
187
|
-
html,
|
|
188
|
-
}));
|
|
189
|
-
this.emit("update-ai-model-session", "The agent configuration was submitted successfully and the agent is ready to create its first agent run");
|
|
190
|
-
await this.maybeSendTextResponse("The agent configuration was submitted successfully and the agent is ready to create its first agent run.");
|
|
191
|
-
}
|
|
192
|
-
catch (error) {
|
|
193
|
-
console.error(`Error finding subscription: ${error}`);
|
|
194
|
-
this.emit("update-ai-model-session", `Failed to submit agent configuration: ${error}`);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
else if (clientEvent.message === "agent_run_changed") {
|
|
198
|
-
try {
|
|
199
|
-
console.log(`agent_run_changed`);
|
|
200
|
-
if (!this.memory.currentAgentStatus?.activeAgentRunId) {
|
|
201
|
-
throw new Error("No active agent run found");
|
|
202
|
-
}
|
|
203
|
-
const agentRun = await YpAgentProductRun.findOne({
|
|
204
|
-
where: {
|
|
205
|
-
id: this.memory.currentAgentStatus.activeAgentRunId,
|
|
206
|
-
},
|
|
207
|
-
});
|
|
208
|
-
if (!agentRun) {
|
|
209
|
-
throw new Error("No agent run found");
|
|
210
|
-
}
|
|
211
|
-
console.log(`agent_run_changed emitting`);
|
|
212
|
-
const currentWorkflowStep = agentRun.workflow?.steps[agentRun.workflow?.currentStepIndex];
|
|
213
|
-
this.emit("update-ai-model-session", `The agent run status has been updated to ${agentRun.status} ${JSON.stringify({ workflow: agentRun.workflow, currentWorkflowStep }, null, 2)} offer the user assistance with this next step in the workflow`);
|
|
214
|
-
}
|
|
215
|
-
catch (error) {
|
|
216
|
-
console.error(`Error finding agent run: ${error}`);
|
|
217
|
-
this.emit("update-ai-model-session", `Failed to update agent run status: ${error}`);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
emit(event, ...args) {
|
|
222
|
-
return this.eventEmitter.emit(event, ...args);
|
|
223
|
-
}
|
|
224
|
-
on(event, listener) {
|
|
225
|
-
this.eventEmitter.on(event, listener);
|
|
226
|
-
}
|
|
227
|
-
async updateCurrentAgentProductPlan(subscriptionPlanId, subscriptionId) {
|
|
228
|
-
const subscriptionPlan = await YpSubscriptionPlan.findOne({
|
|
229
|
-
where: {
|
|
230
|
-
id: subscriptionPlanId,
|
|
231
|
-
},
|
|
232
|
-
});
|
|
233
|
-
if (!subscriptionPlan) {
|
|
234
|
-
throw new Error("No subscription plan found");
|
|
235
|
-
}
|
|
236
|
-
const subscription = subscriptionId
|
|
237
|
-
? await YpSubscription.findOne({
|
|
238
|
-
where: {
|
|
239
|
-
id: subscriptionId,
|
|
240
|
-
},
|
|
241
|
-
})
|
|
242
|
-
: null;
|
|
243
|
-
const requiredStructuredQuestions = subscriptionPlan.configuration.requiredStructuredQuestions;
|
|
244
|
-
const requiredStructuredAnswers = subscription?.configuration?.requiredQuestionsAnswered;
|
|
245
|
-
await this.loadMemoryAsync();
|
|
246
|
-
this.memory.currentAgentStatus = {
|
|
247
|
-
activeAgentRunId: this.memory.currentAgentStatus?.activeAgentRunId,
|
|
248
|
-
subscriptionPlanId,
|
|
249
|
-
subscriptionId,
|
|
250
|
-
subscriptionState: subscription ? "subscribed" : "unsubscribed",
|
|
251
|
-
configurationState: requiredStructuredAnswers &&
|
|
252
|
-
requiredStructuredQuestions &&
|
|
253
|
-
requiredStructuredAnswers.length == requiredStructuredQuestions.length
|
|
254
|
-
? "configured"
|
|
255
|
-
: "not_configured",
|
|
256
|
-
};
|
|
257
|
-
console.log(`updateCurrentAgentProductPlan: ${JSON.stringify(this.memory.currentAgentStatus, null, 2)}`);
|
|
258
|
-
await this.saveMemory();
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Convert tool result to message format
|
|
262
|
-
*/
|
|
263
|
-
convertToolResultToMessage(toolCall, result) {
|
|
264
|
-
if (this.DEBUG) {
|
|
265
|
-
console.log(`convertToolResultToMessage: ${JSON.stringify(toolCall, null, 2)}`);
|
|
266
|
-
}
|
|
267
|
-
return {
|
|
268
|
-
role: "tool",
|
|
269
|
-
content: JSON.stringify(result.data) || result.html || "",
|
|
270
|
-
tool_call_id: toolCall.id,
|
|
271
|
-
name: toolCall.name,
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* Handle executing tool calls with results
|
|
276
|
-
*/
|
|
277
|
-
async handleToolCalls(toolCalls) {
|
|
278
|
-
if (this.DEBUG) {
|
|
279
|
-
console.log(`====================================> handleToolCalls: ${JSON.stringify(Array.from(toolCalls.values()), null, 2)}`);
|
|
280
|
-
}
|
|
281
|
-
const toolResponses = [];
|
|
282
|
-
for (const toolCall of toolCalls.values()) {
|
|
283
|
-
try {
|
|
284
|
-
const func = this.availableTools.get(toolCall.name);
|
|
285
|
-
if (!func) {
|
|
286
|
-
throw new Error(`Unknown function: ${toolCall.name}`);
|
|
287
|
-
}
|
|
288
|
-
let parsedArgs;
|
|
289
|
-
try {
|
|
290
|
-
parsedArgs = JSON.parse(toolCall.arguments);
|
|
291
|
-
}
|
|
292
|
-
catch (e) {
|
|
293
|
-
throw new Error(`Invalid function arguments: ${toolCall.arguments}`);
|
|
294
|
-
}
|
|
295
|
-
// Execute the function and get result
|
|
296
|
-
const result = await func.handler(parsedArgs);
|
|
297
|
-
if (this.DEBUG) {
|
|
298
|
-
console.log(`----------------------------------> Tool execution result:`, JSON.stringify(result, null, 2));
|
|
299
|
-
}
|
|
300
|
-
// Store the result in memory for context
|
|
301
|
-
if (result.success && result.data) {
|
|
302
|
-
await this.setModeData(`${toolCall.name}_result`, result.data);
|
|
303
|
-
}
|
|
304
|
-
// Convert result to message
|
|
305
|
-
const responseMessage = this.convertToolResultToMessage(toolCall, result);
|
|
306
|
-
toolResponses.push(responseMessage);
|
|
307
|
-
let resultData = result.data || result.error;
|
|
308
|
-
// Generate a user-friendly message based on the tool result
|
|
309
|
-
const resultMessage = `<contextFromRetrievedData>${JSON.stringify(resultData, null, 2)}</contextFromRetrievedData>`;
|
|
310
|
-
if (resultData) {
|
|
311
|
-
this.sendToClient("assistant", resultMessage, "hiddenContextMessage", result.uniqueToken, true);
|
|
312
|
-
this.memory.chatLog.push({
|
|
313
|
-
sender: "system",
|
|
314
|
-
hiddenContextMessage: true,
|
|
315
|
-
message: resultMessage,
|
|
316
|
-
type: "hiddenContextMessage",
|
|
317
|
-
uniqueToken: result.uniqueToken,
|
|
318
|
-
});
|
|
319
|
-
await this.saveMemoryIfNeeded();
|
|
320
|
-
}
|
|
321
|
-
else {
|
|
322
|
-
console.error(`No data returned from tool execution: ${toolCall.name}`);
|
|
323
|
-
}
|
|
324
|
-
if (result.html) {
|
|
325
|
-
this.sendToClient("assistant", result.html, "html", result.uniqueToken, true);
|
|
326
|
-
this.addAssistantHtmlMessage(result.html, result.uniqueToken);
|
|
327
|
-
}
|
|
328
|
-
if (result.clientEvents) {
|
|
329
|
-
if (this.DEBUG) {
|
|
330
|
-
console.log(`clientEvents: ${JSON.stringify(result.clientEvents, null, 2)}`);
|
|
331
|
-
}
|
|
332
|
-
for (const clientEvent of result.clientEvents) {
|
|
333
|
-
this.sendToClient("assistant", clientEvent.details, clientEvent.name);
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
// If error, throw it after recording the result
|
|
337
|
-
if (!result.success) {
|
|
338
|
-
console.error(`Unknown error in tool execution: ${result.error}`);
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
catch (error) {
|
|
342
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
343
|
-
console.error(`Error executing tool ${toolCall.name}: ${errorMessage}`);
|
|
344
|
-
throw error;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
// Create a new completion with tool results
|
|
348
|
-
if (toolResponses.length > 0) {
|
|
349
|
-
await this.handleToolResponses(toolResponses);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* Handle tool responses by creating a new completion
|
|
354
|
-
*/
|
|
355
|
-
async handleToolResponses(toolResponses) {
|
|
356
|
-
if (this.DEBUG) {
|
|
357
|
-
console.log(`handleToolResponses: ${JSON.stringify(toolResponses, null, 2)}`);
|
|
358
|
-
}
|
|
359
|
-
// Get existing chat messages
|
|
360
|
-
const messages = [
|
|
361
|
-
{
|
|
362
|
-
role: "system",
|
|
363
|
-
content: this.getCurrentSystemPrompt() +
|
|
364
|
-
`\n IMPORTANT: Do not output function content in text form at all, that is already provided by the function to the user, like the list of agents available for subscription, this information is already being displayed by the function to the user so no need to repeat it in text, just make a general comment`,
|
|
365
|
-
},
|
|
366
|
-
...this.convertToOpenAIMessages(this.memory.chatLog || []),
|
|
367
|
-
{
|
|
368
|
-
role: "assistant",
|
|
369
|
-
content: null,
|
|
370
|
-
tool_calls: Array.from(toolResponses.values()).map((response) => ({
|
|
371
|
-
id: response.tool_call_id,
|
|
372
|
-
type: "function",
|
|
373
|
-
function: {
|
|
374
|
-
name: response.name,
|
|
375
|
-
arguments: "{}", // The actual arguments don't matter here
|
|
376
|
-
},
|
|
377
|
-
})),
|
|
378
|
-
},
|
|
379
|
-
...toolResponses,
|
|
380
|
-
];
|
|
381
|
-
console.log(`handleToolResponses: ${JSON.stringify(messages, null, 2)}`);
|
|
382
|
-
try {
|
|
383
|
-
const stream = await this.openaiClient.chat.completions.create({
|
|
384
|
-
model: this.modelName,
|
|
385
|
-
messages,
|
|
386
|
-
tools: this.getCurrentModeFunctions().map((f) => ({
|
|
387
|
-
type: "function",
|
|
388
|
-
function: {
|
|
389
|
-
name: f.name,
|
|
390
|
-
description: f.description,
|
|
391
|
-
parameters: f.parameters,
|
|
392
|
-
},
|
|
393
|
-
})),
|
|
394
|
-
tool_choice: "auto",
|
|
395
|
-
stream: true,
|
|
396
|
-
});
|
|
397
|
-
await this.streamWebSocketResponses(stream);
|
|
398
|
-
}
|
|
399
|
-
catch (error) {
|
|
400
|
-
console.error("Error handling tool responses:", error);
|
|
401
|
-
throw error;
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
async setupMemoryAsync() {
|
|
405
|
-
if (!this.memory) {
|
|
406
|
-
console.log("loadMemoryWithOwnership: loading memory");
|
|
407
|
-
this.memory = (await this.loadMemory());
|
|
408
|
-
}
|
|
409
|
-
if (!this.memory) {
|
|
410
|
-
console.error("loadMemoryWithOwnership: No memory found!!!");
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
async loadMemoryAsync() {
|
|
414
|
-
this.memory = (await this.loadMemory());
|
|
415
|
-
}
|
|
416
|
-
getCleanedParams(params) {
|
|
417
|
-
return typeof params === "string" ? JSON.parse(params) : params;
|
|
418
|
-
}
|
|
419
|
-
async setCurrentMode(mode) {
|
|
420
|
-
if (mode) {
|
|
421
|
-
console.log(`Setting currentMode to ${mode} it was ${this.memory.currentMode}`);
|
|
422
|
-
}
|
|
423
|
-
else {
|
|
424
|
-
console.log(`No currentMode provided, keeping ${this.memory.currentMode}`);
|
|
425
|
-
}
|
|
426
|
-
await this.loadMemoryAsync();
|
|
427
|
-
this.memory.currentMode = mode;
|
|
428
|
-
await this.saveMemory();
|
|
429
|
-
}
|
|
430
|
-
/**
|
|
431
|
-
* Initialize modes from subclass definitions
|
|
432
|
-
*/
|
|
433
|
-
async initializeModes() {
|
|
434
|
-
console.log("---------------------> initializeModes");
|
|
435
|
-
await this.setupMemoryAsync();
|
|
436
|
-
this.availableTools.clear();
|
|
437
|
-
const modes = await this.defineAvailableModes();
|
|
438
|
-
for (const mode of modes) {
|
|
439
|
-
this.modes.set(mode.name, mode);
|
|
440
|
-
console.log(`initializeModes: ${mode.name}`);
|
|
441
|
-
// Register mode's functions
|
|
442
|
-
for (const func of mode.tools) {
|
|
443
|
-
this.availableTools.set(func.name, func);
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
// this.registerCoreFunctions();
|
|
447
|
-
// Set initial mode if none exists
|
|
448
|
-
if (!this.memory.currentMode && modes.length > 0) {
|
|
449
|
-
this.memory.currentMode = modes[0].name;
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
/**
|
|
453
|
-
* Get current mode's functions
|
|
454
|
-
*/
|
|
455
|
-
getCurrentModeFunctions() {
|
|
456
|
-
const currentMode = this.modes.get(this.memory.currentMode);
|
|
457
|
-
if (!currentMode) {
|
|
458
|
-
console.error(`No current mode found: ${this.memory.currentMode}`);
|
|
459
|
-
return [];
|
|
460
|
-
}
|
|
461
|
-
// Combine mode-specific functions with core functions
|
|
462
|
-
return [
|
|
463
|
-
...currentMode.tools,
|
|
464
|
-
// this.availableTools.get("switch_mode")!,
|
|
465
|
-
];
|
|
466
|
-
}
|
|
467
|
-
get isLoggedIn() {
|
|
468
|
-
return Boolean(this.memory?.currentUser);
|
|
469
|
-
}
|
|
470
|
-
renderLoginStatus() {
|
|
471
|
-
return `
|
|
472
|
-
<loginStatus>
|
|
473
|
-
${this.isLoggedIn ? "The user is logged in." : "The user is not logged in."}
|
|
474
|
-
</loginStatus>
|
|
475
|
-
`;
|
|
476
|
-
}
|
|
477
|
-
/**
|
|
478
|
-
* Get current mode's system prompt
|
|
479
|
-
*/
|
|
480
|
-
getCurrentSystemPrompt() {
|
|
481
|
-
const currentMode = this.modes.get(this.memory.currentMode);
|
|
482
|
-
if (!currentMode) {
|
|
483
|
-
console.error(`No current mode found: ${this.memory.currentMode}`);
|
|
484
|
-
return `${this.defaultSystemPrompt}\n\n${this.renderLoginStatus()}`;
|
|
485
|
-
}
|
|
486
|
-
return `${this.defaultSystemPrompt}\n\n${this.renderLoginStatus()}\n\n<furtherAgentInstructions>\n${currentMode.systemPrompt}\n</furtherAgentInstructions>`;
|
|
487
|
-
}
|
|
488
|
-
sendAvatarUrlChange(url, avatarName) {
|
|
489
|
-
this.wsClientSocket.send(JSON.stringify({
|
|
490
|
-
sender: "system",
|
|
491
|
-
type: "avatar_url_change",
|
|
492
|
-
url,
|
|
493
|
-
data: avatarName,
|
|
494
|
-
}));
|
|
495
|
-
this.currentAssistantAvatarUrl = url || undefined;
|
|
496
|
-
}
|
|
497
|
-
/**
|
|
498
|
-
* Validate mode transition
|
|
499
|
-
*/
|
|
500
|
-
validateModeTransition(fromMode, toMode) {
|
|
501
|
-
const currentMode = this.modes.get(fromMode);
|
|
502
|
-
if (!currentMode)
|
|
503
|
-
return false;
|
|
504
|
-
// Check if transition is allowed
|
|
505
|
-
if (currentMode.allowedTransitions &&
|
|
506
|
-
!currentMode.allowedTransitions.includes(toMode)) {
|
|
507
|
-
return false;
|
|
508
|
-
}
|
|
509
|
-
// Check for maximum transitions
|
|
510
|
-
const transitionCount = this.memory.modeHistory?.length ?? 0;
|
|
511
|
-
if (transitionCount >= this.maxModeTransitions) {
|
|
512
|
-
return false;
|
|
513
|
-
}
|
|
514
|
-
return true;
|
|
515
|
-
}
|
|
516
|
-
/**
|
|
517
|
-
* Clean up mode-specific resources
|
|
518
|
-
*/
|
|
519
|
-
async cleanupMode(mode) {
|
|
520
|
-
const modeConfig = this.modes.get(mode);
|
|
521
|
-
if (modeConfig?.cleanup) {
|
|
522
|
-
await modeConfig.cleanup();
|
|
523
|
-
}
|
|
524
|
-
// Archive mode data if needed
|
|
525
|
-
if (this.memory.modeData) {
|
|
526
|
-
const archiveKey = `mode_data_archive:${this.memory.redisKey}:${mode}`;
|
|
527
|
-
await this.redis.set(archiveKey, JSON.stringify(this.memory.modeData));
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
/**
|
|
531
|
-
* Set mode data with type safety
|
|
532
|
-
*/
|
|
533
|
-
async setModeData(type, data) {
|
|
534
|
-
this.memory.modeData = {
|
|
535
|
-
type,
|
|
536
|
-
data,
|
|
537
|
-
timestamp: Date.now(),
|
|
538
|
-
};
|
|
539
|
-
await this.saveMemory();
|
|
540
|
-
}
|
|
541
|
-
/**
|
|
542
|
-
* Get mode data with type safety
|
|
543
|
-
*/
|
|
544
|
-
getModeData() {
|
|
545
|
-
return this.memory.modeData?.data;
|
|
546
|
-
}
|
|
547
|
-
/**
|
|
548
|
-
* Main conversation handler with updated function handling
|
|
549
|
-
*/
|
|
550
|
-
async conversation(chatLog) {
|
|
551
|
-
await this.loadMemoryAsync();
|
|
552
|
-
await this.initializeModes();
|
|
553
|
-
await this.setChatLog(chatLog);
|
|
554
|
-
await this.saveMemory();
|
|
555
|
-
let systemPrompt = this.getCurrentSystemPrompt();
|
|
556
|
-
const subscriptionPlan = await this.getCurrentSubscriptionPlan();
|
|
557
|
-
if (subscriptionPlan &&
|
|
558
|
-
subscriptionPlan.AgentProduct?.configuration.avatar?.imageUrl) {
|
|
559
|
-
this.currentAssistantAvatarUrl =
|
|
560
|
-
subscriptionPlan.AgentProduct.configuration.avatar.imageUrl;
|
|
561
|
-
}
|
|
562
|
-
else {
|
|
563
|
-
this.currentAssistantAvatarUrl = undefined;
|
|
564
|
-
}
|
|
565
|
-
if (this.memory.currentMode === "agent_direct_connection_mode" &&
|
|
566
|
-
subscriptionPlan?.AgentProduct?.configuration.avatar?.systemPrompt) {
|
|
567
|
-
systemPrompt = `${subscriptionPlan.AgentProduct.configuration.avatar.systemPrompt}\n\n${systemPrompt}`;
|
|
568
|
-
}
|
|
569
|
-
if (this.currentAssistantAvatarUrl &&
|
|
570
|
-
subscriptionPlan?.AgentProduct?.name) {
|
|
571
|
-
this.sendAvatarUrlChange(this.currentAssistantAvatarUrl, subscriptionPlan.AgentProduct.name);
|
|
572
|
-
}
|
|
573
|
-
const messages = [
|
|
574
|
-
{
|
|
575
|
-
role: "system",
|
|
576
|
-
content: systemPrompt,
|
|
577
|
-
},
|
|
578
|
-
...this.convertToOpenAIMessages(chatLog),
|
|
579
|
-
];
|
|
580
|
-
try {
|
|
581
|
-
// Convert functions to tools format
|
|
582
|
-
const tools = this.getCurrentModeFunctions().map((f) => ({
|
|
583
|
-
type: "function",
|
|
584
|
-
function: {
|
|
585
|
-
name: f.name,
|
|
586
|
-
description: f.description,
|
|
587
|
-
parameters: f.parameters,
|
|
588
|
-
},
|
|
589
|
-
}));
|
|
590
|
-
console.log("======================> conversation currentMode", this.memory?.currentMode);
|
|
591
|
-
if (this.DEBUG) {
|
|
592
|
-
console.log("======================> conversation", JSON.stringify(messages, null, 2));
|
|
593
|
-
}
|
|
594
|
-
console.log("======================> conversation", JSON.stringify(tools, null, 2));
|
|
595
|
-
const stream = await this.openaiClient.chat.completions.create({
|
|
596
|
-
model: this.modelName,
|
|
597
|
-
messages,
|
|
598
|
-
tools,
|
|
599
|
-
tool_choice: "auto",
|
|
600
|
-
stream: true,
|
|
601
|
-
});
|
|
602
|
-
await this.streamWebSocketResponses(stream);
|
|
603
|
-
}
|
|
604
|
-
catch (error) {
|
|
605
|
-
console.error("Error in conversation:", error);
|
|
606
|
-
this.sendToClient("assistant", "Error processing request", "error");
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
async getAgentProduct(agentProductId) {
|
|
610
|
-
try {
|
|
611
|
-
const agentProduct = await YpAgentProduct.findByPk(agentProductId);
|
|
612
|
-
return agentProduct;
|
|
613
|
-
}
|
|
614
|
-
catch (error) {
|
|
615
|
-
console.error(`Error getting agent product for ${agentProductId}: ${error}`);
|
|
616
|
-
return null;
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
/**
|
|
620
|
-
* Convert chat log to OpenAI message format
|
|
621
|
-
*/
|
|
622
|
-
convertToOpenAIMessages(chatLog) {
|
|
623
|
-
return chatLog
|
|
624
|
-
.filter((message) => message.sender !== "system")
|
|
625
|
-
.map((message) => {
|
|
626
|
-
if (message.message != null) {
|
|
627
|
-
return { role: message.sender, content: message.message };
|
|
628
|
-
}
|
|
629
|
-
else {
|
|
630
|
-
console.debug("Message content is null, message: " + JSON.stringify(message));
|
|
631
|
-
return null;
|
|
632
|
-
}
|
|
633
|
-
})
|
|
634
|
-
.filter((message) => message !== null);
|
|
635
|
-
}
|
|
636
|
-
/**
|
|
637
|
-
* Handle mode switching
|
|
638
|
-
*/
|
|
639
|
-
async handleModeSwitch(newMode, reason, params) {
|
|
640
|
-
await this.loadMemoryAsync();
|
|
641
|
-
console.log(`handleModeSwitch: ${newMode}${reason ? ": " + reason : ""}`);
|
|
642
|
-
const oldMode = this.memory.currentMode;
|
|
643
|
-
if (!this.modes.has(newMode)) {
|
|
644
|
-
throw new Error(`Invalid mode: ${newMode}`);
|
|
645
|
-
}
|
|
646
|
-
if (oldMode === newMode) {
|
|
647
|
-
console.error(`Trying to switch to the same mode: ${oldMode} to ${newMode}`);
|
|
648
|
-
return;
|
|
649
|
-
}
|
|
650
|
-
if (oldMode && !this.validateModeTransition(oldMode, newMode)) {
|
|
651
|
-
console.warn(`Invalid mode transition from ${oldMode} to ${newMode}`);
|
|
652
|
-
}
|
|
653
|
-
// Perform cleanup of old mode
|
|
654
|
-
if (oldMode) {
|
|
655
|
-
await this.cleanupMode(oldMode);
|
|
656
|
-
}
|
|
657
|
-
// Update mode history
|
|
658
|
-
if (!this.memory.modeHistory) {
|
|
659
|
-
this.memory.modeHistory = [];
|
|
660
|
-
}
|
|
661
|
-
this.memory.modeHistory.push({
|
|
662
|
-
mode: newMode,
|
|
663
|
-
timestamp: Date.now(),
|
|
664
|
-
reason,
|
|
665
|
-
});
|
|
666
|
-
this.memory.currentMode = newMode;
|
|
667
|
-
this.memory.modeData = undefined; // Clear mode data
|
|
668
|
-
this.wsClientSocket.send(JSON.stringify({
|
|
669
|
-
sender: "system",
|
|
670
|
-
type: "current_mode",
|
|
671
|
-
mode: this.memory.currentMode,
|
|
672
|
-
}));
|
|
673
|
-
await this.saveMemory();
|
|
674
|
-
console.log("handleModeSwitch: CurrentMode after save", this.memory.currentMode);
|
|
675
|
-
this.sendToClient("system", newMode, "modeChange", undefined, true);
|
|
676
|
-
}
|
|
677
|
-
async addUserMessage(message) {
|
|
678
|
-
await this.loadMemoryAsync();
|
|
679
|
-
this.memory.chatLog.push({
|
|
680
|
-
sender: "user",
|
|
681
|
-
message,
|
|
682
|
-
type: "message",
|
|
683
|
-
});
|
|
684
|
-
await this.saveMemory();
|
|
685
|
-
}
|
|
686
|
-
async addAssistantMessage(message) {
|
|
687
|
-
await this.loadMemoryAsync();
|
|
688
|
-
this.memory.chatLog.push({
|
|
689
|
-
sender: "assistant",
|
|
690
|
-
message: message,
|
|
691
|
-
type: "message",
|
|
692
|
-
avatarUrl: this.currentAssistantAvatarUrl,
|
|
693
|
-
});
|
|
694
|
-
await this.saveMemory();
|
|
695
|
-
}
|
|
696
|
-
async addAssistantHtmlMessage(html, uniqueToken) {
|
|
697
|
-
await this.loadMemoryAsync();
|
|
698
|
-
this.memory.chatLog.push({
|
|
699
|
-
sender: "assistant",
|
|
700
|
-
html,
|
|
701
|
-
message: "",
|
|
702
|
-
type: "html",
|
|
703
|
-
uniqueToken,
|
|
704
|
-
});
|
|
705
|
-
await this.saveMemory();
|
|
706
|
-
}
|
|
707
|
-
/**
|
|
708
|
-
* Handle streaming responses and function calls with comprehensive debugging
|
|
709
|
-
*/
|
|
710
|
-
async streamWebSocketResponses(stream) {
|
|
711
|
-
return new Promise(async (resolve, reject) => {
|
|
712
|
-
if (this.DEBUG)
|
|
713
|
-
console.log("Starting streamWebSocketResponses");
|
|
714
|
-
this.sendToClient("assistant", "", "start");
|
|
715
|
-
try {
|
|
716
|
-
let botMessage = "";
|
|
717
|
-
const toolCalls = new Map();
|
|
718
|
-
const toolCallArguments = new Map();
|
|
719
|
-
let currentToolCallId = null;
|
|
720
|
-
for await (const part of stream) {
|
|
721
|
-
if (this.DEBUG) {
|
|
722
|
-
console.log("Received stream part:", JSON.stringify(part, null, 2));
|
|
723
|
-
}
|
|
724
|
-
if (!part.choices?.[0]?.delta) {
|
|
725
|
-
if (this.DEBUG)
|
|
726
|
-
console.log("Skipping invalid stream part - no choices or delta");
|
|
727
|
-
continue;
|
|
728
|
-
}
|
|
729
|
-
const delta = part.choices[0].delta;
|
|
730
|
-
if (this.DEBUG) {
|
|
731
|
-
console.log("Processing delta:", JSON.stringify(delta, null, 2));
|
|
732
|
-
}
|
|
733
|
-
if ("tool_calls" in delta && delta.tool_calls) {
|
|
734
|
-
if (this.DEBUG)
|
|
735
|
-
console.log("Processing tool calls in delta");
|
|
736
|
-
for (const toolCall of delta.tool_calls) {
|
|
737
|
-
// If we have a new tool call ID, update the current ID
|
|
738
|
-
if (toolCall.id) {
|
|
739
|
-
if (this.DEBUG)
|
|
740
|
-
console.log(`Setting current tool call ID to: ${toolCall.id}`);
|
|
741
|
-
currentToolCallId = toolCall.id;
|
|
742
|
-
}
|
|
743
|
-
// Always use the currentToolCallId for processing
|
|
744
|
-
if (currentToolCallId) {
|
|
745
|
-
const now = Date.now();
|
|
746
|
-
if (this.DEBUG) {
|
|
747
|
-
console.log(`Processing tool call ${currentToolCallId}:`, {
|
|
748
|
-
name: toolCall.function?.name,
|
|
749
|
-
newArguments: toolCall.function?.arguments,
|
|
750
|
-
exists: toolCalls.has(currentToolCallId),
|
|
751
|
-
});
|
|
752
|
-
}
|
|
753
|
-
// Initialize tool call if it's new
|
|
754
|
-
if (!toolCalls.has(currentToolCallId)) {
|
|
755
|
-
if (this.DEBUG)
|
|
756
|
-
console.log(`Initializing new tool call ${currentToolCallId}`);
|
|
757
|
-
toolCalls.set(currentToolCallId, {
|
|
758
|
-
id: currentToolCallId,
|
|
759
|
-
name: toolCall.function?.name ?? "",
|
|
760
|
-
arguments: "",
|
|
761
|
-
startTime: now,
|
|
762
|
-
});
|
|
763
|
-
toolCallArguments.set(currentToolCallId, "");
|
|
764
|
-
}
|
|
765
|
-
const existingCall = toolCalls.get(currentToolCallId);
|
|
766
|
-
// Check timeout
|
|
767
|
-
if (now - existingCall.startTime > this.toolCallTimeout) {
|
|
768
|
-
if (this.DEBUG)
|
|
769
|
-
console.log(`Tool call timeout for ${existingCall.name}`);
|
|
770
|
-
throw new Error(`Tool call timeout for ${existingCall.name}`);
|
|
771
|
-
}
|
|
772
|
-
// Update name if provided
|
|
773
|
-
if (toolCall.function?.name) {
|
|
774
|
-
if (this.DEBUG)
|
|
775
|
-
console.log(`Updating tool call name to ${toolCall.function.name}`);
|
|
776
|
-
existingCall.name = toolCall.function.name;
|
|
777
|
-
}
|
|
778
|
-
// Concatenate arguments if provided
|
|
779
|
-
if (toolCall.function?.arguments) {
|
|
780
|
-
const currentArgs = toolCallArguments.get(currentToolCallId) || "";
|
|
781
|
-
const newArgs = currentArgs + toolCall.function.arguments;
|
|
782
|
-
if (this.DEBUG) {
|
|
783
|
-
console.log(`Updating arguments for ${currentToolCallId}:`, {
|
|
784
|
-
previous: currentArgs,
|
|
785
|
-
new: toolCall.function.arguments,
|
|
786
|
-
combined: newArgs,
|
|
787
|
-
});
|
|
788
|
-
}
|
|
789
|
-
toolCallArguments.set(currentToolCallId, newArgs);
|
|
790
|
-
existingCall.arguments = newArgs;
|
|
791
|
-
}
|
|
792
|
-
toolCalls.set(currentToolCallId, existingCall);
|
|
793
|
-
if (this.DEBUG) {
|
|
794
|
-
console.log(`Current state of tool call ${currentToolCallId}:`, {
|
|
795
|
-
name: existingCall.name,
|
|
796
|
-
arguments: existingCall.arguments,
|
|
797
|
-
});
|
|
798
|
-
}
|
|
799
|
-
}
|
|
800
|
-
else {
|
|
801
|
-
if (this.DEBUG)
|
|
802
|
-
console.log("No current tool call ID available");
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
|
-
else if ("content" in delta && delta.content) {
|
|
807
|
-
if (this.DEBUG)
|
|
808
|
-
console.log("Processing content:", delta.content);
|
|
809
|
-
const content = delta.content;
|
|
810
|
-
this.sendToClient("assistant", content);
|
|
811
|
-
botMessage += content;
|
|
812
|
-
}
|
|
813
|
-
const finishReason = part.choices[0].finish_reason;
|
|
814
|
-
if (this.DEBUG) {
|
|
815
|
-
console.log("Finish reason:", finishReason);
|
|
816
|
-
if (finishReason === "tool_calls") {
|
|
817
|
-
console.log("Final state of all tool calls:", Object.fromEntries(toolCalls.entries()));
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
if (finishReason === "tool_calls") {
|
|
821
|
-
// Reset current tool call ID
|
|
822
|
-
currentToolCallId = null;
|
|
823
|
-
// Validate all accumulated arguments are valid JSON before proceeding
|
|
824
|
-
for (const [id, call] of toolCalls) {
|
|
825
|
-
try {
|
|
826
|
-
if (this.DEBUG) {
|
|
827
|
-
console.log(`Validating JSON for tool call ${id}:`, call.arguments);
|
|
828
|
-
}
|
|
829
|
-
// Handle empty arguments case
|
|
830
|
-
if (!call.arguments.trim()) {
|
|
831
|
-
call.arguments = "{}"; // Set default empty object
|
|
832
|
-
}
|
|
833
|
-
JSON.parse(call.arguments); // Validate JSON
|
|
834
|
-
}
|
|
835
|
-
catch (e) {
|
|
836
|
-
if (this.DEBUG) {
|
|
837
|
-
console.error(`JSON validation failed for ${id}:`, e);
|
|
838
|
-
console.log("Invalid arguments:", call.arguments);
|
|
839
|
-
}
|
|
840
|
-
throw new Error(`Invalid JSON in function arguments for ${call.name}: ${call.arguments}`);
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
if (this.DEBUG)
|
|
844
|
-
console.log("Executing tool calls");
|
|
845
|
-
await this.handleToolCalls(toolCalls);
|
|
846
|
-
if (this.DEBUG)
|
|
847
|
-
console.log("Clearing tool calls and arguments");
|
|
848
|
-
toolCalls.clear();
|
|
849
|
-
toolCallArguments.clear();
|
|
850
|
-
}
|
|
851
|
-
else if (finishReason === "stop") {
|
|
852
|
-
if (botMessage) {
|
|
853
|
-
if (this.DEBUG)
|
|
854
|
-
console.log("Saving bot message to chat log");
|
|
855
|
-
this.memory.chatLog.push({
|
|
856
|
-
sender: "assistant",
|
|
857
|
-
message: botMessage,
|
|
858
|
-
type: "message",
|
|
859
|
-
avatarUrl: this.currentAssistantAvatarUrl,
|
|
860
|
-
});
|
|
861
|
-
await this.saveMemoryIfNeeded();
|
|
862
|
-
}
|
|
863
|
-
}
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
catch (error) {
|
|
867
|
-
console.error("Stream processing error:", error);
|
|
868
|
-
// Attempt to switch to error recovery mode
|
|
869
|
-
try {
|
|
870
|
-
if (this.DEBUG)
|
|
871
|
-
console.log("Attempting to switch to error recovery mode");
|
|
872
|
-
await this.handleModeSwitch("error_recovery", error instanceof Error ? error.message : "Unknown error", {});
|
|
873
|
-
}
|
|
874
|
-
catch (e) {
|
|
875
|
-
console.error("Failed to switch to error recovery mode:", e);
|
|
876
|
-
}
|
|
877
|
-
this.sendToClient("assistant", "There has been an error, please retry", "error");
|
|
878
|
-
reject(error);
|
|
879
|
-
}
|
|
880
|
-
finally {
|
|
881
|
-
if (this.DEBUG)
|
|
882
|
-
console.log("Finalizing stream response");
|
|
883
|
-
this.sendToClient("assistant", "", "end");
|
|
884
|
-
resolve();
|
|
885
|
-
}
|
|
886
|
-
});
|
|
887
|
-
}
|
|
888
|
-
}
|