@graphoria/server 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +10 -0
- package/README.md +129 -0
- package/cli.ts +105 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/src/ai/agent/agent.d.ts +37 -0
- package/dist/src/ai/agent/agent.d.ts.map +1 -0
- package/dist/src/ai/agent/index.d.ts +3 -0
- package/dist/src/ai/agent/index.d.ts.map +1 -0
- package/dist/src/ai/agent/providers/anthropic.d.ts +10 -0
- package/dist/src/ai/agent/providers/anthropic.d.ts.map +1 -0
- package/dist/src/ai/agent/providers/index.d.ts +8 -0
- package/dist/src/ai/agent/providers/index.d.ts.map +1 -0
- package/dist/src/ai/agent/providers/ollama.d.ts +11 -0
- package/dist/src/ai/agent/providers/ollama.d.ts.map +1 -0
- package/dist/src/ai/agent/providers/openai.d.ts +14 -0
- package/dist/src/ai/agent/providers/openai.d.ts.map +1 -0
- package/dist/src/ai/agent/types.d.ts +51 -0
- package/dist/src/ai/agent/types.d.ts.map +1 -0
- package/dist/src/ai/ask-field.test.d.ts +2 -0
- package/dist/src/ai/ask-field.test.d.ts.map +1 -0
- package/dist/src/ai/index.d.ts +7 -0
- package/dist/src/ai/index.d.ts.map +1 -0
- package/dist/src/ai/mcp/create-server.d.ts +14 -0
- package/dist/src/ai/mcp/create-server.d.ts.map +1 -0
- package/dist/src/ai/mcp/create-server.test.d.ts +2 -0
- package/dist/src/ai/mcp/create-server.test.d.ts.map +1 -0
- package/dist/src/ai/mcp/index.d.ts +13 -0
- package/dist/src/ai/mcp/index.d.ts.map +1 -0
- package/dist/src/ai/mcp/index.test.d.ts +2 -0
- package/dist/src/ai/mcp/index.test.d.ts.map +1 -0
- package/dist/src/ai/mcp/instructions.d.ts +2 -0
- package/dist/src/ai/mcp/instructions.d.ts.map +1 -0
- package/dist/src/ai/tools/agent.d.ts +4 -0
- package/dist/src/ai/tools/agent.d.ts.map +1 -0
- package/dist/src/ai/tools/agent.test.d.ts +2 -0
- package/dist/src/ai/tools/agent.test.d.ts.map +1 -0
- package/dist/src/ai/tools/core.d.ts +75 -0
- package/dist/src/ai/tools/core.d.ts.map +1 -0
- package/dist/src/ai/tools/query-data.d.ts +35 -0
- package/dist/src/ai/tools/query-data.d.ts.map +1 -0
- package/dist/src/analyzeQuery/analyzers/fragmentAnalyzer.d.ts +5 -0
- package/dist/src/analyzeQuery/analyzers/fragmentAnalyzer.d.ts.map +1 -0
- package/dist/src/analyzeQuery/analyzers/operationAnalyzer.d.ts +6 -0
- package/dist/src/analyzeQuery/analyzers/operationAnalyzer.d.ts.map +1 -0
- package/dist/src/analyzeQuery/analyzers/selectionAnalyzer.d.ts +6 -0
- package/dist/src/analyzeQuery/analyzers/selectionAnalyzer.d.ts.map +1 -0
- package/dist/src/analyzeQuery/analyzers/variableAnalyzer.d.ts +4 -0
- package/dist/src/analyzeQuery/analyzers/variableAnalyzer.d.ts.map +1 -0
- package/dist/src/analyzeQuery/depthLimit.d.ts +16 -0
- package/dist/src/analyzeQuery/depthLimit.d.ts.map +1 -0
- package/dist/src/analyzeQuery/depthLimit.test.d.ts +2 -0
- package/dist/src/analyzeQuery/depthLimit.test.d.ts.map +1 -0
- package/dist/src/analyzeQuery/directiveUtils.d.ts +4 -0
- package/dist/src/analyzeQuery/directiveUtils.d.ts.map +1 -0
- package/dist/src/analyzeQuery/directiveUtils.test.d.ts +2 -0
- package/dist/src/analyzeQuery/directiveUtils.test.d.ts.map +1 -0
- package/dist/src/analyzeQuery/index.d.ts +5 -0
- package/dist/src/analyzeQuery/index.d.ts.map +1 -0
- package/dist/src/analyzeQuery/index.test.d.ts +2 -0
- package/dist/src/analyzeQuery/index.test.d.ts.map +1 -0
- package/dist/src/analyzeQuery/resolveVariables.d.ts +71 -0
- package/dist/src/analyzeQuery/resolveVariables.d.ts.map +1 -0
- package/dist/src/analyzeQuery/resolveVariables.test.d.ts +2 -0
- package/dist/src/analyzeQuery/resolveVariables.test.d.ts.map +1 -0
- package/dist/src/analyzeQuery/staticValues.test.d.ts +2 -0
- package/dist/src/analyzeQuery/staticValues.test.d.ts.map +1 -0
- package/dist/src/analyzeQuery/typeUtils.d.ts +9 -0
- package/dist/src/analyzeQuery/typeUtils.d.ts.map +1 -0
- package/dist/src/analyzeQuery/types.d.ts +54 -0
- package/dist/src/analyzeQuery/types.d.ts.map +1 -0
- package/dist/src/analyzeQuery/valueExtractors.d.ts +4 -0
- package/dist/src/analyzeQuery/valueExtractors.d.ts.map +1 -0
- package/dist/src/authentication/duration.d.ts +14 -0
- package/dist/src/authentication/duration.d.ts.map +1 -0
- package/dist/src/authentication/duration.test.d.ts +2 -0
- package/dist/src/authentication/duration.test.d.ts.map +1 -0
- package/dist/src/authentication/index.d.ts +4 -0
- package/dist/src/authentication/index.d.ts.map +1 -0
- package/dist/src/authentication/index.test.d.ts +2 -0
- package/dist/src/authentication/index.test.d.ts.map +1 -0
- package/dist/src/authentication/jwt.d.ts +10 -0
- package/dist/src/authentication/jwt.d.ts.map +1 -0
- package/dist/src/authentication/jwt.test.d.ts +2 -0
- package/dist/src/authentication/jwt.test.d.ts.map +1 -0
- package/dist/src/authentication/paseto.d.ts +7 -0
- package/dist/src/authentication/paseto.d.ts.map +1 -0
- package/dist/src/authentication/paseto.test.d.ts +2 -0
- package/dist/src/authentication/paseto.test.d.ts.map +1 -0
- package/dist/src/authentication/tokenRepository.d.ts +14 -0
- package/dist/src/authentication/tokenRepository.d.ts.map +1 -0
- package/dist/src/authentication/tokenRepository.test.d.ts +2 -0
- package/dist/src/authentication/tokenRepository.test.d.ts.map +1 -0
- package/dist/src/authentication/types.d.ts +42 -0
- package/dist/src/authentication/types.d.ts.map +1 -0
- package/dist/src/cli/seedAuth.d.ts +4 -0
- package/dist/src/cli/seedAuth.d.ts.map +1 -0
- package/dist/src/cli/seedAuth.test.d.ts +2 -0
- package/dist/src/cli/seedAuth.test.d.ts.map +1 -0
- package/dist/src/cli/seedAuthArgs.d.ts +9 -0
- package/dist/src/cli/seedAuthArgs.d.ts.map +1 -0
- package/dist/src/config/helpers/cronHelper.d.ts +21 -0
- package/dist/src/config/helpers/cronHelper.d.ts.map +1 -0
- package/dist/src/config/helpers/cronHelper.test.d.ts +2 -0
- package/dist/src/config/helpers/cronHelper.test.d.ts.map +1 -0
- package/dist/src/config/helpers/index.d.ts +4 -0
- package/dist/src/config/helpers/index.d.ts.map +1 -0
- package/dist/src/config/helpers/operationHelper.d.ts +44 -0
- package/dist/src/config/helpers/operationHelper.d.ts.map +1 -0
- package/dist/src/config/helpers/queueHelper.d.ts +20 -0
- package/dist/src/config/helpers/queueHelper.d.ts.map +1 -0
- package/dist/src/config/helpers/queueHelper.test.d.ts +2 -0
- package/dist/src/config/helpers/queueHelper.test.d.ts.map +1 -0
- package/dist/src/config/index.d.ts +236 -0
- package/dist/src/config/index.d.ts.map +1 -0
- package/dist/src/config/types/ai.d.ts +29 -0
- package/dist/src/config/types/ai.d.ts.map +1 -0
- package/dist/src/config/types/auth.d.ts +112 -0
- package/dist/src/config/types/auth.d.ts.map +1 -0
- package/dist/src/config/types/configuration.d.ts +61 -0
- package/dist/src/config/types/configuration.d.ts.map +1 -0
- package/dist/src/config/types/cron.d.ts +81 -0
- package/dist/src/config/types/cron.d.ts.map +1 -0
- package/dist/src/config/types/db.d.ts +210 -0
- package/dist/src/config/types/db.d.ts.map +1 -0
- package/dist/src/config/types/index.d.ts +17 -0
- package/dist/src/config/types/index.d.ts.map +1 -0
- package/dist/src/config/types/operation.d.ts +226 -0
- package/dist/src/config/types/operation.d.ts.map +1 -0
- package/dist/src/config/types/queue.d.ts +142 -0
- package/dist/src/config/types/queue.d.ts.map +1 -0
- package/dist/src/config/types/remote-rest.d.ts +18 -0
- package/dist/src/config/types/remote-rest.d.ts.map +1 -0
- package/dist/src/config/types/remote-schema.d.ts +27 -0
- package/dist/src/config/types/remote-schema.d.ts.map +1 -0
- package/dist/src/config/types/virtual-columns.d.ts +89 -0
- package/dist/src/config/types/virtual-columns.d.ts.map +1 -0
- package/dist/src/config/types/virtual-columns.test.d.ts +2 -0
- package/dist/src/config/types/virtual-columns.test.d.ts.map +1 -0
- package/dist/src/configuration/getSchemas/index.d.ts +867 -0
- package/dist/src/configuration/getSchemas/index.d.ts.map +1 -0
- package/dist/src/configuration/getSchemas/mergeEntities/index.d.ts +412 -0
- package/dist/src/configuration/getSchemas/mergeEntities/index.d.ts.map +1 -0
- package/dist/src/configuration/getSchemas/type-definition-generator/index.d.ts +16 -0
- package/dist/src/configuration/getSchemas/type-definition-generator/index.d.ts.map +1 -0
- package/dist/src/configuration/getSchemas/type-definition-generator/index.test.d.ts +2 -0
- package/dist/src/configuration/getSchemas/type-definition-generator/index.test.d.ts.map +1 -0
- package/dist/src/configuration/gql/buildExecute.d.ts +31 -0
- package/dist/src/configuration/gql/buildExecute.d.ts.map +1 -0
- package/dist/src/configuration/gql/buildExecute.test.d.ts +2 -0
- package/dist/src/configuration/gql/buildExecute.test.d.ts.map +1 -0
- package/dist/src/configuration/gql/gqlAuthOperations.d.ts +9 -0
- package/dist/src/configuration/gql/gqlAuthOperations.d.ts.map +1 -0
- package/dist/src/configuration/gql/gqlAuthOperations.test.d.ts +2 -0
- package/dist/src/configuration/gql/gqlAuthOperations.test.d.ts.map +1 -0
- package/dist/src/configuration/gql/handleGraphQLRequestFactory.d.ts +28 -0
- package/dist/src/configuration/gql/handleGraphQLRequestFactory.d.ts.map +1 -0
- package/dist/src/configuration/gql/handleGraphQLRequestFactory.test.d.ts +2 -0
- package/dist/src/configuration/gql/handleGraphQLRequestFactory.test.d.ts.map +1 -0
- package/dist/src/configuration/gql/handleGraphQLSubscriptionFactory.d.ts +19 -0
- package/dist/src/configuration/gql/handleGraphQLSubscriptionFactory.d.ts.map +1 -0
- package/dist/src/configuration/index.d.ts +719 -0
- package/dist/src/configuration/index.d.ts.map +1 -0
- package/dist/src/configuration/rest/generateOpenAPI.d.ts +19 -0
- package/dist/src/configuration/rest/generateOpenAPI.d.ts.map +1 -0
- package/dist/src/configuration/rest/handleRESTRequestFactory.d.ts +12 -0
- package/dist/src/configuration/rest/handleRESTRequestFactory.d.ts.map +1 -0
- package/dist/src/configuration/rest/handleRESTRequestFactory.test.d.ts +2 -0
- package/dist/src/configuration/rest/handleRESTRequestFactory.test.d.ts.map +1 -0
- package/dist/src/configuration/rest/index.d.ts +20 -0
- package/dist/src/configuration/rest/index.d.ts.map +1 -0
- package/dist/src/configuration/rest/openApiErrors.d.ts +71 -0
- package/dist/src/configuration/rest/openApiErrors.d.ts.map +1 -0
- package/dist/src/console/App.d.ts +3 -0
- package/dist/src/console/App.d.ts.map +1 -0
- package/dist/src/console/Login.d.ts +6 -0
- package/dist/src/console/Login.d.ts.map +1 -0
- package/dist/src/console/api.d.ts +17 -0
- package/dist/src/console/api.d.ts.map +1 -0
- package/dist/src/console/api.test.d.ts +2 -0
- package/dist/src/console/api.test.d.ts.map +1 -0
- package/dist/src/console/client.d.ts +67 -0
- package/dist/src/console/client.d.ts.map +1 -0
- package/dist/src/console/frontend.d.ts +8 -0
- package/dist/src/console/frontend.d.ts.map +1 -0
- package/dist/src/console/pages/ConfigPage.d.ts +2 -0
- package/dist/src/console/pages/ConfigPage.d.ts.map +1 -0
- package/dist/src/console/pages/RolesPage.d.ts +2 -0
- package/dist/src/console/pages/RolesPage.d.ts.map +1 -0
- package/dist/src/console/pages/StatusPage.d.ts +2 -0
- package/dist/src/console/pages/StatusPage.d.ts.map +1 -0
- package/dist/src/console/pages/TablesPage.d.ts +2 -0
- package/dist/src/console/pages/TablesPage.d.ts.map +1 -0
- package/dist/src/console/useApi.d.ts +8 -0
- package/dist/src/console/useApi.d.ts.map +1 -0
- package/dist/src/cron/index.d.ts +63 -0
- package/dist/src/cron/index.d.ts.map +1 -0
- package/dist/src/databases/auth/auth-operations.d.ts +11 -0
- package/dist/src/databases/auth/auth-operations.d.ts.map +1 -0
- package/dist/src/databases/auth/password.d.ts +3 -0
- package/dist/src/databases/auth/password.d.ts.map +1 -0
- package/dist/src/databases/auth/password.test.d.ts +2 -0
- package/dist/src/databases/auth/password.test.d.ts.map +1 -0
- package/dist/src/databases/common.d.ts +53 -0
- package/dist/src/databases/common.d.ts.map +1 -0
- package/dist/src/databases/common.test.d.ts +2 -0
- package/dist/src/databases/common.test.d.ts.map +1 -0
- package/dist/src/databases/core/executor.d.ts +10 -0
- package/dist/src/databases/core/executor.d.ts.map +1 -0
- package/dist/src/databases/core/executor.test.d.ts +2 -0
- package/dist/src/databases/core/executor.test.d.ts.map +1 -0
- package/dist/src/databases/core/function-mapping.d.ts +38 -0
- package/dist/src/databases/core/function-mapping.d.ts.map +1 -0
- package/dist/src/databases/core/identifier.d.ts +2 -0
- package/dist/src/databases/core/identifier.d.ts.map +1 -0
- package/dist/src/databases/core/identifier.test.d.ts +2 -0
- package/dist/src/databases/core/identifier.test.d.ts.map +1 -0
- package/dist/src/databases/core/query-builder.d.ts +64 -0
- package/dist/src/databases/core/query-builder.d.ts.map +1 -0
- package/dist/src/databases/core/query-builder.test.d.ts +2 -0
- package/dist/src/databases/core/query-builder.test.d.ts.map +1 -0
- package/dist/src/databases/directives.d.ts +16 -0
- package/dist/src/databases/directives.d.ts.map +1 -0
- package/dist/src/databases/directives.test.d.ts +2 -0
- package/dist/src/databases/directives.test.d.ts.map +1 -0
- package/dist/src/databases/engines/mssql/auth.d.ts +9 -0
- package/dist/src/databases/engines/mssql/auth.d.ts.map +1 -0
- package/dist/src/databases/engines/mssql/auth.test.d.ts +2 -0
- package/dist/src/databases/engines/mssql/auth.test.d.ts.map +1 -0
- package/dist/src/databases/engines/mssql/connection.d.ts +14 -0
- package/dist/src/databases/engines/mssql/connection.d.ts.map +1 -0
- package/dist/src/databases/engines/mssql/format.d.ts +5 -0
- package/dist/src/databases/engines/mssql/format.d.ts.map +1 -0
- package/dist/src/databases/engines/mssql/getStructure.d.ts +48 -0
- package/dist/src/databases/engines/mssql/getStructure.d.ts.map +1 -0
- package/dist/src/databases/engines/mssql/getViews.d.ts +7 -0
- package/dist/src/databases/engines/mssql/getViews.d.ts.map +1 -0
- package/dist/src/databases/engines/mssql/query/index.d.ts +7 -0
- package/dist/src/databases/engines/mssql/query/index.d.ts.map +1 -0
- package/dist/src/databases/engines/mssql/query/index.test.d.ts +2 -0
- package/dist/src/databases/engines/mssql/query/index.test.d.ts.map +1 -0
- package/dist/src/databases/engines/mysql/auth.d.ts +9 -0
- package/dist/src/databases/engines/mysql/auth.d.ts.map +1 -0
- package/dist/src/databases/engines/mysql/auth.test.d.ts +2 -0
- package/dist/src/databases/engines/mysql/auth.test.d.ts.map +1 -0
- package/dist/src/databases/engines/mysql/connection.d.ts +14 -0
- package/dist/src/databases/engines/mysql/connection.d.ts.map +1 -0
- package/dist/src/databases/engines/mysql/format.d.ts +5 -0
- package/dist/src/databases/engines/mysql/format.d.ts.map +1 -0
- package/dist/src/databases/engines/mysql/getStructure.d.ts +48 -0
- package/dist/src/databases/engines/mysql/getStructure.d.ts.map +1 -0
- package/dist/src/databases/engines/mysql/getViews.d.ts +7 -0
- package/dist/src/databases/engines/mysql/getViews.d.ts.map +1 -0
- package/dist/src/databases/engines/mysql/query/index.d.ts +7 -0
- package/dist/src/databases/engines/mysql/query/index.d.ts.map +1 -0
- package/dist/src/databases/engines/mysql/query/index.test.d.ts +2 -0
- package/dist/src/databases/engines/mysql/query/index.test.d.ts.map +1 -0
- package/dist/src/databases/engines/postgresql/auth.d.ts +9 -0
- package/dist/src/databases/engines/postgresql/auth.d.ts.map +1 -0
- package/dist/src/databases/engines/postgresql/auth.test.d.ts +2 -0
- package/dist/src/databases/engines/postgresql/auth.test.d.ts.map +1 -0
- package/dist/src/databases/engines/postgresql/connection.d.ts +14 -0
- package/dist/src/databases/engines/postgresql/connection.d.ts.map +1 -0
- package/dist/src/databases/engines/postgresql/format.d.ts +5 -0
- package/dist/src/databases/engines/postgresql/format.d.ts.map +1 -0
- package/dist/src/databases/engines/postgresql/getStructure.d.ts +48 -0
- package/dist/src/databases/engines/postgresql/getStructure.d.ts.map +1 -0
- package/dist/src/databases/engines/postgresql/getViews.d.ts +7 -0
- package/dist/src/databases/engines/postgresql/getViews.d.ts.map +1 -0
- package/dist/src/databases/engines/postgresql/query/index.d.ts +7 -0
- package/dist/src/databases/engines/postgresql/query/index.d.ts.map +1 -0
- package/dist/src/databases/engines/postgresql/query/index.test.d.ts +2 -0
- package/dist/src/databases/engines/postgresql/query/index.test.d.ts.map +1 -0
- package/dist/src/databases/engines/shared/claims.d.ts +2 -0
- package/dist/src/databases/engines/shared/claims.d.ts.map +1 -0
- package/dist/src/databases/engines/shared/types.d.ts +8 -0
- package/dist/src/databases/engines/shared/types.d.ts.map +1 -0
- package/dist/src/databases/high-level-operations.d.ts +146 -0
- package/dist/src/databases/high-level-operations.d.ts.map +1 -0
- package/dist/src/databases/high-level-operations.test.d.ts +2 -0
- package/dist/src/databases/high-level-operations.test.d.ts.map +1 -0
- package/dist/src/databases/index.d.ts +8 -0
- package/dist/src/databases/index.d.ts.map +1 -0
- package/dist/src/databases/metadata/structure.d.ts +56 -0
- package/dist/src/databases/metadata/structure.d.ts.map +1 -0
- package/dist/src/databases/schemaBuilder/convert.d.ts +2 -0
- package/dist/src/databases/schemaBuilder/convert.d.ts.map +1 -0
- package/dist/src/databases/schemaBuilder/generateCreateTable/index.d.ts +15 -0
- package/dist/src/databases/schemaBuilder/generateCreateTable/index.d.ts.map +1 -0
- package/dist/src/databases/schemaBuilder/generateCreateTable/index.test.d.ts +2 -0
- package/dist/src/databases/schemaBuilder/generateCreateTable/index.test.d.ts.map +1 -0
- package/dist/src/databases/schemaBuilder/generateCreateView/index.d.ts +18 -0
- package/dist/src/databases/schemaBuilder/generateCreateView/index.d.ts.map +1 -0
- package/dist/src/databases/schemaBuilder/generateInsert/index.d.ts +8 -0
- package/dist/src/databases/schemaBuilder/generateInsert/index.d.ts.map +1 -0
- package/dist/src/databases/schemaBuilder/generateInsert/index.test.d.ts +2 -0
- package/dist/src/databases/schemaBuilder/generateInsert/index.test.d.ts.map +1 -0
- package/dist/src/databases/schemaBuilder/generateSchemaCreation/index.d.ts +7 -0
- package/dist/src/databases/schemaBuilder/generateSchemaCreation/index.d.ts.map +1 -0
- package/dist/src/databases/schemaBuilder/generateSchemaCreation/index.test.d.ts +2 -0
- package/dist/src/databases/schemaBuilder/generateSchemaCreation/index.test.d.ts.map +1 -0
- package/dist/src/databases/schemaBuilder/mssqlToPostgres/convertView.d.ts +2 -0
- package/dist/src/databases/schemaBuilder/mssqlToPostgres/convertView.d.ts.map +1 -0
- package/dist/src/databases/schemaBuilder/utils.d.ts +8 -0
- package/dist/src/databases/schemaBuilder/utils.d.ts.map +1 -0
- package/dist/src/databases/sqlTypeUtils.d.ts +24 -0
- package/dist/src/databases/sqlTypeUtils.d.ts.map +1 -0
- package/dist/src/databases/sqlTypeUtils.test.d.ts +2 -0
- package/dist/src/databases/sqlTypeUtils.test.d.ts.map +1 -0
- package/dist/src/databases/transformers/data-transformers.d.ts +215 -0
- package/dist/src/databases/transformers/data-transformers.d.ts.map +1 -0
- package/dist/src/databases/transformers/data-transformers.test.d.ts +2 -0
- package/dist/src/databases/transformers/data-transformers.test.d.ts.map +1 -0
- package/dist/src/databases/transformers/genResolverName.d.ts +2 -0
- package/dist/src/databases/transformers/genResolverName.d.ts.map +1 -0
- package/dist/src/databases/transformers/genResolverName.test.d.ts +2 -0
- package/dist/src/databases/transformers/genResolverName.test.d.ts.map +1 -0
- package/dist/src/index.d.ts +139 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/logging/index.d.ts +21 -0
- package/dist/src/logging/index.d.ts.map +1 -0
- package/dist/src/queues/kafka.d.ts +22 -0
- package/dist/src/queues/kafka.d.ts.map +1 -0
- package/dist/src/queues/rabbitmq.d.ts +34 -0
- package/dist/src/queues/rabbitmq.d.ts.map +1 -0
- package/dist/src/queues/rabbitmq.test.d.ts +2 -0
- package/dist/src/queues/rabbitmq.test.d.ts.map +1 -0
- package/dist/src/remoteREST/index.d.ts +13 -0
- package/dist/src/remoteREST/index.d.ts.map +1 -0
- package/dist/src/remoteREST/parse.d.ts +8 -0
- package/dist/src/remoteREST/parse.d.ts.map +1 -0
- package/dist/src/remoteREST/parse.test.d.ts +2 -0
- package/dist/src/remoteREST/parse.test.d.ts.map +1 -0
- package/dist/src/remoteREST/proxy.d.ts +6 -0
- package/dist/src/remoteREST/proxy.d.ts.map +1 -0
- package/dist/src/remoteREST/proxy.test.d.ts +2 -0
- package/dist/src/remoteREST/proxy.test.d.ts.map +1 -0
- package/dist/src/remoteREST/transform.d.ts +8 -0
- package/dist/src/remoteREST/transform.d.ts.map +1 -0
- package/dist/src/remoteREST/transform.test.d.ts +2 -0
- package/dist/src/remoteREST/transform.test.d.ts.map +1 -0
- package/dist/src/remoteREST/types.d.ts +37 -0
- package/dist/src/remoteREST/types.d.ts.map +1 -0
- package/dist/src/remoteSchemas/index.d.ts +13 -0
- package/dist/src/remoteSchemas/index.d.ts.map +1 -0
- package/dist/src/remoteSchemas/introspect.d.ts +7 -0
- package/dist/src/remoteSchemas/introspect.d.ts.map +1 -0
- package/dist/src/remoteSchemas/introspect.test.d.ts +2 -0
- package/dist/src/remoteSchemas/introspect.test.d.ts.map +1 -0
- package/dist/src/remoteSchemas/proxy.d.ts +7 -0
- package/dist/src/remoteSchemas/proxy.d.ts.map +1 -0
- package/dist/src/remoteSchemas/proxy.test.d.ts +2 -0
- package/dist/src/remoteSchemas/proxy.test.d.ts.map +1 -0
- package/dist/src/remoteSchemas/transform.d.ts +9 -0
- package/dist/src/remoteSchemas/transform.d.ts.map +1 -0
- package/dist/src/remoteSchemas/transform.test.d.ts +2 -0
- package/dist/src/remoteSchemas/transform.test.d.ts.map +1 -0
- package/dist/src/remoteSchemas/types.d.ts +41 -0
- package/dist/src/remoteSchemas/types.d.ts.map +1 -0
- package/dist/src/singletons/ai.d.ts +25 -0
- package/dist/src/singletons/ai.d.ts.map +1 -0
- package/dist/src/singletons/ai.test.d.ts +2 -0
- package/dist/src/singletons/ai.test.d.ts.map +1 -0
- package/dist/src/singletons/authentication.d.ts +4 -0
- package/dist/src/singletons/authentication.d.ts.map +1 -0
- package/dist/src/singletons/cache/index.d.ts +5 -0
- package/dist/src/singletons/cache/index.d.ts.map +1 -0
- package/dist/src/singletons/cache/lruCacheStore.d.ts +12 -0
- package/dist/src/singletons/cache/lruCacheStore.d.ts.map +1 -0
- package/dist/src/singletons/cache/lruCacheStore.test.d.ts +2 -0
- package/dist/src/singletons/cache/lruCacheStore.test.d.ts.map +1 -0
- package/dist/src/singletons/cache/redisCacheStore.d.ts +16 -0
- package/dist/src/singletons/cache/redisCacheStore.d.ts.map +1 -0
- package/dist/src/singletons/cache/redisClient.d.ts +3 -0
- package/dist/src/singletons/cache/redisClient.d.ts.map +1 -0
- package/dist/src/singletons/cache/registry.d.ts +7 -0
- package/dist/src/singletons/cache/registry.d.ts.map +1 -0
- package/dist/src/singletons/cache/registry.test.d.ts +2 -0
- package/dist/src/singletons/cache/registry.test.d.ts.map +1 -0
- package/dist/src/singletons/cache/types.d.ts +8 -0
- package/dist/src/singletons/cache/types.d.ts.map +1 -0
- package/dist/src/singletons/cron.d.ts +69 -0
- package/dist/src/singletons/cron.d.ts.map +1 -0
- package/dist/src/singletons/databases.d.ts +27 -0
- package/dist/src/singletons/databases.d.ts.map +1 -0
- package/dist/src/singletons/databases.test.d.ts +2 -0
- package/dist/src/singletons/databases.test.d.ts.map +1 -0
- package/dist/src/singletons/env.d.ts +494 -0
- package/dist/src/singletons/env.d.ts.map +1 -0
- package/dist/src/singletons/queues.d.ts +17 -0
- package/dist/src/singletons/queues.d.ts.map +1 -0
- package/dist/src/subscriptions/index.d.ts +4 -0
- package/dist/src/subscriptions/index.d.ts.map +1 -0
- package/dist/src/subscriptions/strategies/database.d.ts +12 -0
- package/dist/src/subscriptions/strategies/database.d.ts.map +1 -0
- package/dist/src/subscriptions/strategies/index.d.ts +12 -0
- package/dist/src/subscriptions/strategies/index.d.ts.map +1 -0
- package/dist/src/subscriptions/strategies/queue.d.ts +11 -0
- package/dist/src/subscriptions/strategies/queue.d.ts.map +1 -0
- package/dist/src/subscriptions/types.d.ts +67 -0
- package/dist/src/subscriptions/types.d.ts.map +1 -0
- package/dist/src/subscriptions/utils/polling.d.ts +17 -0
- package/dist/src/subscriptions/utils/polling.d.ts.map +1 -0
- package/dist/src/types/common.d.ts +44 -0
- package/dist/src/types/common.d.ts.map +1 -0
- package/dist/src/types/configuration.d.ts +28 -0
- package/dist/src/types/configuration.d.ts.map +1 -0
- package/dist/src/types/db.d.ts +43 -0
- package/dist/src/types/db.d.ts.map +1 -0
- package/dist/src/types/env.d.ts +2302 -0
- package/dist/src/types/env.d.ts.map +1 -0
- package/dist/src/types/env.test.d.ts +2 -0
- package/dist/src/types/env.test.d.ts.map +1 -0
- package/dist/src/types/resolver.d.ts +121 -0
- package/dist/src/types/resolver.d.ts.map +1 -0
- package/dist/src/types/zod/ai.d.ts +6 -0
- package/dist/src/types/zod/ai.d.ts.map +1 -0
- package/dist/src/types/zod/auth.d.ts +76 -0
- package/dist/src/types/zod/auth.d.ts.map +1 -0
- package/dist/src/types/zod/configuration.d.ts +1278 -0
- package/dist/src/types/zod/configuration.d.ts.map +1 -0
- package/dist/src/types/zod/configuration.test.d.ts +2 -0
- package/dist/src/types/zod/configuration.test.d.ts.map +1 -0
- package/dist/src/types/zod/cron.d.ts +32 -0
- package/dist/src/types/zod/cron.d.ts.map +1 -0
- package/dist/src/types/zod/db.d.ts +441 -0
- package/dist/src/types/zod/db.d.ts.map +1 -0
- package/dist/src/types/zod/db.test.d.ts +2 -0
- package/dist/src/types/zod/db.test.d.ts.map +1 -0
- package/dist/src/types/zod/mcp.d.ts +6 -0
- package/dist/src/types/zod/mcp.d.ts.map +1 -0
- package/dist/src/types/zod/operation.d.ts +9 -0
- package/dist/src/types/zod/operation.d.ts.map +1 -0
- package/dist/src/types/zod/queue.d.ts +656 -0
- package/dist/src/types/zod/queue.d.ts.map +1 -0
- package/dist/src/types/zod/queue.test.d.ts +2 -0
- package/dist/src/types/zod/queue.test.d.ts.map +1 -0
- package/dist/src/types/zod/remoteREST.d.ts +6 -0
- package/dist/src/types/zod/remoteREST.d.ts.map +1 -0
- package/dist/src/types/zod/remoteSchema.d.ts +6 -0
- package/dist/src/types/zod/remoteSchema.d.ts.map +1 -0
- package/dist/src/utils/benchmark.d.ts +2 -0
- package/dist/src/utils/benchmark.d.ts.map +1 -0
- package/dist/src/utils/event-emitter.d.ts +19 -0
- package/dist/src/utils/event-emitter.d.ts.map +1 -0
- package/dist/src/utils/responses.d.ts +19 -0
- package/dist/src/utils/responses.d.ts.map +1 -0
- package/dist/src/utils/responses.test.d.ts +2 -0
- package/dist/src/utils/responses.test.d.ts.map +1 -0
- package/dist/src/utils/safeJSONParse.d.ts +2 -0
- package/dist/src/utils/safeJSONParse.d.ts.map +1 -0
- package/dist/src/utils/selection.d.ts +3 -0
- package/dist/src/utils/selection.d.ts.map +1 -0
- package/dist/src/utils/selection.test.d.ts +2 -0
- package/dist/src/utils/selection.test.d.ts.map +1 -0
- package/dist/src/utils/sessionVariables.d.ts +47 -0
- package/dist/src/utils/sessionVariables.d.ts.map +1 -0
- package/dist/src/utils/sessionVariables.test.d.ts +2 -0
- package/dist/src/utils/sessionVariables.test.d.ts.map +1 -0
- package/dist/src/utils/writeSchema.d.ts +3 -0
- package/dist/src/utils/writeSchema.d.ts.map +1 -0
- package/dist/standalone.d.ts +3 -0
- package/dist/standalone.d.ts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +119 -0
- package/playgrounds/console/index.html +25 -0
- package/playgrounds/graphiql/index.html +1144 -0
- package/playgrounds/scalar/index.html +19 -0
- package/src/ai/agent/agent.ts +222 -0
- package/src/ai/agent/index.ts +2 -0
- package/src/ai/agent/providers/anthropic.ts +113 -0
- package/src/ai/agent/providers/index.ts +62 -0
- package/src/ai/agent/providers/ollama.ts +128 -0
- package/src/ai/agent/providers/openai.ts +100 -0
- package/src/ai/agent/types.ts +65 -0
- package/src/ai/index.ts +8 -0
- package/src/ai/mcp/create-server.ts +444 -0
- package/src/ai/mcp/index.ts +80 -0
- package/src/ai/mcp/instructions.ts +103 -0
- package/src/ai/tools/agent.ts +111 -0
- package/src/ai/tools/core.ts +486 -0
- package/src/ai/tools/query-data.ts +109 -0
- package/src/analyzeQuery/analyzers/fragmentAnalyzer.ts +29 -0
- package/src/analyzeQuery/analyzers/operationAnalyzer.ts +50 -0
- package/src/analyzeQuery/analyzers/selectionAnalyzer.ts +135 -0
- package/src/analyzeQuery/analyzers/variableAnalyzer.ts +17 -0
- package/src/analyzeQuery/depthLimit.ts +124 -0
- package/src/analyzeQuery/directiveUtils.ts +99 -0
- package/src/analyzeQuery/index.ts +66 -0
- package/src/analyzeQuery/resolveVariables.ts +394 -0
- package/src/analyzeQuery/typeUtils.ts +47 -0
- package/src/analyzeQuery/types.ts +57 -0
- package/src/analyzeQuery/valueExtractors.ts +96 -0
- package/src/authentication/duration.ts +50 -0
- package/src/authentication/index.ts +34 -0
- package/src/authentication/jwt.ts +220 -0
- package/src/authentication/paseto.ts +256 -0
- package/src/authentication/tokenRepository.ts +75 -0
- package/src/authentication/types.ts +78 -0
- package/src/cli/seedAuth.ts +51 -0
- package/src/cli/seedAuthArgs.ts +50 -0
- package/src/config/helpers/cronHelper.ts +29 -0
- package/src/config/helpers/index.ts +8 -0
- package/src/config/helpers/operationHelper.ts +146 -0
- package/src/config/helpers/queueHelper.ts +27 -0
- package/src/config/index.ts +315 -0
- package/src/config/types/ai.ts +35 -0
- package/src/config/types/auth.ts +147 -0
- package/src/config/types/configuration.ts +69 -0
- package/src/config/types/cron.ts +140 -0
- package/src/config/types/db.ts +256 -0
- package/src/config/types/index.ts +129 -0
- package/src/config/types/operation.ts +253 -0
- package/src/config/types/queue.ts +172 -0
- package/src/config/types/remote-rest.ts +28 -0
- package/src/config/types/remote-schema.ts +39 -0
- package/src/config/types/virtual-columns.ts +157 -0
- package/src/configuration/getSchemas/index.ts +82 -0
- package/src/configuration/getSchemas/mergeEntities/index.ts +221 -0
- package/src/configuration/getSchemas/type-definition-generator/index.ts +511 -0
- package/src/configuration/gql/buildExecute.ts +54 -0
- package/src/configuration/gql/gqlAuthOperations.ts +150 -0
- package/src/configuration/gql/handleGraphQLRequestFactory.ts +415 -0
- package/src/configuration/gql/handleGraphQLSubscriptionFactory.ts +249 -0
- package/src/configuration/index.ts +138 -0
- package/src/configuration/rest/generateOpenAPI.ts +361 -0
- package/src/configuration/rest/handleRESTRequestFactory.ts +335 -0
- package/src/configuration/rest/index.ts +167 -0
- package/src/configuration/rest/openApiErrors.ts +106 -0
- package/src/console/api.ts +270 -0
- package/src/cron/index.ts +251 -0
- package/src/databases/auth/auth-operations.ts +36 -0
- package/src/databases/auth/password.ts +4 -0
- package/src/databases/common.ts +642 -0
- package/src/databases/core/executor.ts +97 -0
- package/src/databases/core/function-mapping.ts +176 -0
- package/src/databases/core/identifier.ts +8 -0
- package/src/databases/core/query-builder.ts +47 -0
- package/src/databases/directives.ts +112 -0
- package/src/databases/engines/mssql/auth.ts +112 -0
- package/src/databases/engines/mssql/connection.ts +151 -0
- package/src/databases/engines/mssql/format.ts +23 -0
- package/src/databases/engines/mssql/getStructure.ts +112 -0
- package/src/databases/engines/mssql/getViews.ts +17 -0
- package/src/databases/engines/mssql/query/index.ts +306 -0
- package/src/databases/engines/mysql/auth.ts +100 -0
- package/src/databases/engines/mysql/connection.ts +100 -0
- package/src/databases/engines/mysql/format.ts +23 -0
- package/src/databases/engines/mysql/getStructure.ts +116 -0
- package/src/databases/engines/mysql/getViews.ts +20 -0
- package/src/databases/engines/mysql/query/index.ts +291 -0
- package/src/databases/engines/postgresql/auth.ts +98 -0
- package/src/databases/engines/postgresql/connection.ts +99 -0
- package/src/databases/engines/postgresql/format.ts +23 -0
- package/src/databases/engines/postgresql/getStructure.ts +149 -0
- package/src/databases/engines/postgresql/getViews.ts +20 -0
- package/src/databases/engines/postgresql/query/index.ts +291 -0
- package/src/databases/engines/shared/claims.ts +26 -0
- package/src/databases/engines/shared/types.ts +7 -0
- package/src/databases/high-level-operations.ts +272 -0
- package/src/databases/index.ts +28 -0
- package/src/databases/metadata/structure.ts +12 -0
- package/src/databases/schemaBuilder/convert.ts +121 -0
- package/src/databases/schemaBuilder/generateCreateTable/index.ts +133 -0
- package/src/databases/schemaBuilder/generateCreateView/index.ts +101 -0
- package/src/databases/schemaBuilder/generateInsert/index.ts +47 -0
- package/src/databases/schemaBuilder/generateSchemaCreation/index.ts +29 -0
- package/src/databases/schemaBuilder/mssqlToPostgres/convertView.ts +47 -0
- package/src/databases/schemaBuilder/utils.ts +17 -0
- package/src/databases/sqlTypeUtils.ts +91 -0
- package/src/databases/transformers/data-transformers.ts +128 -0
- package/src/databases/transformers/genResolverName.ts +18 -0
- package/src/index.ts +461 -0
- package/src/logging/index.ts +69 -0
- package/src/queues/kafka.ts +409 -0
- package/src/queues/rabbitmq.ts +331 -0
- package/src/remoteREST/index.ts +46 -0
- package/src/remoteREST/parse.ts +68 -0
- package/src/remoteREST/proxy.ts +92 -0
- package/src/remoteREST/transform.ts +121 -0
- package/src/remoteREST/types.ts +38 -0
- package/src/remoteSchemas/index.ts +48 -0
- package/src/remoteSchemas/introspect.ts +44 -0
- package/src/remoteSchemas/proxy.ts +146 -0
- package/src/remoteSchemas/transform.ts +210 -0
- package/src/remoteSchemas/types.ts +43 -0
- package/src/singletons/ai.ts +91 -0
- package/src/singletons/authentication.ts +13 -0
- package/src/singletons/cache/index.ts +4 -0
- package/src/singletons/cache/lruCacheStore.ts +34 -0
- package/src/singletons/cache/redisCacheStore.ts +81 -0
- package/src/singletons/cache/redisClient.ts +12 -0
- package/src/singletons/cache/registry.ts +51 -0
- package/src/singletons/cache/types.ts +7 -0
- package/src/singletons/cron.ts +21 -0
- package/src/singletons/databases.ts +68 -0
- package/src/singletons/env.ts +3 -0
- package/src/singletons/queues.ts +101 -0
- package/src/subscriptions/index.ts +20 -0
- package/src/subscriptions/strategies/database.ts +68 -0
- package/src/subscriptions/strategies/index.ts +38 -0
- package/src/subscriptions/strategies/queue.ts +61 -0
- package/src/subscriptions/types.ts +74 -0
- package/src/subscriptions/utils/polling.ts +119 -0
- package/src/types/common.ts +57 -0
- package/src/types/configuration.ts +52 -0
- package/src/types/db.ts +87 -0
- package/src/types/env.ts +137 -0
- package/src/types/resolver.ts +181 -0
- package/src/types/zod/ai.ts +8 -0
- package/src/types/zod/auth.ts +96 -0
- package/src/types/zod/configuration.ts +46 -0
- package/src/types/zod/cron.ts +35 -0
- package/src/types/zod/db.ts +146 -0
- package/src/types/zod/mcp.ts +8 -0
- package/src/types/zod/operation.ts +37 -0
- package/src/types/zod/queue.ts +182 -0
- package/src/types/zod/remoteREST.ts +8 -0
- package/src/types/zod/remoteSchema.ts +11 -0
- package/src/utils/benchmark.ts +13 -0
- package/src/utils/event-emitter.ts +217 -0
- package/src/utils/responses.ts +42 -0
- package/src/utils/safeJSONParse.ts +10 -0
- package/src/utils/selection.ts +35 -0
- package/src/utils/sessionVariables.ts +131 -0
- package/src/utils/writeSchema.ts +13 -0
- package/standalone.ts +32 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { Message, Tool, ToolDefinition } from "./types";
|
|
3
|
+
import { getProvider } from "./providers";
|
|
4
|
+
import { logger } from "../../logging";
|
|
5
|
+
|
|
6
|
+
const MAX_ITERATIONS = 10;
|
|
7
|
+
|
|
8
|
+
/** Strip null values from tool-call arguments — LLMs pass `null` for optional fields instead of omitting them. */
|
|
9
|
+
function stripNulls(args: Record<string, unknown>): Record<string, unknown> {
|
|
10
|
+
for (const key of Object.keys(args)) {
|
|
11
|
+
if (args[key] === null) delete args[key];
|
|
12
|
+
}
|
|
13
|
+
return args;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Minimal view of a Zod 4 internal def for schema introspection. */
|
|
17
|
+
type ZodDef = { type: string; innerType?: ZodLike; shape?: Record<string, ZodLike> };
|
|
18
|
+
type ZodLike = { def?: ZodDef };
|
|
19
|
+
|
|
20
|
+
/** Unwrap optional/default/nullable to the base Zod type. */
|
|
21
|
+
function baseType(field: ZodLike): ZodLike {
|
|
22
|
+
let cur = field;
|
|
23
|
+
while (
|
|
24
|
+
cur.def &&
|
|
25
|
+
["optional", "default", "nullable"].includes(cur.def.type) &&
|
|
26
|
+
cur.def.innerType
|
|
27
|
+
) {
|
|
28
|
+
cur = cur.def.innerType;
|
|
29
|
+
}
|
|
30
|
+
return cur;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* JSON.parse string args whose schema target is a structured/scalar type.
|
|
35
|
+
* Small LLMs send `'["a"]'` or `'true'` as strings; parse before Zod validation.
|
|
36
|
+
*/
|
|
37
|
+
function coerceJsonStrings(
|
|
38
|
+
schema: z.ZodTypeAny,
|
|
39
|
+
args: Record<string, unknown>,
|
|
40
|
+
): Record<string, unknown> {
|
|
41
|
+
const shape = (schema as unknown as ZodLike).def?.shape;
|
|
42
|
+
if (!shape) return args;
|
|
43
|
+
for (const key of Object.keys(args)) {
|
|
44
|
+
if (typeof args[key] !== "string" || !shape[key]) continue;
|
|
45
|
+
const t = baseType(shape[key]).def?.type;
|
|
46
|
+
if (t && ["array", "object", "record", "number", "boolean"].includes(t)) {
|
|
47
|
+
try {
|
|
48
|
+
args[key] = JSON.parse(args[key] as string);
|
|
49
|
+
} catch {
|
|
50
|
+
/* let Zod reject */
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return args;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Convert a Zod-based Tool to an OpenAI-compatible ToolDefinition. */
|
|
58
|
+
function toToolDefinition(tool: Tool): ToolDefinition {
|
|
59
|
+
const jsonSchema = z.toJSONSchema(tool.schema);
|
|
60
|
+
return {
|
|
61
|
+
type: "function",
|
|
62
|
+
function: {
|
|
63
|
+
name: tool.name,
|
|
64
|
+
description: tool.description,
|
|
65
|
+
parameters: jsonSchema as ToolDefinition["function"]["parameters"],
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ---- Factory ----
|
|
71
|
+
|
|
72
|
+
/** Pre-bound agent config. Use with {@link createAgent}. */
|
|
73
|
+
export interface AgentConfig {
|
|
74
|
+
tools: Tool[];
|
|
75
|
+
systemPrompt: string;
|
|
76
|
+
/** Wraps the raw user prompt into the final user message sent to the LLM. */
|
|
77
|
+
wrap: (content: string) => string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Create a pre-configured agent function. Bind tools, system prompt, and prompt
|
|
82
|
+
* wrapper once — call with just a prompt string any number of times.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* const ask = createAgent({
|
|
86
|
+
* tools,
|
|
87
|
+
* systemPrompt: "You are a database assistant...",
|
|
88
|
+
* wrap: (prompt) => `Database-query from user:\n> ${prompt}`,
|
|
89
|
+
* });
|
|
90
|
+
* const answer = await ask("list all contacts grouped by role");
|
|
91
|
+
*/
|
|
92
|
+
export function createAgent(config: AgentConfig): (prompt: string) => Promise<string> {
|
|
93
|
+
return (prompt: string) => ask(prompt, config.tools, config.systemPrompt, config.wrap);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ---- Core agent loop ----
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Send a prompt to the LLM and return ONLY the final text answer.
|
|
100
|
+
* Handles tool-calling loops internally — the caller never sees tool calls.
|
|
101
|
+
*
|
|
102
|
+
* Use {@link createAgent} for a pre-configured single-arg version.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* const answer = await ask(
|
|
106
|
+
* "list all contacts grouped by role",
|
|
107
|
+
* tools,
|
|
108
|
+
* "You are a database assistant...",
|
|
109
|
+
* (prompt) => `Database-query from user:\n> ${prompt}`,
|
|
110
|
+
* );
|
|
111
|
+
*/
|
|
112
|
+
export async function ask(
|
|
113
|
+
prompt: string,
|
|
114
|
+
tools: Tool[],
|
|
115
|
+
systemPrompt: string,
|
|
116
|
+
wrap: (content: string) => string,
|
|
117
|
+
): Promise<string> {
|
|
118
|
+
const messages: Message[] = [
|
|
119
|
+
{ role: "system", content: systemPrompt },
|
|
120
|
+
{ role: "user", content: wrap(prompt) },
|
|
121
|
+
];
|
|
122
|
+
|
|
123
|
+
const provider = await getProvider();
|
|
124
|
+
const toolDefs: ToolDefinition[] = tools.map(toToolDefinition);
|
|
125
|
+
const log = logger("ai-agent");
|
|
126
|
+
|
|
127
|
+
for (let i = 0; i < MAX_ITERATIONS; i++) {
|
|
128
|
+
log.debug({ iteration: i + 1 }, "agent iteration");
|
|
129
|
+
|
|
130
|
+
const { content, toolCalls } = await provider.chat(messages, toolDefs);
|
|
131
|
+
|
|
132
|
+
// If the model returned tool calls, execute them and feed results back
|
|
133
|
+
if (toolCalls.length > 0) {
|
|
134
|
+
log.debug({ toolCallCount: toolCalls.length }, "model requested tool calls");
|
|
135
|
+
|
|
136
|
+
// Append assistant message (with tool_calls) to history
|
|
137
|
+
messages.push({
|
|
138
|
+
role: "assistant",
|
|
139
|
+
content,
|
|
140
|
+
tool_calls: toolCalls,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// Execute each tool and append results
|
|
144
|
+
for (const tc of toolCalls) {
|
|
145
|
+
const toolName = tc.function.name;
|
|
146
|
+
const rawArgs = tc.function.arguments;
|
|
147
|
+
|
|
148
|
+
log.debug({ tool: toolName, args: rawArgs }, "calling tool");
|
|
149
|
+
|
|
150
|
+
const tool = tools.find((t) => t.name === toolName);
|
|
151
|
+
if (!tool) {
|
|
152
|
+
log.warn({ tool: toolName }, "unknown tool requested by model");
|
|
153
|
+
messages.push({
|
|
154
|
+
role: "tool",
|
|
155
|
+
content: JSON.stringify({
|
|
156
|
+
error: `Unknown tool: ${toolName}`,
|
|
157
|
+
}),
|
|
158
|
+
tool_name: toolName,
|
|
159
|
+
tool_call_id: tc.id,
|
|
160
|
+
});
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
// Strip nulls, coerce stringified JSON (LLM robustness), then validate via Zod
|
|
166
|
+
const cleanedArgs = coerceJsonStrings(tool.schema, stripNulls(rawArgs));
|
|
167
|
+
const parsedArgs = tool.schema.parse(cleanedArgs);
|
|
168
|
+
const result = await tool.execute(parsedArgs);
|
|
169
|
+
const resultStr = JSON.stringify(result);
|
|
170
|
+
|
|
171
|
+
log.debug({ tool: toolName, resultLength: resultStr.length }, "tool returned");
|
|
172
|
+
|
|
173
|
+
messages.push({
|
|
174
|
+
role: "tool",
|
|
175
|
+
content: resultStr,
|
|
176
|
+
tool_name: toolName,
|
|
177
|
+
tool_call_id: tc.id,
|
|
178
|
+
});
|
|
179
|
+
} catch (err) {
|
|
180
|
+
log.warn({ tool: toolName, err }, "tool execution failed");
|
|
181
|
+
messages.push({
|
|
182
|
+
role: "tool",
|
|
183
|
+
content: JSON.stringify({
|
|
184
|
+
error: err instanceof Error ? err.message : String(err),
|
|
185
|
+
}),
|
|
186
|
+
tool_name: toolName,
|
|
187
|
+
tool_call_id: tc.id,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Continue loop — feed tool results back to model
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// No tool calls — this is the final answer
|
|
197
|
+
// Guard: if model never called query_data or graphql_execute, it's likely hallucinating
|
|
198
|
+
const calledTools = messages
|
|
199
|
+
.filter((m) => m.role === "assistant" && m.tool_calls)
|
|
200
|
+
.flatMap((m) => m.tool_calls!.map((tc) => tc.function.name));
|
|
201
|
+
|
|
202
|
+
const hasDescribed = calledTools.includes("describe_entity");
|
|
203
|
+
const hasQueried = calledTools.includes("query_data");
|
|
204
|
+
const hasGraphql = calledTools.includes("graphql_execute");
|
|
205
|
+
|
|
206
|
+
if (!hasQueried && !hasGraphql) {
|
|
207
|
+
log.warn("model attempted answer without querying data, nudging");
|
|
208
|
+
messages.push({
|
|
209
|
+
role: "user",
|
|
210
|
+
content: hasDescribed
|
|
211
|
+
? "STOP — you haven't queried the data yet. Call query_data with the entity and columns from describe_entity. Use operation 'aggregate' with groupBy for grouping. Do NOT fabricate data."
|
|
212
|
+
: "STOP — you skipped steps. Call describe_entity to get the table schema, then query_data to run the query. Do NOT fabricate data.",
|
|
213
|
+
});
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
log.debug({ answerLength: content.length }, "model answered directly");
|
|
218
|
+
return content;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
throw new Error(`Agent loop exceeded ${MAX_ITERATIONS} iterations without a final answer.`);
|
|
222
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
2
|
+
import type { ChatResult, Message, Provider, ToolCall, ToolDefinition } from "../types";
|
|
3
|
+
|
|
4
|
+
export interface AnthropicConfig {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
model: string;
|
|
7
|
+
maxTokens?: number;
|
|
8
|
+
temperature?: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function toAnthropicTool(t: ToolDefinition): Anthropic.Tool {
|
|
12
|
+
return {
|
|
13
|
+
name: t.function.name,
|
|
14
|
+
description: t.function.description,
|
|
15
|
+
input_schema: t.function.parameters as Anthropic.Tool["input_schema"],
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Serialize normalized history → Anthropic's wire shape.
|
|
21
|
+
* - `system` messages are extracted to the top-level `system` param.
|
|
22
|
+
* - consecutive `tool` results are coalesced into a single user message of
|
|
23
|
+
* tool_result blocks (Anthropic requires them grouped after the assistant turn).
|
|
24
|
+
*/
|
|
25
|
+
function toAnthropicMessages(messages: Message[]): {
|
|
26
|
+
system: string | undefined;
|
|
27
|
+
anthropicMessages: Anthropic.MessageParam[];
|
|
28
|
+
} {
|
|
29
|
+
let system: string | undefined;
|
|
30
|
+
const out: Anthropic.MessageParam[] = [];
|
|
31
|
+
|
|
32
|
+
for (const m of messages) {
|
|
33
|
+
if (m.role === "system") {
|
|
34
|
+
system = system ? `${system}\n\n${m.content}` : m.content;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (m.role === "user") {
|
|
39
|
+
out.push({ role: "user", content: m.content });
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (m.role === "assistant") {
|
|
44
|
+
const blocks: Array<Anthropic.TextBlockParam | Anthropic.ToolUseBlockParam> = [];
|
|
45
|
+
if (m.content) blocks.push({ type: "text", text: m.content });
|
|
46
|
+
for (const tc of m.tool_calls ?? []) {
|
|
47
|
+
blocks.push({
|
|
48
|
+
type: "tool_use",
|
|
49
|
+
id: tc.id,
|
|
50
|
+
name: tc.function.name,
|
|
51
|
+
input: tc.function.arguments,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
out.push({ role: "assistant", content: blocks });
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// role === "tool" → tool_result block, grouped into a trailing user message.
|
|
59
|
+
const block: Anthropic.ToolResultBlockParam = {
|
|
60
|
+
type: "tool_result",
|
|
61
|
+
tool_use_id: m.tool_call_id ?? "",
|
|
62
|
+
content: m.content,
|
|
63
|
+
};
|
|
64
|
+
const last = out[out.length - 1];
|
|
65
|
+
if (last && last.role === "user" && Array.isArray(last.content)) {
|
|
66
|
+
last.content.push(block);
|
|
67
|
+
} else {
|
|
68
|
+
out.push({ role: "user", content: [block] });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return { system, anthropicMessages: out };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Provider for Anthropic's Messages API (Claude). */
|
|
76
|
+
export function makeAnthropic(cfg: AnthropicConfig): Provider {
|
|
77
|
+
const client = new Anthropic({ apiKey: cfg.apiKey });
|
|
78
|
+
const maxTokens = cfg.maxTokens ?? 4096;
|
|
79
|
+
const temperature = cfg.temperature ?? 0;
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
async chat(messages: Message[], tools: ToolDefinition[]): Promise<ChatResult> {
|
|
83
|
+
const { system, anthropicMessages } = toAnthropicMessages(messages);
|
|
84
|
+
|
|
85
|
+
const res = await client.messages.create({
|
|
86
|
+
model: cfg.model,
|
|
87
|
+
max_tokens: maxTokens,
|
|
88
|
+
temperature,
|
|
89
|
+
system,
|
|
90
|
+
tools: tools.map(toAnthropicTool),
|
|
91
|
+
messages: anthropicMessages,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
let content = "";
|
|
95
|
+
const toolCalls: ToolCall[] = [];
|
|
96
|
+
for (const block of res.content) {
|
|
97
|
+
if (block.type === "text") {
|
|
98
|
+
content += block.text;
|
|
99
|
+
} else if (block.type === "tool_use") {
|
|
100
|
+
toolCalls.push({
|
|
101
|
+
id: block.id,
|
|
102
|
+
function: {
|
|
103
|
+
name: block.name,
|
|
104
|
+
arguments: (block.input ?? {}) as Record<string, unknown>,
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return { content, toolCalls };
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Provider } from "../types";
|
|
2
|
+
|
|
3
|
+
function requireEnv(key: string): string {
|
|
4
|
+
const value = process.env[key];
|
|
5
|
+
if (!value) {
|
|
6
|
+
throw new Error(`${key} is required for LLM_PROVIDER="${process.env.LLM_PROVIDER}".`);
|
|
7
|
+
}
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// Provider registry. Each factory dynamically imports its module so the
|
|
12
|
+
// provider SDK is loaded into memory ONLY when that provider is selected.
|
|
13
|
+
// Add OpenAI-compatible backends (Groq, Mistral, Together, …) as one more
|
|
14
|
+
// entry pointing makeOpenAICompatible at their baseURL.
|
|
15
|
+
const REGISTRY: Record<string, () => Promise<Provider>> = {
|
|
16
|
+
ollama: async () => {
|
|
17
|
+
const { ollamaProvider } = await import("./ollama");
|
|
18
|
+
return ollamaProvider;
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
openai: async () => {
|
|
22
|
+
const { makeOpenAICompatible } = await import("./openai");
|
|
23
|
+
return makeOpenAICompatible({
|
|
24
|
+
apiKey: requireEnv("OPENAI_API_KEY"),
|
|
25
|
+
baseURL: process.env.OPENAI_BASE_URL, // undefined → SDK default (api.openai.com)
|
|
26
|
+
model: process.env.LLM_MODEL ?? "gpt-4o-mini",
|
|
27
|
+
});
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
deepseek: async () => {
|
|
31
|
+
const { makeOpenAICompatible } = await import("./openai");
|
|
32
|
+
return makeOpenAICompatible({
|
|
33
|
+
apiKey: requireEnv("DEEPSEEK_API_KEY"),
|
|
34
|
+
baseURL: "https://api.deepseek.com",
|
|
35
|
+
model: process.env.LLM_MODEL ?? "deepseek-chat",
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
anthropic: async () => {
|
|
40
|
+
const { makeAnthropic } = await import("./anthropic");
|
|
41
|
+
return makeAnthropic({
|
|
42
|
+
apiKey: requireEnv("ANTHROPIC_API_KEY"),
|
|
43
|
+
model: process.env.LLM_MODEL ?? "claude-haiku-4-5-20251001",
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Resolve the active LLM provider from `LLM_PROVIDER` (default "ollama").
|
|
50
|
+
* The provider module (and its SDK) is dynamically imported on demand, so
|
|
51
|
+
* unused provider SDKs never load. Each provider reads its own env vars lazily.
|
|
52
|
+
*/
|
|
53
|
+
export function getProvider(): Promise<Provider> {
|
|
54
|
+
const name = (process.env.LLM_PROVIDER ?? "ollama").toLowerCase();
|
|
55
|
+
const factory = REGISTRY[name];
|
|
56
|
+
if (!factory) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`Unknown LLM_PROVIDER "${name}". Valid options: ${Object.keys(REGISTRY).join(", ")}.`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return factory();
|
|
62
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { ChatResult, Message, Provider, ToolDefinition } from "../types";
|
|
2
|
+
|
|
3
|
+
const OLLAMA_HOST = process.env.OLLAMA_HOST ?? "http://localhost:11434";
|
|
4
|
+
// LLM_MODEL is the unified knob; OLLAMA_MODEL kept for backward compatibility.
|
|
5
|
+
const OLLAMA_MODEL = process.env.LLM_MODEL ?? process.env.OLLAMA_MODEL ?? "qwen3.5:2b";
|
|
6
|
+
|
|
7
|
+
export const config = {
|
|
8
|
+
host: OLLAMA_HOST,
|
|
9
|
+
model: OLLAMA_MODEL,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// ---- Per-model presets for optimal accuracy ----
|
|
13
|
+
// Keyed by model family prefix (matched case-insensitive).
|
|
14
|
+
|
|
15
|
+
interface ModelPreset {
|
|
16
|
+
temperature: number;
|
|
17
|
+
top_p?: number;
|
|
18
|
+
top_k?: number;
|
|
19
|
+
num_predict?: number;
|
|
20
|
+
seed?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const PRESETS: Record<string, ModelPreset> = {
|
|
24
|
+
// Google Gemma 4 — recommended sampling params from official docs
|
|
25
|
+
gemma4: { temperature: 1.0, top_p: 0.95, top_k: 64 },
|
|
26
|
+
|
|
27
|
+
// Meta Llama 3.x — temp 0 kills hallucination on small models
|
|
28
|
+
llama3: { temperature: 0, num_predict: 2048 },
|
|
29
|
+
|
|
30
|
+
// Alibaba Qwen 3.x — low temp for deterministic tool use
|
|
31
|
+
qwen3: { temperature: 0.1, top_p: 0.9 },
|
|
32
|
+
|
|
33
|
+
// IBM Granite 4 — conservative sampling
|
|
34
|
+
granite4: { temperature: 0.1 },
|
|
35
|
+
|
|
36
|
+
// Google FunctionGemma — tiny, keep deterministic
|
|
37
|
+
functiongemma: { temperature: 0 },
|
|
38
|
+
|
|
39
|
+
// Default fallback — safe for unknown models
|
|
40
|
+
default: { temperature: 0, top_p: 0.9 },
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function resolvePreset(model: string): ModelPreset {
|
|
44
|
+
const key = model.toLowerCase();
|
|
45
|
+
for (const [prefix, preset] of Object.entries(PRESETS)) {
|
|
46
|
+
if (key.startsWith(prefix)) return preset;
|
|
47
|
+
}
|
|
48
|
+
return PRESETS.default!;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ---- Ollama wire format ----
|
|
52
|
+
|
|
53
|
+
interface OllamaToolCall {
|
|
54
|
+
function: { name: string; arguments: Record<string, unknown> };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface OllamaMessage {
|
|
58
|
+
role: "system" | "user" | "assistant" | "tool";
|
|
59
|
+
content: string;
|
|
60
|
+
tool_calls?: OllamaToolCall[];
|
|
61
|
+
tool_name?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface OllamaChatResponse {
|
|
65
|
+
message: { role: "assistant"; content: string; tool_calls?: OllamaToolCall[] };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Serialize normalized history → Ollama's wire shape (tool results keyed by tool_name). */
|
|
69
|
+
function toOllamaMessages(messages: Message[]): OllamaMessage[] {
|
|
70
|
+
return messages.map((m): OllamaMessage => {
|
|
71
|
+
if (m.role === "assistant" && m.tool_calls && m.tool_calls.length > 0) {
|
|
72
|
+
return {
|
|
73
|
+
role: "assistant",
|
|
74
|
+
content: m.content,
|
|
75
|
+
tool_calls: m.tool_calls.map((tc) => ({
|
|
76
|
+
function: { name: tc.function.name, arguments: tc.function.arguments },
|
|
77
|
+
})),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
if (m.role === "tool") {
|
|
81
|
+
return { role: "tool", content: m.content, tool_name: m.tool_name };
|
|
82
|
+
}
|
|
83
|
+
return { role: m.role, content: m.content };
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Native Ollama provider. Talks to /api/chat directly (no SDK) and keeps the
|
|
89
|
+
* per-model sampling presets. Default provider — runs local/free, no API key.
|
|
90
|
+
*/
|
|
91
|
+
export const ollamaProvider: Provider = {
|
|
92
|
+
async chat(messages: Message[], tools: ToolDefinition[]): Promise<ChatResult> {
|
|
93
|
+
const preset = resolvePreset(config.model);
|
|
94
|
+
|
|
95
|
+
const body = {
|
|
96
|
+
model: config.model,
|
|
97
|
+
messages: toOllamaMessages(messages),
|
|
98
|
+
tools,
|
|
99
|
+
stream: false,
|
|
100
|
+
options: {
|
|
101
|
+
temperature: preset.temperature,
|
|
102
|
+
top_p: preset.top_p,
|
|
103
|
+
top_k: preset.top_k,
|
|
104
|
+
num_predict: preset.num_predict,
|
|
105
|
+
seed: preset.seed,
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const res = await fetch(`${config.host}/api/chat`, {
|
|
110
|
+
method: "POST",
|
|
111
|
+
headers: { "Content-Type": "application/json" },
|
|
112
|
+
body: JSON.stringify(body),
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
if (!res.ok) {
|
|
116
|
+
const text = await res.text();
|
|
117
|
+
throw new Error(`Ollama API error ${res.status} (${res.statusText}): ${text}`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const data = (await res.json()) as OllamaChatResponse;
|
|
121
|
+
const toolCalls = (data.message.tool_calls ?? []).map((tc, i) => ({
|
|
122
|
+
id: `ollama-${i}`, // Ollama omits ids; synthesize one for correlation.
|
|
123
|
+
function: { name: tc.function.name, arguments: tc.function.arguments },
|
|
124
|
+
}));
|
|
125
|
+
|
|
126
|
+
return { content: data.message.content ?? "", toolCalls };
|
|
127
|
+
},
|
|
128
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
import type { ChatResult, Message, Provider, ToolDefinition } from "../types";
|
|
3
|
+
|
|
4
|
+
type OAIMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam;
|
|
5
|
+
type OAITool = OpenAI.Chat.Completions.ChatCompletionTool;
|
|
6
|
+
|
|
7
|
+
export interface OpenAICompatConfig {
|
|
8
|
+
apiKey: string;
|
|
9
|
+
model: string;
|
|
10
|
+
/** Override for OpenAI-compatible endpoints (DeepSeek, Groq, Mistral, …). */
|
|
11
|
+
baseURL?: string;
|
|
12
|
+
temperature?: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function parseArgs(raw: string): Record<string, unknown> {
|
|
16
|
+
if (!raw) return {};
|
|
17
|
+
try {
|
|
18
|
+
return JSON.parse(raw) as Record<string, unknown>;
|
|
19
|
+
} catch {
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Serialize normalized history → OpenAI Chat Completions messages. */
|
|
25
|
+
function toOpenAIMessages(messages: Message[]): OAIMessage[] {
|
|
26
|
+
return messages.map((m): OAIMessage => {
|
|
27
|
+
switch (m.role) {
|
|
28
|
+
case "system":
|
|
29
|
+
return { role: "system", content: m.content };
|
|
30
|
+
case "user":
|
|
31
|
+
return { role: "user", content: m.content };
|
|
32
|
+
case "assistant":
|
|
33
|
+
return m.tool_calls && m.tool_calls.length > 0
|
|
34
|
+
? {
|
|
35
|
+
role: "assistant",
|
|
36
|
+
content: m.content || null,
|
|
37
|
+
tool_calls: m.tool_calls.map((tc) => ({
|
|
38
|
+
id: tc.id,
|
|
39
|
+
type: "function",
|
|
40
|
+
function: {
|
|
41
|
+
name: tc.function.name,
|
|
42
|
+
arguments: JSON.stringify(tc.function.arguments),
|
|
43
|
+
},
|
|
44
|
+
})),
|
|
45
|
+
}
|
|
46
|
+
: { role: "assistant", content: m.content };
|
|
47
|
+
case "tool":
|
|
48
|
+
return { role: "tool", tool_call_id: m.tool_call_id ?? "", content: m.content };
|
|
49
|
+
default: {
|
|
50
|
+
const _exhaustive: never = m.role;
|
|
51
|
+
throw new Error(`Unhandled message role: ${String(_exhaustive)}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function toOpenAITools(tools: ToolDefinition[]): OAITool[] {
|
|
58
|
+
return tools.map((t) => ({
|
|
59
|
+
type: "function",
|
|
60
|
+
function: {
|
|
61
|
+
name: t.function.name,
|
|
62
|
+
description: t.function.description,
|
|
63
|
+
parameters: t.function.parameters,
|
|
64
|
+
},
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Provider for any OpenAI-compatible Chat Completions endpoint.
|
|
70
|
+
* Used for OpenAI, DeepSeek, and other compatible servers via `baseURL`.
|
|
71
|
+
*/
|
|
72
|
+
export function makeOpenAICompatible(cfg: OpenAICompatConfig): Provider {
|
|
73
|
+
const client = new OpenAI({ apiKey: cfg.apiKey, baseURL: cfg.baseURL });
|
|
74
|
+
const temperature = cfg.temperature ?? 0;
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
async chat(messages: Message[], tools: ToolDefinition[]): Promise<ChatResult> {
|
|
78
|
+
const res = await client.chat.completions.create({
|
|
79
|
+
model: cfg.model,
|
|
80
|
+
messages: toOpenAIMessages(messages),
|
|
81
|
+
tools: toOpenAITools(tools),
|
|
82
|
+
temperature,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const msg = res.choices[0]?.message;
|
|
86
|
+
const toolCalls = (msg?.tool_calls ?? []).flatMap((tc) =>
|
|
87
|
+
tc.type === "function"
|
|
88
|
+
? [
|
|
89
|
+
{
|
|
90
|
+
id: tc.id,
|
|
91
|
+
function: { name: tc.function.name, arguments: parseArgs(tc.function.arguments) },
|
|
92
|
+
},
|
|
93
|
+
]
|
|
94
|
+
: [],
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
return { content: msg?.content ?? "", toolCalls };
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// ---- Tool definitions (OpenAI-compatible JSON Schema) ----
|
|
2
|
+
|
|
3
|
+
import type { z } from "zod";
|
|
4
|
+
|
|
5
|
+
export interface ToolFunction {
|
|
6
|
+
name: string;
|
|
7
|
+
description: string;
|
|
8
|
+
parameters: {
|
|
9
|
+
type: "object";
|
|
10
|
+
properties: Record<string, unknown>;
|
|
11
|
+
required?: string[];
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ToolDefinition {
|
|
16
|
+
type: "function";
|
|
17
|
+
function: ToolFunction;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// ---- Unified tool (Zod schema + executor in one place) ----
|
|
21
|
+
|
|
22
|
+
export interface Tool<TSchema extends z.ZodTypeAny = z.ZodTypeAny> {
|
|
23
|
+
name: string;
|
|
24
|
+
description: string;
|
|
25
|
+
schema: TSchema;
|
|
26
|
+
execute: (args: z.infer<TSchema>) => Promise<unknown>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ---- Normalized messages (provider-agnostic) ----
|
|
30
|
+
|
|
31
|
+
export interface ToolCall {
|
|
32
|
+
/** Correlation id. Synthesized for providers that omit one (Ollama). */
|
|
33
|
+
id: string;
|
|
34
|
+
function: {
|
|
35
|
+
name: string;
|
|
36
|
+
arguments: Record<string, unknown>;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface Message {
|
|
41
|
+
role: "system" | "user" | "assistant" | "tool";
|
|
42
|
+
content: string;
|
|
43
|
+
tool_calls?: ToolCall[];
|
|
44
|
+
/** Tool-result correlation id (OpenAI tool_call_id / Anthropic tool_use_id). */
|
|
45
|
+
tool_call_id?: string;
|
|
46
|
+
/** Tool name for Ollama tool-result messages. */
|
|
47
|
+
tool_name?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ---- Provider abstraction ----
|
|
51
|
+
|
|
52
|
+
/** Normalized result of one chat turn, independent of provider wire format. */
|
|
53
|
+
export interface ChatResult {
|
|
54
|
+
content: string;
|
|
55
|
+
toolCalls: ToolCall[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A chat backend. Each implementation translates the normalized message/tool
|
|
60
|
+
* history to and from its provider's wire format. Stateless: the full message
|
|
61
|
+
* history is passed on every call.
|
|
62
|
+
*/
|
|
63
|
+
export interface Provider {
|
|
64
|
+
chat(messages: Message[], tools: ToolDefinition[]): Promise<ChatResult>;
|
|
65
|
+
}
|