create-windy 0.4.1 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +292 -125
- package/package.json +1 -1
- package/template/.windy-template.json +2 -2
- package/template/apps/agent-server/adk_web_agents/canonical_debug/agent.py +29 -9
- package/template/apps/agent-server/adk_web_agents/services.py +25 -0
- package/template/apps/agent-server/src/southwind_agent_server/adk_adapter.py +40 -11
- package/template/apps/agent-server/src/southwind_agent_server/adk_debug_sessions.py +325 -0
- package/template/apps/agent-server/src/southwind_agent_server/adk_model.py +70 -6
- package/template/apps/agent-server/src/southwind_agent_server/adk_tools.py +1 -1
- package/template/apps/agent-server/src/southwind_agent_server/app.py +16 -4
- package/template/apps/agent-server/src/southwind_agent_server/approval_resolution.py +30 -0
- package/template/apps/agent-server/src/southwind_agent_server/contracts.py +9 -0
- package/template/apps/agent-server/src/southwind_agent_server/deterministic.py +2 -1
- package/template/apps/agent-server/src/southwind_agent_server/engine.py +3 -1
- package/template/apps/agent-server/src/southwind_agent_server/openai_engine.py +95 -130
- package/template/apps/agent-server/src/southwind_agent_server/openai_engine_validation.py +133 -0
- package/template/apps/agent-server/src/southwind_agent_server/openai_provider.py +89 -20
- package/template/apps/agent-server/src/southwind_agent_server/postgres_report_insert.py +34 -0
- package/template/apps/agent-server/src/southwind_agent_server/postgres_report_queries.py +234 -0
- package/template/apps/agent-server/src/southwind_agent_server/postgres_report_replay.py +44 -0
- package/template/apps/agent-server/src/southwind_agent_server/postgres_report_status.py +83 -0
- package/template/apps/agent-server/src/southwind_agent_server/postgres_report_store.py +331 -0
- package/template/apps/agent-server/src/southwind_agent_server/postgres_report_summary.py +304 -0
- package/template/apps/agent-server/src/southwind_agent_server/report_lifecycle.py +217 -0
- package/template/apps/agent-server/src/southwind_agent_server/report_metrics.py +149 -0
- package/template/apps/agent-server/src/southwind_agent_server/report_presenter.py +280 -0
- package/template/apps/agent-server/src/southwind_agent_server/report_routes.py +126 -0
- package/template/apps/agent-server/src/southwind_agent_server/report_store.py +344 -0
- package/template/apps/agent-server/src/southwind_agent_server/report_types.py +111 -0
- package/template/apps/agent-server/src/southwind_agent_server/runtime_factory.py +31 -2
- package/template/apps/agent-server/src/southwind_agent_server/service.py +56 -51
- package/template/apps/agent-server/src/southwind_agent_server/signal_events.py +18 -1
- package/template/apps/agent-server/tests/test_adk_adapter.py +7 -9
- package/template/apps/agent-server/tests/test_adk_debug_sessions.py +215 -0
- package/template/apps/agent-server/tests/test_adk_web_canonical_debug.py +48 -1
- package/template/apps/agent-server/tests/test_openai_provider.py +58 -6
- package/template/apps/agent-server/tests/test_postgres_report_status.py +59 -0
- package/template/apps/agent-server/tests/test_postgres_report_summary.py +298 -0
- package/template/apps/agent-server/tests/test_provider_attempt_reporting.py +152 -0
- package/template/apps/agent-server/tests/test_report_attempt_lifecycle.py +126 -0
- package/template/apps/agent-server/tests/test_report_resilience.py +181 -0
- package/template/apps/agent-server/tests/test_report_store.py +286 -0
- package/template/apps/agent-server/tests/test_report_summary_parity.py +115 -0
- package/template/apps/agent-server/tests/test_report_terminal_sync.py +177 -0
- package/template/apps/server/src/agent/debug-session-facade-provider-failure.test.ts +269 -0
- package/template/apps/server/src/agent/debug-session-facade.test.ts +335 -0
- package/template/apps/server/src/agent/debug-session-facade.ts +302 -0
- package/template/apps/server/src/agent/debug-session-id.test.ts +37 -0
- package/template/apps/server/src/agent/debug-session-id.ts +37 -0
- package/template/apps/server/src/agent/debug-session-provider-reader.ts +54 -0
- package/template/apps/server/src/agent/debug-session-routes.test.ts +64 -0
- package/template/apps/server/src/agent/debug-session-routes.ts +73 -0
- package/template/apps/server/src/agent/debug-session-validation.ts +106 -0
- package/template/apps/server/src/agent/drizzle-run-binding-repository.ts +2 -0
- package/template/apps/server/src/agent/provider-input-governance.test.ts +18 -0
- package/template/apps/server/src/agent/provider-input-governance.ts +37 -1
- package/template/apps/server/src/agent/remote-runtime.test.ts +7 -0
- package/template/apps/server/src/agent/remote-runtime.ts +1 -0
- package/template/apps/server/src/agent/run-authorization.test.ts +98 -0
- package/template/apps/server/src/agent/run-authorization.ts +32 -1
- package/template/apps/server/src/agent/run-binding-repository.ts +1 -0
- package/template/apps/server/src/agent/run-contracts.ts +5 -0
- package/template/apps/server/src/agent/run-facade-adk-debug.test.ts +39 -0
- package/template/apps/server/src/agent/run-facade.test.ts +78 -0
- package/template/apps/server/src/agent/run-facade.ts +33 -3
- package/template/apps/server/src/agent/run-input-commitment.ts +50 -0
- package/template/apps/server/src/agent/run-input-verification.ts +41 -0
- package/template/apps/server/src/agent/runtime-bootstrap-report.test.ts +136 -0
- package/template/apps/server/src/agent/runtime-bootstrap.ts +66 -1
- package/template/apps/server/src/agent/tool-host.test.ts +1 -0
- package/template/apps/server/src/agent/unavailable-routes.ts +4 -0
- package/template/apps/server/src/ai-runtime-report/actor-directory.ts +94 -0
- package/template/apps/server/src/ai-runtime-report/actor-handle.test.ts +27 -0
- package/template/apps/server/src/ai-runtime-report/actor-handle.ts +81 -0
- package/template/apps/server/src/ai-runtime-report/contracts.ts +91 -0
- package/template/apps/server/src/ai-runtime-report/remote-parser.ts +212 -0
- package/template/apps/server/src/ai-runtime-report/remote-port.ts +138 -0
- package/template/apps/server/src/ai-runtime-report/routes.ts +172 -0
- package/template/apps/server/src/ai-runtime-report/scope.ts +63 -0
- package/template/apps/server/src/ai-runtime-report/service.test.ts +271 -0
- package/template/apps/server/src/ai-runtime-report/service.ts +206 -0
- package/template/apps/server/src/license/runtime-audit-reliability.test.ts +2 -0
- package/template/apps/server/src/license/runtime-service.test.ts +6 -0
- package/template/apps/server/src/module-host/ai-operation-port.test.ts +63 -0
- package/template/apps/server/src/module-host/ai-operation-port.ts +30 -5
- package/template/apps/server/src/module-host/debug-session-provider-registration.test.ts +162 -0
- package/template/apps/server/src/module-host/debug-session-provider-registration.ts +84 -0
- package/template/apps/server/src/module-host/initialize-contracts.ts +7 -0
- package/template/apps/server/src/module-host/initialize.ts +6 -197
- package/template/apps/server/src/module-host/request-context.ts +1 -0
- package/template/apps/server/src/module-host/scoped-host.ts +214 -0
- package/template/apps/server/src/module-host.ts +1 -0
- package/template/apps/server/src/runtime-adk-debug.test.ts +25 -1
- package/template/apps/server/src/runtime-adk-debug.ts +8 -2
- package/template/apps/server/src/system/operations.test.ts +54 -0
- package/template/apps/server/src/system/operations.ts +23 -1
- package/template/apps/server/src/system/seed.test.ts +4 -0
- package/template/apps/server/src/system/system-resource-monitoring.test.ts +82 -0
- package/template/apps/server/src/system/system-resource-monitoring.ts +300 -0
- package/template/apps/web/src/layout/AppSidebar.vue +6 -1
- package/template/apps/web/src/layout/AppSidebar.webtest.ts +2 -0
- package/template/apps/web/src/pages/auth/LoginPage.vue +6 -1
- package/template/apps/web/src/pages/settings/BrandingSettingsSection.vue +20 -3
- package/template/apps/web/src/pages/settings/BrandingSettingsSection.webtest.ts +80 -0
- package/template/apps/web/src/pages/system/AiRuntimePage.vue +129 -0
- package/template/apps/web/src/pages/system/SystemOperationsPage.vue +2 -1
- package/template/apps/web/src/pages/system/ai-runtime-presenter.ts +60 -0
- package/template/apps/web/src/pages/system/ai-runtime-presenter.webtest.ts +48 -0
- package/template/apps/web/src/pages/system/audit-time-range.ts +34 -2
- package/template/apps/web/src/pages/system/components/AuditLogFilters.vue +4 -3
- package/template/apps/web/src/pages/system/components/AuditLogFilters.webtest.ts +26 -1
- package/template/apps/web/src/pages/system/components/OperationGpuMonitor.vue +98 -0
- package/template/apps/web/src/pages/system/components/OperationGpuMonitor.webtest.ts +61 -0
- package/template/apps/web/src/pages/system/components/OperationServiceMonitor.vue +53 -0
- package/template/apps/web/src/pages/system/components/OperationServiceMonitor.webtest.ts +20 -1
- package/template/apps/web/src/pages/system/components/SystemResourcePage.webtest.ts +14 -1
- package/template/apps/web/src/pages/system/components/ai-runtime/AiRuntimeActivityChart.vue +85 -0
- package/template/apps/web/src/pages/system/components/ai-runtime/AiRuntimeCharts.webtest.ts +82 -0
- package/template/apps/web/src/pages/system/components/ai-runtime/AiRuntimeFilters.vue +98 -0
- package/template/apps/web/src/pages/system/components/ai-runtime/AiRuntimeFilters.webtest.ts +27 -0
- package/template/apps/web/src/pages/system/components/ai-runtime/AiRuntimeRunDetailDialog.vue +168 -0
- package/template/apps/web/src/pages/system/components/ai-runtime/AiRuntimeRunTable.vue +130 -0
- package/template/apps/web/src/pages/system/components/ai-runtime/AiRuntimeSummaryCards.vue +82 -0
- package/template/apps/web/src/pages/system/components/ai-runtime/AiRuntimeSummaryCards.webtest.ts +102 -0
- package/template/apps/web/src/pages/system/components/ai-runtime/AiRuntimeTokenByModelChart.vue +101 -0
- package/template/apps/web/src/pages/system/composables/useAiRuntimeReport.ts +185 -0
- package/template/apps/web/src/pages/system/composables/useAiRuntimeReport.webtest.ts +158 -0
- package/template/apps/web/src/pages/system/composables/useSystemResource.ts +21 -7
- package/template/apps/web/src/router/error-navigation.ts +22 -0
- package/template/apps/web/src/router/index.ts +12 -23
- package/template/apps/web/src/router/platform-admin-route-bindings.ts +4 -0
- package/template/apps/web/src/services/ai-runtime-api.ts +164 -0
- package/template/apps/web/src/services/ai-runtime-api.webtest.ts +111 -0
- package/template/apps/web/src/services/operations-api.ts +28 -0
- package/template/docker-compose.agent-debug.yml +1 -1
- package/template/docs/architecture/ai-runtime.md +2 -1
- package/template/docs/architecture/module-host.md +3 -0
- package/template/docs/platform/agent-run-attestation.md +65 -0
- package/template/docs/platform/agent-runtime-observability.md +135 -0
- package/template/docs/platform/agent-runtime.md +47 -3
- package/template/docs/platform/operations-overview.md +13 -0
- package/template/docs/platform/platform-authentication.md +4 -3
- package/template/packages/config/package.json +1 -1
- package/template/packages/database/drizzle/0045_tiny_cassandra_nova.sql +65 -0
- package/template/packages/database/drizzle/0046_tranquil_living_mummy.sql +6 -0
- package/template/packages/database/drizzle/0047_oval_luke_cage.sql +18 -0
- package/template/packages/database/drizzle/meta/0045_snapshot.json +8814 -0
- package/template/packages/database/drizzle/meta/0046_snapshot.json +8820 -0
- package/template/packages/database/drizzle/meta/0047_snapshot.json +8835 -0
- package/template/packages/database/drizzle/meta/_journal.json +21 -0
- package/template/packages/database/package.json +1 -1
- package/template/packages/database/src/agent-runtime-schema.test.ts +54 -0
- package/template/packages/database/src/schema/agent-runtime.ts +114 -0
- package/template/packages/jobs/package.json +1 -1
- package/template/packages/modules/package.json +1 -1
- package/template/packages/modules/src/system-admin-routes.ts +10 -0
- package/template/packages/modules/src/system-admin-ui-catalog.ts +1 -1
- package/template/packages/modules/src/system-api-control-plane.ts +17 -1
- package/template/packages/modules/src/system-audit-actions.ts +10 -0
- package/template/packages/modules/src/system-features.ts +8 -0
- package/template/packages/modules/src/system-module-catalog.test.ts +35 -0
- package/template/packages/modules/src/system-module-catalog.ts +15 -4
- package/template/packages/modules/src/system-modules-access.test.ts +69 -0
- package/template/packages/modules/src/system-modules.ts +13 -0
- package/template/packages/modules/src/system-permissions.ts +2 -0
- package/template/packages/server-sdk/index.ts +11 -0
- package/template/packages/server-sdk/package.json +2 -1
- package/template/packages/server-sdk/src/ai-operation-port.ts +5 -0
- package/template/packages/server-sdk/src/debug-session-provider.ts +63 -0
- package/template/packages/server-sdk/src/module-host.ts +5 -0
- package/template/packages/shared/index.ts +3 -0
- package/template/packages/shared/package.json +1 -1
- package/template/packages/shared/src/ai-runtime-report.ts +134 -0
- package/template/scripts/lib/postgres-regression.ts +201 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ModuleAiOperationHandlerRegistration } from "./ai-operation-registration.js";
|
|
2
|
+
import type { ModuleDebugSessionProviderV1 } from "./debug-session-provider.js";
|
|
2
3
|
import type { ModuleDurableHandlerDefinition } from "./durable-handler.js";
|
|
3
4
|
import type { ModuleGovernedResourceRegistration } from "./data-governance-registration.js";
|
|
4
5
|
import type { ModuleRolePreset } from "./role-preset.js";
|
|
@@ -60,6 +61,10 @@ export interface ModuleHost {
|
|
|
60
61
|
registerRolePresets(presets: readonly ModuleRolePreset[]): void;
|
|
61
62
|
/** 注册 Manifest `providers` 已声明的搜索 Provider。 */
|
|
62
63
|
registerSearchProviders(providers: readonly ModuleSearchProvider[]): void;
|
|
64
|
+
/** 注册开发期 ADK Web 使用的只读业务会话投影。 */
|
|
65
|
+
registerDebugSessionProviders(
|
|
66
|
+
providers: readonly ModuleDebugSessionProviderV1[],
|
|
67
|
+
): void;
|
|
63
68
|
}
|
|
64
69
|
|
|
65
70
|
/**
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export * from "./src/audit.js";
|
|
2
|
+
// @windy-module system.agent begin
|
|
3
|
+
export * from "./src/ai-runtime-report.js";
|
|
4
|
+
// @windy-module system.agent end
|
|
2
5
|
export * from "./src/database.js";
|
|
3
6
|
export * from "./src/data-governance.js";
|
|
4
7
|
export * from "./src/feature.js";
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { ISODateTime, PageResult } from "./primitives.js";
|
|
2
|
+
|
|
3
|
+
export type AiRuntimeReportRange = "day" | "month" | "retained";
|
|
4
|
+
export type AiRuntimeRunStatus =
|
|
5
|
+
| "accepted"
|
|
6
|
+
| "running"
|
|
7
|
+
| "awaiting-approval"
|
|
8
|
+
| "completed"
|
|
9
|
+
| "failed"
|
|
10
|
+
| "cancelled";
|
|
11
|
+
export type AiRuntimeAttemptOutcome =
|
|
12
|
+
| "running"
|
|
13
|
+
| "completed"
|
|
14
|
+
| "failed"
|
|
15
|
+
| "cancelled"
|
|
16
|
+
| "blocked"
|
|
17
|
+
| "abandoned";
|
|
18
|
+
export type AiRuntimeCacheStatus = "hit" | "miss" | "unknown";
|
|
19
|
+
export type AiRuntimeUsageSource =
|
|
20
|
+
| "provider-reported"
|
|
21
|
+
| "estimated"
|
|
22
|
+
| "unknown";
|
|
23
|
+
|
|
24
|
+
export interface AiRuntimeRunCounts {
|
|
25
|
+
total: number;
|
|
26
|
+
completed: number;
|
|
27
|
+
failed: number;
|
|
28
|
+
cancelled: number;
|
|
29
|
+
inProgress: number;
|
|
30
|
+
successRate: number | null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface AiRuntimeCacheSummary {
|
|
34
|
+
hit: number;
|
|
35
|
+
miss: number;
|
|
36
|
+
unknown: number;
|
|
37
|
+
hitRate: number | null;
|
|
38
|
+
tokenRate: number | null;
|
|
39
|
+
coverageRate: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface AiRuntimeTokenSummary {
|
|
43
|
+
input: number | null;
|
|
44
|
+
output: number | null;
|
|
45
|
+
cachedInput: number | null;
|
|
46
|
+
reasoning: number | null;
|
|
47
|
+
total: number | null;
|
|
48
|
+
coverageRate: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface AiRuntimeModelUsageSummary {
|
|
52
|
+
deploymentKey: string;
|
|
53
|
+
model: string;
|
|
54
|
+
attempts: number;
|
|
55
|
+
inputTokens: number | null;
|
|
56
|
+
outputTokens: number | null;
|
|
57
|
+
cachedInputTokens: number | null;
|
|
58
|
+
reasoningTokens: number | null;
|
|
59
|
+
totalTokens: number | null;
|
|
60
|
+
unknownUsageCount: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface AiRuntimeActivityPoint {
|
|
64
|
+
date: string;
|
|
65
|
+
runs: number;
|
|
66
|
+
completed: number;
|
|
67
|
+
failed: number;
|
|
68
|
+
totalTokens: number | null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface AiRuntimeReportSummary {
|
|
72
|
+
generatedAt: ISODateTime;
|
|
73
|
+
reportingAvailableFrom: ISODateTime | null;
|
|
74
|
+
retentionDays: number;
|
|
75
|
+
runs: AiRuntimeRunCounts;
|
|
76
|
+
cache: AiRuntimeCacheSummary;
|
|
77
|
+
tokens: AiRuntimeTokenSummary;
|
|
78
|
+
byModel: AiRuntimeModelUsageSummary[];
|
|
79
|
+
activity: AiRuntimeActivityPoint[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface AiRuntimeActorSummary {
|
|
83
|
+
handle: string;
|
|
84
|
+
displayName: string;
|
|
85
|
+
username?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface AiRuntimeRunUsage {
|
|
89
|
+
inputTokens: number | null;
|
|
90
|
+
outputTokens: number | null;
|
|
91
|
+
cachedInputTokens: number | null;
|
|
92
|
+
reasoningTokens: number | null;
|
|
93
|
+
totalTokens: number | null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface AiRuntimeRunListItem {
|
|
97
|
+
runId: string;
|
|
98
|
+
operationKey: string;
|
|
99
|
+
actor: AiRuntimeActorSummary | null;
|
|
100
|
+
status: AiRuntimeRunStatus;
|
|
101
|
+
startedAt: ISODateTime | null;
|
|
102
|
+
finishedAt: ISODateTime | null;
|
|
103
|
+
durationMs: number | null;
|
|
104
|
+
attemptCount: number;
|
|
105
|
+
models: string[];
|
|
106
|
+
cacheStatus: AiRuntimeCacheStatus;
|
|
107
|
+
usage: AiRuntimeRunUsage;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface AiRuntimeAttemptDetail extends AiRuntimeRunUsage {
|
|
111
|
+
attemptId: string;
|
|
112
|
+
sequence: number;
|
|
113
|
+
toolRound: number;
|
|
114
|
+
deploymentKey: string;
|
|
115
|
+
model: string;
|
|
116
|
+
outcome: AiRuntimeAttemptOutcome;
|
|
117
|
+
cacheStatus: AiRuntimeCacheStatus;
|
|
118
|
+
usageSource: AiRuntimeUsageSource;
|
|
119
|
+
firstTokenLatencyMs: number | null;
|
|
120
|
+
durationMs: number | null;
|
|
121
|
+
safeErrorCode: string | null;
|
|
122
|
+
startedAt: ISODateTime;
|
|
123
|
+
finishedAt: ISODateTime | null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface AiRuntimeRunDetail extends AiRuntimeRunListItem {
|
|
127
|
+
releaseId: string;
|
|
128
|
+
traceId: string;
|
|
129
|
+
safeErrorCode: string | null;
|
|
130
|
+
attempts: AiRuntimeAttemptDetail[];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export type AiRuntimeRunPage = PageResult<AiRuntimeRunListItem>;
|
|
134
|
+
export type AiRuntimeActorPage = PageResult<AiRuntimeActorSummary>;
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
|
|
3
|
+
export interface CommandOptions {
|
|
4
|
+
allowFailure?: boolean;
|
|
5
|
+
cwd?: string;
|
|
6
|
+
env?: Record<string, string | undefined>;
|
|
7
|
+
stdin?: string;
|
|
8
|
+
stdout?: "inherit" | "pipe";
|
|
9
|
+
stderr?: "inherit" | "pipe";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface CommandResult {
|
|
13
|
+
exitCode: number;
|
|
14
|
+
stdout: string;
|
|
15
|
+
stderr: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const repositoryRoot = resolve(import.meta.dir, "../..");
|
|
19
|
+
|
|
20
|
+
export async function runCommand(
|
|
21
|
+
command: string[],
|
|
22
|
+
options: CommandOptions = {},
|
|
23
|
+
): Promise<CommandResult> {
|
|
24
|
+
const child = Bun.spawn(command, {
|
|
25
|
+
cwd: options.cwd || repositoryRoot,
|
|
26
|
+
env: options.env ?? process.env,
|
|
27
|
+
stdin: options.stdin === undefined ? undefined : "pipe",
|
|
28
|
+
stdout: options.stdout || "pipe",
|
|
29
|
+
stderr: options.stderr || "pipe",
|
|
30
|
+
});
|
|
31
|
+
if (options.stdin !== undefined) {
|
|
32
|
+
child.stdin.write(options.stdin);
|
|
33
|
+
child.stdin.end();
|
|
34
|
+
}
|
|
35
|
+
const [stdout, stderr, exitCode] = await Promise.all([
|
|
36
|
+
child.stdout ? new Response(child.stdout).text() : "",
|
|
37
|
+
child.stderr ? new Response(child.stderr).text() : "",
|
|
38
|
+
child.exited,
|
|
39
|
+
]);
|
|
40
|
+
if (exitCode !== 0 && !options.allowFailure) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`${command.join(" ")} failed (${exitCode})\n${stdout}${stderr}`,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
return { exitCode, stdout: stdout.trim(), stderr: stderr.trim() };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class PostgresRegressionHarness {
|
|
49
|
+
readonly container: string;
|
|
50
|
+
private port?: string;
|
|
51
|
+
|
|
52
|
+
constructor(prefix: string) {
|
|
53
|
+
this.container = `${prefix}-${crypto.randomUUID().slice(0, 8)}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async start(): Promise<void> {
|
|
57
|
+
await runCommand([
|
|
58
|
+
"docker",
|
|
59
|
+
"run",
|
|
60
|
+
"--rm",
|
|
61
|
+
"--detach",
|
|
62
|
+
"--name",
|
|
63
|
+
this.container,
|
|
64
|
+
"--env",
|
|
65
|
+
"POSTGRES_DB=windy",
|
|
66
|
+
"--env",
|
|
67
|
+
"POSTGRES_USER=windy",
|
|
68
|
+
"--env",
|
|
69
|
+
"POSTGRES_PASSWORD=windy",
|
|
70
|
+
"--publish",
|
|
71
|
+
"127.0.0.1::5432",
|
|
72
|
+
"postgres:17-alpine",
|
|
73
|
+
]);
|
|
74
|
+
await this.waitUntilReady();
|
|
75
|
+
const portResult = await runCommand([
|
|
76
|
+
"docker",
|
|
77
|
+
"port",
|
|
78
|
+
this.container,
|
|
79
|
+
"5432/tcp",
|
|
80
|
+
]);
|
|
81
|
+
this.port = portResult.stdout.match(/:(\d+)$/)?.[1];
|
|
82
|
+
if (!this.port) {
|
|
83
|
+
throw new Error(`无法解析 PostgreSQL 端口:${portResult.stdout}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
databaseUrl(database = "windy"): string {
|
|
88
|
+
if (!this.port) throw new Error("PostgreSQL 容器尚未启动");
|
|
89
|
+
const url = new URL(`postgresql://127.0.0.1:${this.port}/${database}`);
|
|
90
|
+
url.username = "windy";
|
|
91
|
+
url.password = "windy";
|
|
92
|
+
return url.toString();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async createDatabase(database: string): Promise<void> {
|
|
96
|
+
await this.execute(`CREATE DATABASE ${quoteIdentifier(database)}`, "windy");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async execute(sql: string, database = "windy"): Promise<void> {
|
|
100
|
+
await this.psql(sql, database);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async query(sql: string, database = "windy"): Promise<string> {
|
|
104
|
+
return (await this.psql(sql, database)).stdout;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async migrate(
|
|
108
|
+
database = "windy",
|
|
109
|
+
allowFailure = false,
|
|
110
|
+
): Promise<CommandResult> {
|
|
111
|
+
return runCommand(["bun", "run", "db:migrate"], {
|
|
112
|
+
allowFailure,
|
|
113
|
+
env: { ...process.env, DATABASE_URL: this.databaseUrl(database) },
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async close(): Promise<void> {
|
|
118
|
+
await runCommand(["docker", "rm", "--force", this.container], {
|
|
119
|
+
allowFailure: true,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private async psql(sql: string, database: string): Promise<CommandResult> {
|
|
124
|
+
return runCommand(
|
|
125
|
+
[
|
|
126
|
+
"docker",
|
|
127
|
+
"exec",
|
|
128
|
+
"--interactive",
|
|
129
|
+
this.container,
|
|
130
|
+
"psql",
|
|
131
|
+
"--set",
|
|
132
|
+
"ON_ERROR_STOP=1",
|
|
133
|
+
"--username",
|
|
134
|
+
"windy",
|
|
135
|
+
"--dbname",
|
|
136
|
+
database,
|
|
137
|
+
"--tuples-only",
|
|
138
|
+
"--no-align",
|
|
139
|
+
],
|
|
140
|
+
{ stdin: sql },
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private async waitUntilReady(): Promise<void> {
|
|
145
|
+
for (let attempt = 0; attempt < 60; attempt += 1) {
|
|
146
|
+
const [result, logs] = await Promise.all([
|
|
147
|
+
runCommand(
|
|
148
|
+
[
|
|
149
|
+
"docker",
|
|
150
|
+
"exec",
|
|
151
|
+
this.container,
|
|
152
|
+
"pg_isready",
|
|
153
|
+
"--username",
|
|
154
|
+
"windy",
|
|
155
|
+
"--dbname",
|
|
156
|
+
"windy",
|
|
157
|
+
],
|
|
158
|
+
{ allowFailure: true },
|
|
159
|
+
),
|
|
160
|
+
runCommand(["docker", "logs", this.container], {
|
|
161
|
+
allowFailure: true,
|
|
162
|
+
}),
|
|
163
|
+
]);
|
|
164
|
+
if (
|
|
165
|
+
result.exitCode === 0 &&
|
|
166
|
+
`${logs.stdout}\n${logs.stderr}`.includes(
|
|
167
|
+
"PostgreSQL init process complete; ready for start up.",
|
|
168
|
+
)
|
|
169
|
+
) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
await Bun.sleep(500);
|
|
173
|
+
}
|
|
174
|
+
throw new Error("PostgreSQL 未在 30 秒内就绪");
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function expectEqual(
|
|
179
|
+
actual: string,
|
|
180
|
+
expected: string,
|
|
181
|
+
label: string,
|
|
182
|
+
): void {
|
|
183
|
+
if (actual !== expected) {
|
|
184
|
+
throw new Error(`${label}:期望 ${expected},实际 ${actual}`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function expect(condition: unknown, label: string): asserts condition {
|
|
189
|
+
if (!condition) throw new Error(label);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function sqlLiteral(value: string): string {
|
|
193
|
+
return `'${value.replaceAll("'", "''")}'`;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function quoteIdentifier(value: string): string {
|
|
197
|
+
if (!/^[a-z][a-z0-9_]*$/.test(value)) {
|
|
198
|
+
throw new Error(`不安全的数据库标识符:${value}`);
|
|
199
|
+
}
|
|
200
|
+
return `"${value}"`;
|
|
201
|
+
}
|