@cat-factory/node-server 0.29.0 → 0.31.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.
Files changed (33) hide show
  1. package/dist/config.d.ts.map +1 -1
  2. package/dist/config.js +37 -0
  3. package/dist/config.js.map +1 -1
  4. package/dist/container.d.ts.map +1 -1
  5. package/dist/container.js +23 -1
  6. package/dist/container.js.map +1 -1
  7. package/dist/db/schema.d.ts +243 -0
  8. package/dist/db/schema.d.ts.map +1 -1
  9. package/dist/db/schema.js +49 -1
  10. package/dist/db/schema.js.map +1 -1
  11. package/dist/repositories/drizzle.d.ts +2 -1
  12. package/dist/repositories/drizzle.d.ts.map +1 -1
  13. package/dist/repositories/drizzle.js +97 -2
  14. package/dist/repositories/drizzle.js.map +1 -1
  15. package/dist/retention.d.ts +9 -1
  16. package/dist/retention.d.ts.map +1 -1
  17. package/dist/retention.js +32 -0
  18. package/dist/retention.js.map +1 -1
  19. package/dist/server.d.ts.map +1 -1
  20. package/dist/server.js +6 -1
  21. package/dist/server.js.map +1 -1
  22. package/dist/storage/PostgresBinaryBlobBackend.d.ts +11 -0
  23. package/dist/storage/PostgresBinaryBlobBackend.d.ts.map +1 -0
  24. package/dist/storage/PostgresBinaryBlobBackend.js +40 -0
  25. package/dist/storage/PostgresBinaryBlobBackend.js.map +1 -0
  26. package/drizzle/20260627083558_glossy_shen/migration.sql +23 -0
  27. package/drizzle/20260627083558_glossy_shen/snapshot.json +12574 -0
  28. package/drizzle/20260627102927_worthless_celestials/migration.sql +1 -0
  29. package/drizzle/20260627102927_worthless_celestials/snapshot.json +12587 -0
  30. package/drizzle/20260627111231_binary_artifacts_created_index/migration.sql +1 -0
  31. package/drizzle/20260627111231_binary_artifacts_created_index/snapshot.json +12615 -0
  32. package/drizzle/20260627131842_brave_slayback/snapshot.json +459 -3
  33. package/package.json +16 -15
@@ -1,7 +1,7 @@
1
1
  import { LLM_WARNING_FINISH_REASONS } from '@cat-factory/kernel';
2
2
  import { blockInsertValues, blockPatchToColumns, rowToBlock, rowToExecution, executionToDetail, rowToPipeline, rowToSandboxExperiment, rowToSandboxFixture, rowToSandboxGrade, rowToSandboxPromptVersion, rowToSandboxRun, rowToWorkspace, } from '@cat-factory/server';
3
- import { and, desc, eq, gte, inArray, isNull, lt, ne, or, sql } from 'drizzle-orm';
4
- import { accountInvitations, passwordResetTokens, accountSettings, localSettings, accounts, agentContextSnapshots, agentRuns, blocks, consensusSessions, incidentEnrichmentConnections, observabilityConnections, emailConnections, llmCallMetrics, provisioningLog, memberships, mergeThresholdPresets, releaseHealthConfigs, pipelineScheduleRuns, pipelineSchedules, pipelines, requirementReviews, kaizenGradings, kaizenVerifiedCombos, clarityReviews, brainstormSessions, sandboxPromptVersions, sandboxFixtures, sandboxExperiments, sandboxRuns, sandboxGrades, services, tokenUsage, trackerSettings, modelPresets, userIdentities, users, workspaceFragmentDefaults, workspaceServices, workspaceSettings, workspaces, } from '../db/schema.js';
3
+ import { and, asc, count, desc, eq, gte, inArray, isNull, lt, ne, or, sql } from 'drizzle-orm';
4
+ import { accountInvitations, passwordResetTokens, accountSettings, localSettings, accounts, agentContextSnapshots, agentRuns, blocks, consensusSessions, incidentEnrichmentConnections, observabilityConnections, emailConnections, llmCallMetrics, provisioningLog, memberships, mergeThresholdPresets, releaseHealthConfigs, pipelineScheduleRuns, pipelineSchedules, pipelines, requirementReviews, kaizenGradings, kaizenVerifiedCombos, clarityReviews, binaryArtifacts, brainstormSessions, sandboxPromptVersions, sandboxFixtures, sandboxExperiments, sandboxRuns, sandboxGrades, services, tokenUsage, trackerSettings, modelPresets, userIdentities, users, workspaceFragmentDefaults, workspaceServices, workspaceSettings, workspaces, } from '../db/schema.js';
5
5
  // Drizzle/Postgres implementations of the core kernel repository ports. The
6
6
  // row<->domain mapping is the SAME shared mapping the Cloudflare D1 repos use
7
7
  // (@cat-factory/server), so behaviour matches across stores; this layer only owns
@@ -898,6 +898,7 @@ class DrizzleLlmCallMetricRepository {
898
898
  agentKind: llmCallMetrics.agent_kind,
899
899
  calls: sql `count(*)::int`,
900
900
  promptTokens: sql `coalesce(sum(${llmCallMetrics.prompt_tokens}), 0)::int`,
901
+ cachedPromptTokens: sql `coalesce(sum(${llmCallMetrics.cached_prompt_tokens}), 0)::int`,
901
902
  completionTokens: sql `coalesce(sum(${llmCallMetrics.completion_tokens}), 0)::int`,
902
903
  peakCompletionTokens: sql `coalesce(max(${llmCallMetrics.completion_tokens}), 0)::int`,
903
904
  maxOutputTokens: sql `max(${llmCallMetrics.request_max_tokens})`,
@@ -918,6 +919,7 @@ class DrizzleLlmCallMetricRepository {
918
919
  agentKind: r.agentKind,
919
920
  calls: Number(r.calls),
920
921
  promptTokens: Number(r.promptTokens),
922
+ cachedPromptTokens: Number(r.cachedPromptTokens),
921
923
  completionTokens: Number(r.completionTokens),
922
924
  peakCompletionTokens: Number(r.peakCompletionTokens),
923
925
  maxOutputTokens: r.maxOutputTokens == null ? null : Number(r.maxOutputTokens),
@@ -1003,6 +1005,95 @@ class DrizzleAgentContextSnapshotRepository {
1003
1005
  return deleted.length;
1004
1006
  }
1005
1007
  }
1008
+ function rowToBinaryArtifact(row) {
1009
+ return {
1010
+ id: row.id,
1011
+ workspaceId: row.workspace_id,
1012
+ executionId: row.execution_id,
1013
+ blockId: row.block_id,
1014
+ kind: row.kind,
1015
+ view: row.view,
1016
+ contentType: row.content_type,
1017
+ byteSize: row.byte_size,
1018
+ hash: row.hash,
1019
+ storage: row.storage,
1020
+ storageKey: row.storage_key,
1021
+ createdAt: row.created_at,
1022
+ };
1023
+ }
1024
+ /** Drizzle/Postgres metadata store for binary artifacts (mirror of D1 migration 0017). */
1025
+ class DrizzleBinaryArtifactMetadataStore {
1026
+ db;
1027
+ constructor(db) {
1028
+ this.db = db;
1029
+ }
1030
+ async insert(record) {
1031
+ await this.db.insert(binaryArtifacts).values({
1032
+ workspace_id: record.workspaceId,
1033
+ id: record.id,
1034
+ execution_id: record.executionId,
1035
+ block_id: record.blockId,
1036
+ kind: record.kind,
1037
+ view: record.view,
1038
+ content_type: record.contentType,
1039
+ byte_size: record.byteSize,
1040
+ hash: record.hash,
1041
+ storage: record.storage,
1042
+ storage_key: record.storageKey,
1043
+ created_at: record.createdAt,
1044
+ });
1045
+ }
1046
+ async get(workspaceId, id) {
1047
+ const rows = await this.db
1048
+ .select()
1049
+ .from(binaryArtifacts)
1050
+ .where(and(eq(binaryArtifacts.workspace_id, workspaceId), eq(binaryArtifacts.id, id)))
1051
+ .limit(1);
1052
+ return rows[0] ? rowToBinaryArtifact(rows[0]) : null;
1053
+ }
1054
+ async listByExecution(workspaceId, executionId) {
1055
+ const rows = await this.db
1056
+ .select()
1057
+ .from(binaryArtifacts)
1058
+ .where(and(eq(binaryArtifacts.workspace_id, workspaceId), eq(binaryArtifacts.execution_id, executionId)))
1059
+ .orderBy(asc(binaryArtifacts.created_at), asc(binaryArtifacts.id));
1060
+ return rows.map(rowToBinaryArtifact);
1061
+ }
1062
+ async countByExecution(workspaceId, executionId) {
1063
+ const rows = await this.db
1064
+ .select({ n: count() })
1065
+ .from(binaryArtifacts)
1066
+ .where(and(eq(binaryArtifacts.workspace_id, workspaceId), eq(binaryArtifacts.execution_id, executionId)));
1067
+ return rows[0]?.n ?? 0;
1068
+ }
1069
+ async listByBlock(workspaceId, blockId) {
1070
+ const rows = await this.db
1071
+ .select()
1072
+ .from(binaryArtifacts)
1073
+ .where(and(eq(binaryArtifacts.workspace_id, workspaceId), eq(binaryArtifacts.block_id, blockId)))
1074
+ .orderBy(asc(binaryArtifacts.created_at), asc(binaryArtifacts.id));
1075
+ return rows.map(rowToBinaryArtifact);
1076
+ }
1077
+ async delete(workspaceId, id) {
1078
+ await this.db
1079
+ .delete(binaryArtifacts)
1080
+ .where(and(eq(binaryArtifacts.workspace_id, workspaceId), eq(binaryArtifacts.id, id)));
1081
+ }
1082
+ async listOlderThan(workspaceId, olderThan) {
1083
+ const rows = await this.db
1084
+ .select()
1085
+ .from(binaryArtifacts)
1086
+ .where(and(eq(binaryArtifacts.workspace_id, workspaceId), lt(binaryArtifacts.created_at, olderThan)));
1087
+ return rows.map(rowToBinaryArtifact);
1088
+ }
1089
+ async deleteOlderThan(workspaceId, olderThan) {
1090
+ const deleted = await this.db
1091
+ .delete(binaryArtifacts)
1092
+ .where(and(eq(binaryArtifacts.workspace_id, workspaceId), lt(binaryArtifacts.created_at, olderThan)))
1093
+ .returning({ id: binaryArtifacts.id });
1094
+ return deleted.length;
1095
+ }
1096
+ }
1006
1097
  function rowToProvisioningLog(row) {
1007
1098
  return {
1008
1099
  id: row.id,
@@ -2709,6 +2800,7 @@ export class DrizzleWorkspaceSettingsRepository {
2709
2800
  taskLimitShared: row.task_limit_shared,
2710
2801
  taskLimitPerType: perType,
2711
2802
  storeAgentContext: row.store_agent_context === 1,
2803
+ artifactRetentionDays: row.artifact_retention_days,
2712
2804
  kaizenEnabled: row.kaizen_enabled === 1,
2713
2805
  delegateAgentsToRunnerPool: row.delegate_agents_to_runner_pool === 1,
2714
2806
  delegateTestEnvToProvider: row.delegate_test_env_to_provider === 1,
@@ -2726,6 +2818,7 @@ export class DrizzleWorkspaceSettingsRepository {
2726
2818
  ? JSON.stringify(settings.taskLimitPerType)
2727
2819
  : null,
2728
2820
  store_agent_context: settings.storeAgentContext ? 1 : 0,
2821
+ artifact_retention_days: settings.artifactRetentionDays,
2729
2822
  kaizen_enabled: settings.kaizenEnabled ? 1 : 0,
2730
2823
  delegate_agents_to_runner_pool: settings.delegateAgentsToRunnerPool ? 1 : 0,
2731
2824
  delegate_test_env_to_provider: settings.delegateTestEnvToProvider ? 1 : 0,
@@ -2743,6 +2836,7 @@ export class DrizzleWorkspaceSettingsRepository {
2743
2836
  task_limit_shared: values.task_limit_shared,
2744
2837
  task_limit_per_type: values.task_limit_per_type,
2745
2838
  store_agent_context: values.store_agent_context,
2839
+ artifact_retention_days: values.artifact_retention_days,
2746
2840
  kaizen_enabled: values.kaizen_enabled,
2747
2841
  delegate_agents_to_runner_pool: values.delegate_agents_to_runner_pool,
2748
2842
  delegate_test_env_to_provider: values.delegate_test_env_to_provider,
@@ -3054,6 +3148,7 @@ export function createDrizzleRepositories(db, clock) {
3054
3148
  tokenUsageRepository: new DrizzleTokenUsageRepository(db),
3055
3149
  llmCallMetricRepository: new DrizzleLlmCallMetricRepository(db),
3056
3150
  agentContextSnapshotRepository: new DrizzleAgentContextSnapshotRepository(db),
3151
+ binaryArtifactMetadataStore: new DrizzleBinaryArtifactMetadataStore(db),
3057
3152
  agentRunRepository: new DrizzleAgentRunRepository(db),
3058
3153
  modelPresetRepository: new DrizzleModelPresetRepository(db),
3059
3154
  serviceFragmentDefaultsRepository: new DrizzleServiceFragmentDefaultsRepository(db),