@m6d/cortex-server 1.8.0 → 2.0.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/README.md +8 -3
- package/contracts/README.md +23 -0
- package/contracts/graph/embed.ts +50 -0
- package/{src/lib → contracts}/graph/helpers.ts +4 -10
- package/{src/lib → contracts}/graph/neo4j.ts +14 -27
- package/contracts/graph/schema.ts +65 -0
- package/{src/lib → contracts}/graph/types.ts +2 -29
- package/contracts/graph.ts +36 -0
- package/contracts/runtime.ts +208 -0
- package/contracts/wire.ts +143 -0
- package/dist/contracts/graph/embed.d.ts +22 -0
- package/dist/{src/lib → contracts}/graph/helpers.d.ts +16 -14
- package/dist/{src/lib → contracts}/graph/neo4j.d.ts +5 -5
- package/dist/contracts/graph/schema.d.ts +62 -0
- package/dist/{src/lib → contracts}/graph/types.d.ts +1 -1
- package/dist/contracts/graph.d.ts +14 -0
- package/dist/contracts/runtime.d.ts +278 -0
- package/dist/contracts/wire.d.ts +120 -0
- package/dist/src/lib/adapters/database/index.d.ts +61 -0
- package/dist/src/lib/adapters/database/message-content.d.ts +30 -0
- package/dist/src/lib/adapters/database/mssql/attachments.d.ts +100 -0
- package/dist/src/lib/adapters/database/mssql/client.d.ts +8 -0
- package/dist/src/lib/adapters/database/mssql/index.d.ts +194 -0
- package/dist/src/lib/adapters/database/mssql/llm-requests.d.ts +17 -0
- package/dist/src/lib/adapters/database/mssql/messages.d.ts +37 -0
- package/dist/src/lib/adapters/database/mssql/threads.d.ts +50 -0
- package/dist/src/lib/adapters/database/postgres/attachments.d.ts +113 -0
- package/dist/src/lib/adapters/database/postgres/client.d.ts +8 -0
- package/dist/src/lib/adapters/database/postgres/index.d.ts +207 -0
- package/dist/src/lib/adapters/database/postgres/llm-requests.d.ts +17 -0
- package/dist/src/lib/adapters/database/postgres/messages.d.ts +37 -0
- package/dist/src/lib/adapters/database/postgres/threads.d.ts +50 -0
- package/{src/lib/adapters/storage.ts → dist/src/lib/adapters/storage/index.d.ts} +1 -1
- package/dist/src/lib/adapters/{minio.d.ts → storage/minio.d.ts} +1 -1
- package/dist/src/lib/ai/active-runs.d.ts +63 -0
- package/dist/src/lib/ai/attachments.d.ts +30 -0
- package/dist/src/lib/ai/backend-url.d.ts +16 -0
- package/dist/src/lib/ai/cc-runtime.d.ts +61 -0
- package/dist/src/lib/ai/commit-gate.d.ts +21 -0
- package/dist/src/lib/ai/context/builder.d.ts +6 -7
- package/dist/src/lib/ai/context/compression-middleware.d.ts +15 -0
- package/dist/src/lib/ai/context/compressor.d.ts +2 -13
- package/dist/src/lib/ai/context/intra-turn-compressor.d.ts +9 -9
- package/dist/src/lib/ai/context/optimize-thread-context.d.ts +9 -0
- package/dist/src/lib/ai/context/summarizer.d.ts +3 -3
- package/dist/src/lib/ai/context/token-estimator.d.ts +5 -8
- package/dist/src/lib/ai/fetch.d.ts +1 -1
- package/dist/src/lib/ai/finish-turn.d.ts +23 -0
- package/dist/src/lib/ai/helpers.d.ts +37 -4
- package/dist/src/lib/ai/index.d.ts +6 -3
- package/dist/src/lib/ai/inspector.d.ts +41 -0
- package/dist/src/lib/ai/interceptors/request-interceptor.d.ts +6 -5
- package/dist/src/lib/ai/prompt.d.ts +5 -4
- package/dist/src/lib/ai/redis-runs.d.ts +41 -0
- package/dist/src/lib/ai/redis-stream-log.d.ts +39 -0
- package/dist/src/lib/ai/tools/execute-code.tool.d.ts +18 -4
- package/dist/src/lib/ai/tools/query-graph.tool.d.ts +13 -5
- package/dist/src/lib/ai/tools/read-attachment.tool.d.ts +21 -0
- package/dist/src/lib/ai/tools/search-common.d.ts +17 -0
- package/dist/src/lib/ai/tools/search-knowledge.tool.d.ts +15 -0
- package/dist/src/lib/ai/tools/search-services.tool.d.ts +12 -0
- package/dist/src/lib/ai/tools/search-tools.tool.d.ts +12 -0
- package/dist/src/lib/ai/turn-tools.d.ts +39 -0
- package/dist/src/lib/ai/vision/rasterize-pdf.d.ts +26 -0
- package/dist/src/lib/ai/vision/rasterize-pdf.worker.d.ts +1 -0
- package/dist/src/lib/ai/vision/vision-reader.d.ts +45 -0
- package/dist/src/lib/cc/client.d.ts +183 -0
- package/dist/src/lib/cc/config-cache.d.ts +20 -0
- package/dist/src/lib/cc/format.d.ts +23 -0
- package/dist/src/lib/cc/registry.d.ts +34 -0
- package/dist/src/lib/cc/types.d.ts +27 -0
- package/dist/src/lib/config.d.ts +81 -64
- package/dist/src/lib/db/drizzle.config.mssql.d.ts +2 -0
- package/dist/src/lib/db/drizzle.config.pg.d.ts +2 -0
- package/dist/src/lib/db/migrate.d.ts +7 -1
- package/dist/src/lib/db/{schema.d.ts → schema.mssql.d.ts} +138 -29
- package/dist/src/lib/db/schema.pg.d.ts +546 -0
- package/dist/src/lib/factory.d.ts +2 -12
- package/dist/src/lib/graph/index.d.ts +10 -11
- package/dist/src/lib/graph/resolver.d.ts +2 -3
- package/dist/src/lib/index.d.ts +3 -3
- package/dist/src/lib/redis.d.ts +19 -0
- package/dist/src/lib/routes/chat.d.ts +1 -1
- package/dist/src/lib/routes/files.d.ts +1 -1
- package/dist/src/lib/routes/owned-thread.d.ts +17 -0
- package/dist/src/lib/routes/threads.d.ts +1 -1
- package/dist/src/lib/routes/ws.d.ts +1 -1
- package/dist/src/lib/types.d.ts +20 -37
- package/dist/src/lib/ws/connections.d.ts +2 -1
- package/package.json +33 -18
- package/src/lib/adapters/database/index.ts +78 -0
- package/src/lib/adapters/database/message-content.ts +39 -0
- package/src/lib/adapters/database/mssql/attachments.ts +90 -0
- package/src/lib/adapters/database/mssql/client.ts +11 -0
- package/src/lib/adapters/database/mssql/index.ts +23 -0
- package/src/lib/adapters/database/mssql/llm-requests.ts +43 -0
- package/src/lib/adapters/database/mssql/messages.ts +92 -0
- package/src/lib/adapters/database/mssql/threads.ts +83 -0
- package/src/lib/adapters/database/postgres/attachments.ts +88 -0
- package/src/lib/adapters/database/postgres/client.ts +11 -0
- package/src/lib/adapters/database/postgres/index.ts +23 -0
- package/src/lib/adapters/database/postgres/llm-requests.ts +45 -0
- package/src/lib/adapters/database/postgres/messages.ts +92 -0
- package/src/lib/adapters/database/postgres/threads.ts +84 -0
- package/{dist/src/lib/adapters/storage.d.ts → src/lib/adapters/storage/index.ts} +1 -1
- package/src/lib/adapters/{minio.ts → storage/minio.ts} +5 -5
- package/src/lib/ai/active-runs.ts +157 -0
- package/src/lib/ai/attachments.ts +144 -0
- package/src/lib/ai/backend-url.ts +27 -0
- package/src/lib/ai/cc-runtime.ts +63 -0
- package/src/lib/ai/commit-gate.ts +116 -0
- package/src/lib/ai/context/builder.ts +12 -14
- package/src/lib/ai/context/compression-middleware.ts +44 -0
- package/src/lib/ai/context/compressor.ts +67 -59
- package/src/lib/ai/context/intra-turn-compressor.ts +121 -204
- package/src/lib/ai/context/optimize-thread-context.ts +50 -0
- package/src/lib/ai/context/summarizer.ts +28 -30
- package/src/lib/ai/context/token-estimator.ts +35 -30
- package/src/lib/ai/fetch.ts +10 -5
- package/src/lib/ai/finish-turn.ts +75 -0
- package/src/lib/ai/helpers.ts +83 -15
- package/src/lib/ai/index.ts +359 -329
- package/src/lib/ai/inspector.ts +112 -0
- package/src/lib/ai/interceptors/request-interceptor.ts +70 -42
- package/src/lib/ai/prompt.ts +126 -125
- package/src/lib/ai/redis-runs.ts +167 -0
- package/src/lib/ai/redis-stream-log.ts +219 -0
- package/src/lib/ai/tools/execute-code.tool.ts +178 -67
- package/src/lib/ai/tools/query-graph.tool.ts +7 -8
- package/src/lib/ai/tools/read-attachment.tool.ts +59 -0
- package/src/lib/ai/tools/search-common.ts +15 -0
- package/src/lib/ai/tools/search-knowledge.tool.ts +33 -0
- package/src/lib/ai/tools/search-services.tool.ts +31 -0
- package/src/lib/ai/tools/search-tools.tool.ts +29 -0
- package/src/lib/ai/turn-tools.ts +154 -0
- package/src/lib/ai/vision/rasterize-pdf.ts +69 -0
- package/src/lib/ai/vision/rasterize-pdf.worker.ts +81 -0
- package/src/lib/ai/vision/vision-reader.ts +130 -0
- package/src/lib/auth/middleware.ts +5 -3
- package/src/lib/cc/client.ts +273 -0
- package/src/lib/cc/config-cache.ts +32 -0
- package/src/lib/cc/format.ts +96 -0
- package/src/lib/cc/registry.ts +62 -0
- package/src/lib/cc/types.ts +44 -0
- package/src/lib/config.ts +102 -71
- package/src/lib/db/drizzle.config.mssql.ts +9 -0
- package/src/lib/db/drizzle.config.pg.ts +9 -0
- package/src/lib/db/migrate.ts +45 -10
- package/src/lib/db/migrations/mssql/20260729092726_talented_wilson_fisk/migration.sql +20 -0
- package/src/lib/db/migrations/mssql/20260729092726_talented_wilson_fisk/snapshot.json +497 -0
- package/src/lib/db/migrations/mssql/20260801003652_perpetual_blue_shield/migration.sql +1 -0
- package/src/lib/db/migrations/mssql/20260801003652_perpetual_blue_shield/snapshot.json +516 -0
- package/src/lib/db/migrations/mssql/20260812174642_user_id_text/migration.sql +2 -0
- package/src/lib/db/migrations/mssql/20260812174642_user_id_text/snapshot.json +516 -0
- package/src/lib/db/migrations/pg/20260801084117_thick_ben_grimm/migration.sql +53 -0
- package/src/lib/db/migrations/pg/20260801084117_thick_ben_grimm/snapshot.json +584 -0
- package/src/lib/db/migrations/pg/20260812174622_user_id_text/migration.sql +2 -0
- package/src/lib/db/migrations/pg/20260812174622_user_id_text/snapshot.json +584 -0
- package/src/lib/db/{schema.ts → schema.mssql.ts} +29 -18
- package/src/lib/db/schema.pg.ts +95 -0
- package/src/lib/factory.ts +44 -89
- package/src/lib/graph/index.ts +12 -40
- package/src/lib/graph/resolver.ts +146 -116
- package/src/lib/index.ts +11 -4
- package/src/lib/redis.ts +44 -0
- package/src/lib/routes/chat.ts +91 -62
- package/src/lib/routes/files.ts +105 -83
- package/src/lib/routes/owned-thread.ts +17 -0
- package/src/lib/routes/threads.ts +14 -30
- package/src/lib/routes/ws.ts +9 -3
- package/src/lib/types.ts +37 -36
- package/src/lib/ws/connections.ts +9 -1
- package/tsconfig.json +48 -0
- package/dist/src/lib/adapters/database.d.ts +0 -45
- package/dist/src/lib/adapters/mssql.d.ts +0 -3
- package/dist/src/lib/ai/active-streams.d.ts +0 -14
- package/dist/src/lib/ai/context/index.d.ts +0 -16
- package/dist/src/lib/ai/tools/capture-files.tool.d.ts +0 -6
- package/dist/src/lib/cli/extract-endpoints.d.ts +0 -6
- package/dist/src/lib/graph/expand-domains.d.ts +0 -2
- package/dist/src/lib/graph/generate-cypher.d.ts +0 -22
- package/dist/src/lib/graph/seed.d.ts +0 -19
- package/dist/src/lib/graph/validate.d.ts +0 -2
- package/dist/src/lib/routes/index.d.ts +0 -4
- package/dist/src/lib/ws/events.d.ts +0 -39
- package/dist/src/lib/ws/index.d.ts +0 -3
- package/dist/src/lib/ws/notify.d.ts +0 -2
- package/src/lib/adapters/database.ts +0 -48
- package/src/lib/adapters/mssql.ts +0 -231
- package/src/lib/ai/active-streams.ts +0 -123
- package/src/lib/ai/context/index.ts +0 -80
- package/src/lib/ai/tools/capture-files.tool.ts +0 -20
- package/src/lib/cli/extract-endpoints.ts +0 -550
- package/src/lib/graph/expand-domains.ts +0 -276
- package/src/lib/graph/generate-cypher.ts +0 -192
- package/src/lib/graph/seed.ts +0 -162
- package/src/lib/graph/validate.ts +0 -78
- package/src/lib/routes/index.ts +0 -4
- package/src/lib/ws/events.ts +0 -39
- package/src/lib/ws/index.ts +0 -11
- package/src/lib/ws/notify.ts +0 -9
- package/src/sample/db/client.ts +0 -9
- package/src/sample/db/migrations/20260411023125_rich_purple_man/migration.sql +0 -5
- package/src/sample/db/migrations/20260411023125_rich_purple_man/snapshot.json +0 -50
- package/src/sample/db/schema.ts +0 -10
- package/src/sample/domains/account/concepts/session.concept.ts +0 -17
- package/src/sample/domains/account/endpoints/listCurrentSessions.endpoint.ts +0 -31
- package/src/sample/domains/account/endpoints/revokeAllOtherSessions.endpoint.ts +0 -22
- package/src/sample/domains/account/endpoints/revokeCurrentSession.endpoint.ts +0 -33
- package/src/sample/domains/account/index.ts +0 -18
- package/src/sample/domains/appraisals/concepts/appraisalCycle.concept.ts +0 -15
- package/src/sample/domains/appraisals/concepts/appraisalRecord.concept.ts +0 -20
- package/src/sample/domains/appraisals/concepts/appraisalRecordCompetency.concept.ts +0 -10
- package/src/sample/domains/appraisals/concepts/appraisalRecordFunctionalRequirement.concept.ts +0 -10
- package/src/sample/domains/appraisals/concepts/appraisalRecordGoal.concept.ts +0 -17
- package/src/sample/domains/appraisals/endpoints/getAppraisalRecordById.endpoint.ts +0 -321
- package/src/sample/domains/appraisals/endpoints/getAppraisalRecordCompetencies.endpoint.ts +0 -120
- package/src/sample/domains/appraisals/endpoints/getAppraisalRecordFunctionalRequirements.endpoint.ts +0 -126
- package/src/sample/domains/appraisals/endpoints/getAppraisalRecordGoals.endpoint.ts +0 -104
- package/src/sample/domains/appraisals/endpoints/getAppraisalRecordScore.endpoint.ts +0 -58
- package/src/sample/domains/appraisals/endpoints/listAppraisalCyclesSimple.endpoint.ts +0 -52
- package/src/sample/domains/appraisals/endpoints/listAppraisalRecords.endpoint.ts +0 -245
- package/src/sample/domains/appraisals/endpoints/listAppraisalTemplatesSimple.endpoint.ts +0 -52
- package/src/sample/domains/appraisals/index.ts +0 -40
- package/src/sample/domains/appraisals/rules/appraisalRecordScoping.rule.ts +0 -7
- package/src/sample/domains/attendance/concepts/attendancePermission.concept.ts +0 -17
- package/src/sample/domains/attendance/concepts/attendanceTransaction.concept.ts +0 -17
- package/src/sample/domains/attendance/concepts/publicHoliday.concept.ts +0 -10
- package/src/sample/domains/attendance/concepts/punch.concept.ts +0 -19
- package/src/sample/domains/attendance/endpoints/getAttendanceTransactionById.endpoint.ts +0 -444
- package/src/sample/domains/attendance/endpoints/getCurrentAttendanceProfile.endpoint.ts +0 -886
- package/src/sample/domains/attendance/endpoints/getCurrentAttendanceStatistics.endpoint.ts +0 -49
- package/src/sample/domains/attendance/endpoints/listAttendancePermissions.endpoint.ts +0 -176
- package/src/sample/domains/attendance/endpoints/listAttendanceTransactionStates.endpoint.ts +0 -26
- package/src/sample/domains/attendance/endpoints/listAttendanceTransactions.endpoint.ts +0 -373
- package/src/sample/domains/attendance/endpoints/listPublicHolidaysSimple.endpoint.ts +0 -52
- package/src/sample/domains/attendance/endpoints/listPunchTypes.endpoint.ts +0 -25
- package/src/sample/domains/attendance/endpoints/listPunches.endpoint.ts +0 -178
- package/src/sample/domains/attendance/index.ts +0 -41
- package/src/sample/domains/attendance/services/attendancePermissions.service.ts +0 -15
- package/src/sample/domains/employees/concepts/employee.concept.ts +0 -20
- package/src/sample/domains/employees/concepts/employeeBankAccount.concept.ts +0 -19
- package/src/sample/domains/employees/concepts/employeeChild.concept.ts +0 -19
- package/src/sample/domains/employees/concepts/employeeDocument.concept.ts +0 -21
- package/src/sample/domains/employees/concepts/employeeEvent.concept.ts +0 -21
- package/src/sample/domains/employees/concepts/employeeExperience.concept.ts +0 -18
- package/src/sample/domains/employees/concepts/employeeJobLevelChange.concept.ts +0 -21
- package/src/sample/domains/employees/concepts/employeeMedal.concept.ts +0 -20
- package/src/sample/domains/employees/concepts/employeeQualification.concept.ts +0 -20
- package/src/sample/domains/employees/concepts/employeeSpouse.concept.ts +0 -21
- package/src/sample/domains/employees/concepts/employeeViolation.concept.ts +0 -19
- package/src/sample/domains/employees/endpoints/getCurrentEmployee.endpoint.ts +0 -1651
- package/src/sample/domains/employees/endpoints/getCurrentIdentity.endpoint.ts +0 -197
- package/src/sample/domains/employees/endpoints/getCurrentManagers.endpoint.ts +0 -142
- package/src/sample/domains/employees/endpoints/getEmployeeById.endpoint.ts +0 -1659
- package/src/sample/domains/employees/endpoints/getEmployeeStatistics.endpoint.ts +0 -43
- package/src/sample/domains/employees/endpoints/listBankAccounts.endpoint.ts +0 -223
- package/src/sample/domains/employees/endpoints/listChildren.endpoint.ts +0 -211
- package/src/sample/domains/employees/endpoints/listDocuments.endpoint.ts +0 -222
- package/src/sample/domains/employees/endpoints/listEmployeesSimple.endpoint.ts +0 -156
- package/src/sample/domains/employees/endpoints/listEvents.endpoint.ts +0 -120
- package/src/sample/domains/employees/endpoints/listExperiences.endpoint.ts +0 -188
- package/src/sample/domains/employees/endpoints/listJobLevelChanges.endpoint.ts +0 -240
- package/src/sample/domains/employees/endpoints/listMedals.endpoint.ts +0 -266
- package/src/sample/domains/employees/endpoints/listQualifications.endpoint.ts +0 -265
- package/src/sample/domains/employees/endpoints/listSpouses.endpoint.ts +0 -278
- package/src/sample/domains/employees/endpoints/listViolations.endpoint.ts +0 -245
- package/src/sample/domains/employees/index.ts +0 -75
- package/src/sample/domains/employees/services/employees.service.ts +0 -181
- package/src/sample/domains/index.ts +0 -10
- package/src/sample/domains/leaves/concepts/leave.concept.ts +0 -21
- package/src/sample/domains/leaves/concepts/leaveBalance.concept.ts +0 -17
- package/src/sample/domains/leaves/concepts/leaveBalanceTransaction.concept.ts +0 -16
- package/src/sample/domains/leaves/concepts/leaveType.concept.ts +0 -13
- package/src/sample/domains/leaves/concepts/leaveTypes.concept.ts +0 -205
- package/src/sample/domains/leaves/endpoints/getBalance.endpoint.ts +0 -53
- package/src/sample/domains/leaves/endpoints/getLeaveById.endpoint.ts +0 -53
- package/src/sample/domains/leaves/endpoints/listBalanceTransactions.endpoint.ts +0 -196
- package/src/sample/domains/leaves/endpoints/listBalanceTypes.endpoint.ts +0 -26
- package/src/sample/domains/leaves/endpoints/listBalances.endpoint.ts +0 -201
- package/src/sample/domains/leaves/endpoints/listLeaveTypes.endpoint.ts +0 -28
- package/src/sample/domains/leaves/endpoints/listLeaves.endpoint.ts +0 -234
- package/src/sample/domains/leaves/index.ts +0 -38
- package/src/sample/domains/leaves/services/leaveRequests.service.ts +0 -232
- package/src/sample/domains/notifications/concepts/notification.concept.ts +0 -19
- package/src/sample/domains/notifications/endpoints/countMyNotifications.endpoint.ts +0 -21
- package/src/sample/domains/notifications/endpoints/getMyNotification.endpoint.ts +0 -57
- package/src/sample/domains/notifications/endpoints/listMyNotifications.endpoint.ts +0 -59
- package/src/sample/domains/notifications/endpoints/markMyNotificationAsRead.endpoint.ts +0 -32
- package/src/sample/domains/notifications/index.ts +0 -20
- package/src/sample/domains/payroll/concepts/payslip.concept.ts +0 -18
- package/src/sample/domains/payroll/concepts/salaryCertificate.concept.ts +0 -17
- package/src/sample/domains/payroll/concepts/salaryCertificateRequestingEntity.concept.ts +0 -8
- package/src/sample/domains/payroll/endpoints/getPayslipById.endpoint.ts +0 -238
- package/src/sample/domains/payroll/endpoints/listPayslips.endpoint.ts +0 -216
- package/src/sample/domains/payroll/endpoints/listSalaryCertificateLanguages.endpoint.ts +0 -25
- package/src/sample/domains/payroll/endpoints/listSalaryCertificateRequestingEntities.endpoint.ts +0 -62
- package/src/sample/domains/payroll/endpoints/listSalaryCertificateTypes.endpoint.ts +0 -24
- package/src/sample/domains/payroll/endpoints/listSalaryCertificates.endpoint.ts +0 -122
- package/src/sample/domains/payroll/endpoints/previewSalaryCertificate.endpoint.ts +0 -45
- package/src/sample/domains/payroll/index.ts +0 -32
- package/src/sample/domains/payroll/rules/payrollEmployeeContext.rule.ts +0 -7
- package/src/sample/domains/payroll/services/salaryCertificates.service.ts +0 -15
- package/src/sample/domains/servicing/concepts/service.concept.ts +0 -6
- package/src/sample/domains/servicing/concepts/serviceRequest.concept.ts +0 -12
- package/src/sample/domains/servicing/concepts/serviceRequestApproval.concept.ts +0 -9
- package/src/sample/domains/servicing/endpoints/cancelServiceRequest.endpoint.ts +0 -21
- package/src/sample/domains/servicing/endpoints/createServiceRequest.endpoint.ts +0 -511
- package/src/sample/domains/servicing/endpoints/getServiceRequest.endpoint.ts +0 -352
- package/src/sample/domains/servicing/endpoints/getServiceRequestFormItems.endpoint.ts +0 -219
- package/src/sample/domains/servicing/endpoints/getServiceRequestTransactionFormItems.endpoint.ts +0 -216
- package/src/sample/domains/servicing/endpoints/getServiceStates.endpoint.ts +0 -47
- package/src/sample/domains/servicing/endpoints/invokeServiceRequestAction.endpoint.ts +0 -139
- package/src/sample/domains/servicing/endpoints/listServiceRequests.endpoint.ts +0 -292
- package/src/sample/domains/servicing/endpoints/listServices.endpoint.ts +0 -108
- package/src/sample/domains/servicing/endpoints/updateServiceRequest.endpoint.ts +0 -355
- package/src/sample/domains/servicing/index.ts +0 -44
- package/src/sample/domains/servicing/rules/checkValidActions.rule.ts +0 -7
- package/src/sample/domains/servicing/rules/createServiceRequest.rule.ts +0 -7
- package/src/sample/domains/servicing/rules/filterServiceRequestsByState.rule.ts +0 -7
- package/src/sample/domains/servicing/rules/passingOptionFields.rule.ts +0 -7
- package/src/sample/domains/shared/rules/pagination.rule.ts +0 -7
- package/src/sample/domains/suggestions/concepts/suggestion.concept.ts +0 -19
- package/src/sample/domains/suggestions/concepts/suggestionCategory.concept.ts +0 -7
- package/src/sample/domains/suggestions/concepts/suggestionImpactLevel.concept.ts +0 -7
- package/src/sample/domains/suggestions/endpoints/createSuggestion.endpoint.ts +0 -705
- package/src/sample/domains/suggestions/endpoints/deleteSuggestion.endpoint.ts +0 -32
- package/src/sample/domains/suggestions/endpoints/getSuggestionById.endpoint.ts +0 -635
- package/src/sample/domains/suggestions/endpoints/listSuggestionCategories.endpoint.ts +0 -62
- package/src/sample/domains/suggestions/endpoints/listSuggestionImpactLevels.endpoint.ts +0 -62
- package/src/sample/domains/suggestions/endpoints/listSuggestionStates.endpoint.ts +0 -24
- package/src/sample/domains/suggestions/endpoints/listSuggestions.endpoint.ts +0 -132
- package/src/sample/domains/suggestions/endpoints/updateSuggestion.endpoint.ts +0 -690
- package/src/sample/domains/suggestions/endpoints/updateSuggestionState.endpoint.ts +0 -39
- package/src/sample/domains/suggestions/index.ts +0 -34
- package/src/sample/domains/suggestions/rules/suggestionScope.rule.ts +0 -7
- package/src/sample/domains/surveys/concepts/survey.concept.ts +0 -10
- package/src/sample/domains/surveys/concepts/surveyResponse.concept.ts +0 -14
- package/src/sample/domains/surveys/endpoints/getSurveyResponseById.endpoint.ts +0 -131
- package/src/sample/domains/surveys/endpoints/getTargetedSurvey.endpoint.ts +0 -129
- package/src/sample/domains/surveys/endpoints/listTargetedSurveys.endpoint.ts +0 -160
- package/src/sample/domains/surveys/endpoints/respondToSurvey.endpoint.ts +0 -144
- package/src/sample/domains/surveys/index.ts +0 -21
- package/src/sample/drizzle.config.ts +0 -11
- package/src/sample/index.bak.ts +0 -341
- package/src/sample/index.ts +0 -318
- package/src/sample/official-statements/defaults.ts +0 -57
- package/src/sample/official-statements/routes.ts +0 -137
- package/src/sample/official-statements/service.ts +0 -148
- package/src/sample/official-statements/types.ts +0 -23
- /package/src/lib/db/migrations/{20260326231647_nice_speedball → mssql/20260326231647_nice_speedball}/migration.sql +0 -0
- /package/src/lib/db/migrations/{20260326231647_nice_speedball → mssql/20260326231647_nice_speedball}/snapshot.json +0 -0
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Transforms declarative DomainDef into idempotent MERGE-based Cypher.
|
|
3
|
-
*
|
|
4
|
-
* Returns two phases:
|
|
5
|
-
* - `nodes` -- MERGE statements that create/update Domain, Concept, Endpoint,
|
|
6
|
-
* Service, and Rule nodes.
|
|
7
|
-
* - `edges` -- MATCH+MERGE statements that wire up relationships between nodes.
|
|
8
|
-
*
|
|
9
|
-
* The caller MUST run all `nodes` from every module before running any `edges`,
|
|
10
|
-
* because edges may reference nodes defined in a different module (e.g. a Rule
|
|
11
|
-
* in the leaves module that GOVERNS a Concept created by the servicing module).
|
|
12
|
-
*/
|
|
13
|
-
import type { DomainDef } from "./types.ts";
|
|
14
|
-
export type GeneratedCypher = {
|
|
15
|
-
nodes: string[];
|
|
16
|
-
edges: string[];
|
|
17
|
-
};
|
|
18
|
-
export declare function generateCypher(data: DomainDef): {
|
|
19
|
-
nodes: string[];
|
|
20
|
-
edges: string[];
|
|
21
|
-
};
|
|
22
|
-
export declare function toCypherScript(data: DomainDef): string;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Seed orchestrator for the knowledge graph.
|
|
3
|
-
*
|
|
4
|
-
* Validates domains, generates Cypher, executes against Neo4j,
|
|
5
|
-
* and generates concept embeddings. All configuration is passed
|
|
6
|
-
* explicitly — no environment variables or Convex dependencies.
|
|
7
|
-
*/
|
|
8
|
-
import type { EmbeddingModel } from "ai";
|
|
9
|
-
import type { Neo4jConfig } from "./neo4j.ts";
|
|
10
|
-
import type { DomainDef } from "./types.ts";
|
|
11
|
-
export type EmbeddingConfig = {
|
|
12
|
-
model: EmbeddingModel;
|
|
13
|
-
dimension: number;
|
|
14
|
-
};
|
|
15
|
-
export type SeedGraphConfig = {
|
|
16
|
-
neo4j: Neo4jConfig;
|
|
17
|
-
embedding: EmbeddingConfig;
|
|
18
|
-
};
|
|
19
|
-
export declare function seedGraph(config: SeedGraphConfig, domains: Record<string, DomainDef>): Promise<void>;
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { ThreadSummary } from "../types.ts";
|
|
2
|
-
export type ThreadCreatedEvent = {
|
|
3
|
-
type: "thread:created";
|
|
4
|
-
payload: {
|
|
5
|
-
thread: ThreadSummary;
|
|
6
|
-
};
|
|
7
|
-
};
|
|
8
|
-
export type ThreadDeletedEvent = {
|
|
9
|
-
type: "thread:deleted";
|
|
10
|
-
payload: {
|
|
11
|
-
threadId: string;
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
|
-
export type ThreadTitleUpdatedEvent = {
|
|
15
|
-
type: "thread:title-updated";
|
|
16
|
-
payload: {
|
|
17
|
-
thread: ThreadSummary;
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
export type ThreadRunStartedEvent = {
|
|
21
|
-
type: "thread:run-started";
|
|
22
|
-
payload: {
|
|
23
|
-
thread: ThreadSummary;
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
export type ThreadRunFinishedEvent = {
|
|
27
|
-
type: "thread:run-finished";
|
|
28
|
-
payload: {
|
|
29
|
-
thread: ThreadSummary;
|
|
30
|
-
};
|
|
31
|
-
};
|
|
32
|
-
export type ThreadMessagesUpdatedEvent = {
|
|
33
|
-
type: "thread:messages-updated";
|
|
34
|
-
payload: {
|
|
35
|
-
threadId: string;
|
|
36
|
-
thread: ThreadSummary;
|
|
37
|
-
};
|
|
38
|
-
};
|
|
39
|
-
export type WsEvent = ThreadCreatedEvent | ThreadDeletedEvent | ThreadTitleUpdatedEvent | ThreadRunStartedEvent | ThreadRunFinishedEvent | ThreadMessagesUpdatedEvent;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
export { addConnection, removeConnection, getConnections } from "./connections.ts";
|
|
2
|
-
export { notify } from "./notify.ts";
|
|
3
|
-
export type { WsEvent, ThreadCreatedEvent, ThreadDeletedEvent, ThreadTitleUpdatedEvent, ThreadRunStartedEvent, ThreadRunFinishedEvent, ThreadMessagesUpdatedEvent, } from "./events.ts";
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import type { ToolUIPart, UIMessage } from "ai";
|
|
2
|
-
import type { Thread, StoredMessage, CapturedFileInput, TokenUsage } from "../types";
|
|
3
|
-
import type { ThreadContextMeta } from "../ai/context/types.ts";
|
|
4
|
-
|
|
5
|
-
export type DatabaseAdapter = {
|
|
6
|
-
threads: {
|
|
7
|
-
list(userId: string, agentId: string): Promise<Thread[]>;
|
|
8
|
-
getById(userId: string, threadId: string): Promise<Thread | null>;
|
|
9
|
-
create(userId: string, agentId: string): Promise<Thread>;
|
|
10
|
-
delete(userId: string, threadId: string): Promise<void>;
|
|
11
|
-
touch(threadId: string): Promise<Thread>;
|
|
12
|
-
updateTitle(threadId: string, title: string): Promise<void>;
|
|
13
|
-
updateSession(threadId: string, session: Record<string, unknown>): Promise<void>;
|
|
14
|
-
updateContextMeta(threadId: string, meta: ThreadContextMeta): Promise<void>;
|
|
15
|
-
};
|
|
16
|
-
messages: {
|
|
17
|
-
list(userId: string, threadId: string, opts?: { limit?: number }): Promise<StoredMessage[]>;
|
|
18
|
-
upsert(threadId: string, messages: UIMessage[]): Promise<void>;
|
|
19
|
-
};
|
|
20
|
-
llmRequests: {
|
|
21
|
-
insert(
|
|
22
|
-
requests: {
|
|
23
|
-
messageId: string;
|
|
24
|
-
stepNumber: number;
|
|
25
|
-
prompt: string;
|
|
26
|
-
output: string | null;
|
|
27
|
-
tokenUsage: TokenUsage | null;
|
|
28
|
-
}[],
|
|
29
|
-
): Promise<void>;
|
|
30
|
-
listByMessageId(messageId: string): Promise<
|
|
31
|
-
{
|
|
32
|
-
id: string;
|
|
33
|
-
prompt: string;
|
|
34
|
-
output: string | null;
|
|
35
|
-
tokenUsage: TokenUsage | null;
|
|
36
|
-
}[]
|
|
37
|
-
>;
|
|
38
|
-
};
|
|
39
|
-
capturedFiles: {
|
|
40
|
-
create(
|
|
41
|
-
userId: string,
|
|
42
|
-
messageId: string,
|
|
43
|
-
toolPart: ToolUIPart,
|
|
44
|
-
files: CapturedFileInput[],
|
|
45
|
-
): Promise<{ id: string; uploadId: string }[]>;
|
|
46
|
-
getById(id: string, userId: string): Promise<{ name: string } | null>;
|
|
47
|
-
};
|
|
48
|
-
};
|
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
import type { ToolUIPart, UIMessage } from "ai";
|
|
2
|
-
import { and, asc, desc, eq, getColumns, inArray, type InferInsertModel } from "drizzle-orm";
|
|
3
|
-
import { drizzle } from "drizzle-orm/node-mssql";
|
|
4
|
-
import { threads, messages, capturedFiles, llmRequests } from "../db/schema";
|
|
5
|
-
import type { DatabaseAdapter } from "./database";
|
|
6
|
-
import type { StorageAdapter } from "./storage";
|
|
7
|
-
import type { Thread, CapturedFileInput, MessageMetadata } from "../types";
|
|
8
|
-
import type { ThreadContextMeta } from "../ai/context/types.ts";
|
|
9
|
-
|
|
10
|
-
export function createMssqlAdapter(connectionString: string, storage: StorageAdapter) {
|
|
11
|
-
const db = drizzle(connectionString, { casing: "snake_case" });
|
|
12
|
-
|
|
13
|
-
const adapter: DatabaseAdapter = {
|
|
14
|
-
threads: {
|
|
15
|
-
async list(userId: string, agentId: string) {
|
|
16
|
-
return (await db
|
|
17
|
-
.select()
|
|
18
|
-
.from(threads)
|
|
19
|
-
.where(and(eq(threads.userId, userId), eq(threads.agentId, agentId)))
|
|
20
|
-
.orderBy(desc(threads.updatedAt), desc(threads.createdAt))) as Thread[];
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
async getById(userId: string, threadId: string) {
|
|
24
|
-
const result = await db
|
|
25
|
-
.select()
|
|
26
|
-
.top(1)
|
|
27
|
-
.from(threads)
|
|
28
|
-
.where(and(eq(threads.id, threadId), eq(threads.userId, userId)))
|
|
29
|
-
.execute();
|
|
30
|
-
return result.length ? (result[0] as Thread) : null;
|
|
31
|
-
},
|
|
32
|
-
|
|
33
|
-
async create(userId: string, agentId: string) {
|
|
34
|
-
const result = await db
|
|
35
|
-
.insert(threads)
|
|
36
|
-
.output()
|
|
37
|
-
.values({ userId, agentId })
|
|
38
|
-
.execute();
|
|
39
|
-
return result[0] as Thread;
|
|
40
|
-
},
|
|
41
|
-
|
|
42
|
-
async delete(userId: string, threadId: string) {
|
|
43
|
-
const capturedFilePaths = await db
|
|
44
|
-
.select({ id: capturedFiles.id })
|
|
45
|
-
.from(capturedFiles)
|
|
46
|
-
.innerJoin(messages, eq(messages.id, capturedFiles.messageId))
|
|
47
|
-
.innerJoin(threads, eq(threads.id, messages.threadId))
|
|
48
|
-
.where(and(eq(threads.id, threadId), eq(threads.userId, userId)))
|
|
49
|
-
.execute()
|
|
50
|
-
.then((x) => x.map((y) => `captured_files/${y.id}`));
|
|
51
|
-
|
|
52
|
-
await storage.delete(capturedFilePaths);
|
|
53
|
-
|
|
54
|
-
await db
|
|
55
|
-
.delete(threads)
|
|
56
|
-
.where(and(eq(threads.id, threadId), eq(threads.userId, userId)))
|
|
57
|
-
.execute();
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
async touch(threadId: string) {
|
|
61
|
-
const result = await db
|
|
62
|
-
.update(threads)
|
|
63
|
-
.set({ updatedAt: new Date() })
|
|
64
|
-
.where(eq(threads.id, threadId))
|
|
65
|
-
.output()
|
|
66
|
-
.execute();
|
|
67
|
-
|
|
68
|
-
return result[0] as Thread;
|
|
69
|
-
},
|
|
70
|
-
|
|
71
|
-
async updateTitle(threadId: string, title: string) {
|
|
72
|
-
await db.update(threads).set({ title }).where(eq(threads.id, threadId)).execute();
|
|
73
|
-
},
|
|
74
|
-
|
|
75
|
-
async updateSession(threadId: string, session: Record<string, unknown>) {
|
|
76
|
-
await db.update(threads).set({ session }).where(eq(threads.id, threadId)).execute();
|
|
77
|
-
},
|
|
78
|
-
|
|
79
|
-
async updateContextMeta(threadId: string, meta: ThreadContextMeta) {
|
|
80
|
-
await db
|
|
81
|
-
.update(threads)
|
|
82
|
-
.set({ contextMeta: meta })
|
|
83
|
-
.where(eq(threads.id, threadId))
|
|
84
|
-
.execute();
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
|
|
88
|
-
messages: {
|
|
89
|
-
async list(userId: string, threadId: string, opts?: { limit?: number }) {
|
|
90
|
-
return await db
|
|
91
|
-
.select(getColumns(messages))
|
|
92
|
-
.from(messages)
|
|
93
|
-
.innerJoin(threads, eq(threads.id, messages.threadId))
|
|
94
|
-
.where(and(eq(threads.userId, userId), eq(threads.id, threadId)))
|
|
95
|
-
.orderBy(desc(messages.createdAt))
|
|
96
|
-
.offset(0)
|
|
97
|
-
.fetch(opts?.limit ?? 100)
|
|
98
|
-
.then((x) => x.reverse());
|
|
99
|
-
},
|
|
100
|
-
|
|
101
|
-
async upsert(threadId: string, messagesToUpsert: UIMessage<MessageMetadata>[]) {
|
|
102
|
-
const ids = messagesToUpsert.map((x) => x.id);
|
|
103
|
-
|
|
104
|
-
const existingIds = await db
|
|
105
|
-
.select({ id: messages.id })
|
|
106
|
-
.from(messages)
|
|
107
|
-
.where(inArray(messages.id, ids))
|
|
108
|
-
.execute()
|
|
109
|
-
.then((x) => x.map((y) => y.id));
|
|
110
|
-
|
|
111
|
-
const newMessages = messagesToUpsert.filter((x) => !existingIds.includes(x.id));
|
|
112
|
-
|
|
113
|
-
if (newMessages.length) {
|
|
114
|
-
await db
|
|
115
|
-
.insert(messages)
|
|
116
|
-
.values(
|
|
117
|
-
newMessages.map(
|
|
118
|
-
(x) =>
|
|
119
|
-
({
|
|
120
|
-
id: x.id,
|
|
121
|
-
role: x.role,
|
|
122
|
-
threadId,
|
|
123
|
-
content: x,
|
|
124
|
-
text: x.parts.find((y) => y.type === "text")?.text,
|
|
125
|
-
}) satisfies InferInsertModel<typeof messages>,
|
|
126
|
-
),
|
|
127
|
-
)
|
|
128
|
-
.execute();
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const existingMessages = messagesToUpsert.filter((x) => existingIds.includes(x.id));
|
|
132
|
-
for (const message of existingMessages) {
|
|
133
|
-
await db
|
|
134
|
-
.update(messages)
|
|
135
|
-
.set({
|
|
136
|
-
content: message,
|
|
137
|
-
text: message.parts.find((x) => x.type === "text")?.text,
|
|
138
|
-
})
|
|
139
|
-
.where(eq(messages.id, message.id))
|
|
140
|
-
.execute();
|
|
141
|
-
}
|
|
142
|
-
},
|
|
143
|
-
},
|
|
144
|
-
|
|
145
|
-
llmRequests: {
|
|
146
|
-
async insert(requests) {
|
|
147
|
-
if (!requests.length) return;
|
|
148
|
-
|
|
149
|
-
await db
|
|
150
|
-
.insert(llmRequests)
|
|
151
|
-
.values(
|
|
152
|
-
requests.map(
|
|
153
|
-
(r) =>
|
|
154
|
-
({
|
|
155
|
-
messageId: r.messageId,
|
|
156
|
-
stepNumber: r.stepNumber,
|
|
157
|
-
prompt: r.prompt,
|
|
158
|
-
output: r.output,
|
|
159
|
-
tokenUsage: r.tokenUsage,
|
|
160
|
-
}) satisfies InferInsertModel<typeof llmRequests>,
|
|
161
|
-
),
|
|
162
|
-
)
|
|
163
|
-
.execute();
|
|
164
|
-
},
|
|
165
|
-
|
|
166
|
-
async listByMessageId(messageId) {
|
|
167
|
-
return await db
|
|
168
|
-
.select({
|
|
169
|
-
id: llmRequests.id,
|
|
170
|
-
prompt: llmRequests.prompt,
|
|
171
|
-
output: llmRequests.output,
|
|
172
|
-
tokenUsage: llmRequests.tokenUsage,
|
|
173
|
-
})
|
|
174
|
-
.from(llmRequests)
|
|
175
|
-
.where(eq(llmRequests.messageId, messageId))
|
|
176
|
-
.orderBy(asc(llmRequests.stepNumber))
|
|
177
|
-
.execute();
|
|
178
|
-
},
|
|
179
|
-
},
|
|
180
|
-
|
|
181
|
-
capturedFiles: {
|
|
182
|
-
async create(
|
|
183
|
-
userId: string,
|
|
184
|
-
messageId: string,
|
|
185
|
-
toolPart: ToolUIPart,
|
|
186
|
-
files: CapturedFileInput[],
|
|
187
|
-
) {
|
|
188
|
-
const result = await db
|
|
189
|
-
.insert(capturedFiles)
|
|
190
|
-
.output({ id: capturedFiles.id })
|
|
191
|
-
.values(
|
|
192
|
-
files.map(
|
|
193
|
-
(file) =>
|
|
194
|
-
({
|
|
195
|
-
userId,
|
|
196
|
-
messageId,
|
|
197
|
-
toolPart,
|
|
198
|
-
name: file.file.name,
|
|
199
|
-
agentGeneratedId: file.id,
|
|
200
|
-
}) satisfies InferInsertModel<typeof capturedFiles>,
|
|
201
|
-
),
|
|
202
|
-
)
|
|
203
|
-
.execute()
|
|
204
|
-
.then((x) => x.map((y) => y.id));
|
|
205
|
-
|
|
206
|
-
await Promise.all(
|
|
207
|
-
result.map(
|
|
208
|
-
async (id, idx) =>
|
|
209
|
-
await storage.put(`captured_files/${id}`, files[idx]!.file.bytes),
|
|
210
|
-
),
|
|
211
|
-
);
|
|
212
|
-
|
|
213
|
-
return result.map((r, i) => ({ id: files[i]!.id, uploadId: r }));
|
|
214
|
-
},
|
|
215
|
-
|
|
216
|
-
async getById(id: string, userId: string) {
|
|
217
|
-
const file = await db
|
|
218
|
-
.select()
|
|
219
|
-
.top(1)
|
|
220
|
-
.from(capturedFiles)
|
|
221
|
-
.where(and(eq(capturedFiles.id, id), eq(capturedFiles.userId, userId)))
|
|
222
|
-
.execute()
|
|
223
|
-
.then((x) => (x.length ? x[0] : null));
|
|
224
|
-
|
|
225
|
-
return file ? { name: file.name } : null;
|
|
226
|
-
},
|
|
227
|
-
},
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
return adapter;
|
|
231
|
-
}
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
type ActiveStream = {
|
|
2
|
-
id: string;
|
|
3
|
-
abortController: AbortController;
|
|
4
|
-
buffer: string[];
|
|
5
|
-
subscribers: Set<ReadableStreamDefaultController<string>>;
|
|
6
|
-
isComplete: boolean;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
const streams = new Map<string, ActiveStream>();
|
|
10
|
-
|
|
11
|
-
export function registerStream(threadId: string, abortController: AbortController) {
|
|
12
|
-
const existing = streams.get(threadId);
|
|
13
|
-
if (existing) {
|
|
14
|
-
existing.abortController.abort();
|
|
15
|
-
for (const controller of existing.subscribers) {
|
|
16
|
-
try {
|
|
17
|
-
controller.close();
|
|
18
|
-
} catch {
|
|
19
|
-
/* already closed */
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
streams.delete(threadId);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const entry: ActiveStream = {
|
|
26
|
-
id: crypto.randomUUID(),
|
|
27
|
-
abortController,
|
|
28
|
-
buffer: [],
|
|
29
|
-
subscribers: new Set(),
|
|
30
|
-
isComplete: false,
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
streams.set(threadId, entry);
|
|
34
|
-
return entry;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function attachSseStream(threadId: string, sseStream: ReadableStream<string>) {
|
|
38
|
-
const entry = streams.get(threadId);
|
|
39
|
-
if (!entry) return;
|
|
40
|
-
|
|
41
|
-
const reader = sseStream.getReader();
|
|
42
|
-
(async function consume() {
|
|
43
|
-
try {
|
|
44
|
-
while (true) {
|
|
45
|
-
const { done, value } = await reader.read();
|
|
46
|
-
if (done) break;
|
|
47
|
-
entry.buffer.push(value);
|
|
48
|
-
for (const controller of entry.subscribers) {
|
|
49
|
-
try {
|
|
50
|
-
controller.enqueue(value);
|
|
51
|
-
} catch {
|
|
52
|
-
/* subscriber cancelled */
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
} catch {
|
|
57
|
-
/* stream aborted or errored */
|
|
58
|
-
} finally {
|
|
59
|
-
entry.isComplete = true;
|
|
60
|
-
for (const controller of entry.subscribers) {
|
|
61
|
-
try {
|
|
62
|
-
controller.close();
|
|
63
|
-
} catch {
|
|
64
|
-
/* already closed */
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
})();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function subscribe(threadId: string) {
|
|
72
|
-
const entry = streams.get(threadId);
|
|
73
|
-
if (!entry) return null;
|
|
74
|
-
|
|
75
|
-
let savedController: ReadableStreamDefaultController<string>;
|
|
76
|
-
|
|
77
|
-
return new ReadableStream<string>({
|
|
78
|
-
start(controller) {
|
|
79
|
-
savedController = controller;
|
|
80
|
-
controller.enqueue("[START]"); // XXX: we need to add this flag for an immediate response for the subscriber, otherwise we'd have to wait for the first token from the llm.
|
|
81
|
-
for (const chunk of entry.buffer) {
|
|
82
|
-
controller.enqueue(chunk);
|
|
83
|
-
}
|
|
84
|
-
if (entry.isComplete) {
|
|
85
|
-
controller.close();
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
entry.subscribers.add(controller);
|
|
89
|
-
},
|
|
90
|
-
cancel() {
|
|
91
|
-
entry.subscribers.delete(savedController);
|
|
92
|
-
},
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function abortStream(threadId: string) {
|
|
97
|
-
const entry = streams.get(threadId);
|
|
98
|
-
if (!entry) return false;
|
|
99
|
-
queueMicrotask(() => entry.abortController.abort());
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function isStreamRunning(threadId: string) {
|
|
104
|
-
const entry = streams.get(threadId);
|
|
105
|
-
return entry ? !entry.isComplete : false;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function removeStream(threadId: string, streamId?: string) {
|
|
109
|
-
const entry = streams.get(threadId);
|
|
110
|
-
if (!entry) return;
|
|
111
|
-
if (streamId && entry.id !== streamId) return;
|
|
112
|
-
|
|
113
|
-
if (entry) {
|
|
114
|
-
for (const controller of entry.subscribers) {
|
|
115
|
-
try {
|
|
116
|
-
controller.close();
|
|
117
|
-
} catch {
|
|
118
|
-
/* already closed */
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
streams.delete(threadId);
|
|
123
|
-
}
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
export type { ContextConfig, ThreadContextMeta } from "./types.ts";
|
|
2
|
-
export { DEFAULT_CONTEXT_CONFIG } from "./types.ts";
|
|
3
|
-
export {
|
|
4
|
-
CHARS_PER_TOKEN,
|
|
5
|
-
estimateTokens,
|
|
6
|
-
estimateMessageTokens,
|
|
7
|
-
estimateMessagesTokens,
|
|
8
|
-
} from "./token-estimator.ts";
|
|
9
|
-
export { compressToolResults, smartStructuralCompress, summarizeValue } from "./compressor.ts";
|
|
10
|
-
export { summarizeMessages } from "./summarizer.ts";
|
|
11
|
-
export { buildContextMessages, trimMessagesToFit } from "./builder.ts";
|
|
12
|
-
export {
|
|
13
|
-
compressIntraTurnToolResults,
|
|
14
|
-
estimateToolResultTokens,
|
|
15
|
-
summarizeOldStepResults,
|
|
16
|
-
} from "./intra-turn-compressor.ts";
|
|
17
|
-
|
|
18
|
-
import type { UIMessage } from "ai";
|
|
19
|
-
import type { ResolvedCortexAgentConfig } from "../../config.ts";
|
|
20
|
-
import type { Thread } from "../../types.ts";
|
|
21
|
-
import type { ThreadContextMeta } from "./types.ts";
|
|
22
|
-
import { estimateMessagesTokens } from "./token-estimator.ts";
|
|
23
|
-
import { summarizeMessages } from "./summarizer.ts";
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Post-response context optimization.
|
|
27
|
-
* Called fire-and-forget from onFinish — summarizes older messages
|
|
28
|
-
* when token usage exceeds the configured threshold.
|
|
29
|
-
*/
|
|
30
|
-
export async function optimizeThreadContext(
|
|
31
|
-
thread: Thread,
|
|
32
|
-
messages: UIMessage[],
|
|
33
|
-
config: ResolvedCortexAgentConfig,
|
|
34
|
-
) {
|
|
35
|
-
const contextConfig = config.context;
|
|
36
|
-
|
|
37
|
-
// 1. Estimate tokens for all messages
|
|
38
|
-
const estimates = estimateMessagesTokens(messages);
|
|
39
|
-
const totalEstimatedTokens = estimates.reduce((sum, e) => sum + e.tokens, 0);
|
|
40
|
-
|
|
41
|
-
// 2. Check if over summarization threshold
|
|
42
|
-
const threshold = contextConfig.maxContextTokens * contextConfig.summarizationThreshold;
|
|
43
|
-
|
|
44
|
-
if (totalEstimatedTokens <= threshold) {
|
|
45
|
-
// Update token estimate but skip summarization
|
|
46
|
-
const meta: ThreadContextMeta = {
|
|
47
|
-
summary: thread.contextMeta?.summary ?? null,
|
|
48
|
-
summaryUpToMessageId: thread.contextMeta?.summaryUpToMessageId ?? null,
|
|
49
|
-
totalEstimatedTokens,
|
|
50
|
-
lastOptimizedAt: new Date().toISOString(),
|
|
51
|
-
};
|
|
52
|
-
await config.db.threads.updateContextMeta(thread.id, meta);
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// 3. Keep the most recent messages unsummarized (they're the hot context)
|
|
57
|
-
const recentCount = Math.min(contextConfig.recentMessagesToKeep, messages.length);
|
|
58
|
-
const messagesToSummarize = messages.slice(0, messages.length - recentCount);
|
|
59
|
-
|
|
60
|
-
if (messagesToSummarize.length === 0) return;
|
|
61
|
-
|
|
62
|
-
// 4. Determine model config for summarization
|
|
63
|
-
const modelConfig = contextConfig.summarizationModel ?? config.model;
|
|
64
|
-
|
|
65
|
-
// 5. Generate summary incorporating any existing summary
|
|
66
|
-
const existingSummary = thread.contextMeta?.summary ?? null;
|
|
67
|
-
|
|
68
|
-
const summary = await summarizeMessages(messagesToSummarize, existingSummary, modelConfig);
|
|
69
|
-
|
|
70
|
-
// 6. Update thread context meta
|
|
71
|
-
const lastSummarizedMessage = messagesToSummarize.at(-1);
|
|
72
|
-
const meta = {
|
|
73
|
-
summary,
|
|
74
|
-
summaryUpToMessageId: lastSummarizedMessage?.id ?? null,
|
|
75
|
-
totalEstimatedTokens,
|
|
76
|
-
lastOptimizedAt: new Date().toISOString(),
|
|
77
|
-
} satisfies ThreadContextMeta;
|
|
78
|
-
|
|
79
|
-
await config.db.threads.updateContextMeta(thread.id, meta);
|
|
80
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { tool } from "ai";
|
|
2
|
-
import z from "zod";
|
|
3
|
-
|
|
4
|
-
export const captureFilesTool = tool({
|
|
5
|
-
description: "Let the user upload a file",
|
|
6
|
-
inputSchema: z.object({
|
|
7
|
-
files: z.array(
|
|
8
|
-
z.object({
|
|
9
|
-
label: z
|
|
10
|
-
.string()
|
|
11
|
-
.describe("A label to view to the user to know what file they need to upload"),
|
|
12
|
-
id: z
|
|
13
|
-
.string()
|
|
14
|
-
.describe(
|
|
15
|
-
"An ID to the file for you to use it when calling apis or later processing",
|
|
16
|
-
),
|
|
17
|
-
}),
|
|
18
|
-
),
|
|
19
|
-
}),
|
|
20
|
-
});
|