target-supabase-sdk 0.2.3 → 0.2.4

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 (155) hide show
  1. package/README.md +24 -18
  2. package/dist/browser.d.ts +11 -4
  3. package/dist/browser.d.ts.map +1 -1
  4. package/dist/browser.js +3 -3
  5. package/dist/chunks/{enable-log-persist-C7GJo_Nc.js → enable-log-persist-B8rg_wXG.js} +5 -6
  6. package/dist/chunks/enable-log-persist-B8rg_wXG.js.map +1 -0
  7. package/dist/chunks/{create-logger-DyH3mZ8p.js → list.api-Ab9v8yOg.js} +66 -57
  8. package/dist/chunks/list.api-Ab9v8yOg.js.map +1 -0
  9. package/dist/chunks/{word.interface-DLgRobKV.js → word.interface-B_fbVFOW.js} +1078 -97
  10. package/dist/chunks/word.interface-B_fbVFOW.js.map +1 -0
  11. package/dist/extraction/extraction.api.d.ts +117 -0
  12. package/dist/extraction/extraction.api.d.ts.map +1 -0
  13. package/dist/extraction/extraction.interface.d.ts +43 -0
  14. package/dist/extraction/extraction.interface.d.ts.map +1 -0
  15. package/dist/list/list.interface.d.ts +2 -2
  16. package/dist/list/list.interface.d.ts.map +1 -1
  17. package/dist/node/node-runtime.base.d.ts +13 -1
  18. package/dist/node/node-runtime.base.d.ts.map +1 -1
  19. package/dist/node/process/create-managed-child-processes.d.ts +12 -0
  20. package/dist/node/process/create-managed-child-processes.d.ts.map +1 -0
  21. package/dist/node/process/index.d.ts +3 -1
  22. package/dist/node/process/index.d.ts.map +1 -1
  23. package/dist/node/process/managed-child-processes.d.ts +15 -2
  24. package/dist/node/process/managed-child-processes.d.ts.map +1 -1
  25. package/dist/node/runtime-state/create-service-runtime-state-store.d.ts +23 -0
  26. package/dist/node/runtime-state/create-service-runtime-state-store.d.ts.map +1 -0
  27. package/dist/node/runtime-state/index.d.ts +4 -0
  28. package/dist/node/runtime-state/index.d.ts.map +1 -0
  29. package/dist/node/runtime-state/service-runtime-state.types.d.ts +57 -0
  30. package/dist/node/runtime-state/service-runtime-state.types.d.ts.map +1 -0
  31. package/dist/node/service-guard/index.d.ts +9 -0
  32. package/dist/node/service-guard/index.d.ts.map +1 -0
  33. package/dist/node/service-guard/register-service-guard-runner.d.ts +9 -0
  34. package/dist/node/service-guard/register-service-guard-runner.d.ts.map +1 -0
  35. package/dist/node/service-guard/run-readiness-gate.d.ts +11 -0
  36. package/dist/node/service-guard/run-readiness-gate.d.ts.map +1 -0
  37. package/dist/node/service-guard/run-service-guard-tick.d.ts +7 -0
  38. package/dist/node/service-guard/run-service-guard-tick.d.ts.map +1 -0
  39. package/dist/node/service-guard/service-guard-node.d.ts +18 -0
  40. package/dist/node/service-guard/service-guard-node.d.ts.map +1 -0
  41. package/dist/node/service-guard/service-guard.interface.d.ts +58 -0
  42. package/dist/node/service-guard/service-guard.interface.d.ts.map +1 -0
  43. package/dist/node/service-guard/worker-spawn-cooldown.d.ts +4 -0
  44. package/dist/node/service-guard/worker-spawn-cooldown.d.ts.map +1 -0
  45. package/dist/node/service-host/index.d.ts +9 -0
  46. package/dist/node/service-host/index.d.ts.map +1 -0
  47. package/dist/node/service-host/registry-slot-guard-step.d.ts +21 -0
  48. package/dist/node/service-host/registry-slot-guard-step.d.ts.map +1 -0
  49. package/dist/node/service-host/service-host.d.ts +36 -0
  50. package/dist/node/service-host/service-host.d.ts.map +1 -0
  51. package/dist/node/service-host/single-process-service.d.ts +21 -0
  52. package/dist/node/service-host/single-process-service.d.ts.map +1 -0
  53. package/dist/node.d.ts +15 -2
  54. package/dist/node.d.ts.map +1 -1
  55. package/dist/node.js +2088 -1495
  56. package/dist/node.js.map +1 -1
  57. package/dist/preload.js +3 -3
  58. package/dist/service/config.api.d.ts +143 -0
  59. package/dist/service/config.api.d.ts.map +1 -0
  60. package/dist/service/config.interface.d.ts +22 -0
  61. package/dist/service/config.interface.d.ts.map +1 -0
  62. package/dist/service/index.d.ts +16 -17
  63. package/dist/service/index.d.ts.map +1 -1
  64. package/dist/service/registry-lifecycle.d.ts +43 -0
  65. package/dist/service/registry-lifecycle.d.ts.map +1 -0
  66. package/dist/service/registry.service.d.ts +94 -0
  67. package/dist/service/registry.service.d.ts.map +1 -0
  68. package/dist/service/service-bootstrap.d.ts +28 -0
  69. package/dist/service/service-bootstrap.d.ts.map +1 -0
  70. package/dist/service/service.api.d.ts +160 -0
  71. package/dist/service/service.api.d.ts.map +1 -1
  72. package/dist/service/service.interface.d.ts +32 -1
  73. package/dist/service/service.interface.d.ts.map +1 -1
  74. package/dist/shared/log/index.d.ts +4 -0
  75. package/dist/shared/log/index.d.ts.map +1 -1
  76. package/dist/shared/log/log-batch.d.ts +44 -0
  77. package/dist/shared/log/log-batch.d.ts.map +1 -0
  78. package/dist/shared/log/log-persist-coordinator.d.ts +37 -0
  79. package/dist/shared/log/log-persist-coordinator.d.ts.map +1 -0
  80. package/dist/shared/log/log-persist-flush.d.ts.map +1 -1
  81. package/dist/shared/log/log-persist.config.d.ts +0 -1
  82. package/dist/shared/log/log-persist.config.d.ts.map +1 -1
  83. package/dist/shared/log/log-persist.interface.d.ts +8 -0
  84. package/dist/shared/log/log-persist.interface.d.ts.map +1 -1
  85. package/dist/shared/utils/config-path.utils.d.ts +21 -0
  86. package/dist/shared/utils/config-path.utils.d.ts.map +1 -1
  87. package/dist/shared/utils/get-value-at-path.d.ts +3 -0
  88. package/dist/shared/utils/get-value-at-path.d.ts.map +1 -0
  89. package/dist/task/index.d.ts +2 -0
  90. package/dist/task/index.d.ts.map +1 -1
  91. package/dist/task/task-manager.d.ts +1 -1
  92. package/dist/task/task-manager.d.ts.map +1 -1
  93. package/dist/task/task-post-retry.api.d.ts +24 -0
  94. package/dist/task/task-post-retry.api.d.ts.map +1 -0
  95. package/dist/task/task-repo-context.d.ts +7 -1
  96. package/dist/task/task-repo-context.d.ts.map +1 -1
  97. package/dist/trigger/trigger-manager.d.ts +1 -1
  98. package/dist/trigger/trigger-manager.d.ts.map +1 -1
  99. package/dist/trigger/trigger-node.d.ts +1 -0
  100. package/dist/trigger/trigger-node.d.ts.map +1 -1
  101. package/dist/trigger/trigger.constant.d.ts +1 -1
  102. package/dist/trigger/trigger.constant.d.ts.map +1 -1
  103. package/dist/trigger/trigger.interface.d.ts +10 -0
  104. package/dist/trigger/trigger.interface.d.ts.map +1 -1
  105. package/package.json +4 -2
  106. package/src/browser.ts +98 -4
  107. package/src/extraction/extraction.api.ts +232 -0
  108. package/src/extraction/extraction.interface.ts +45 -0
  109. package/src/list/list.interface.ts +2 -2
  110. package/src/node/node-runtime.base.ts +37 -6
  111. package/src/node/process/create-managed-child-processes.ts +27 -0
  112. package/src/node/process/index.ts +4 -0
  113. package/src/node/process/managed-child-processes.ts +48 -15
  114. package/src/node/runtime-state/create-service-runtime-state-store.ts +125 -0
  115. package/src/node/runtime-state/index.ts +17 -0
  116. package/src/node/runtime-state/service-runtime-state.types.ts +64 -0
  117. package/src/node/service-guard/index.ts +17 -0
  118. package/src/node/service-guard/register-service-guard-runner.ts +64 -0
  119. package/src/node/service-guard/run-readiness-gate.ts +34 -0
  120. package/src/node/service-guard/run-service-guard-tick.ts +106 -0
  121. package/src/node/service-guard/service-guard-node.ts +52 -0
  122. package/src/node/service-guard/service-guard.interface.ts +64 -0
  123. package/src/node/service-guard/worker-spawn-cooldown.ts +13 -0
  124. package/src/node/service-host/index.ts +30 -0
  125. package/src/node/service-host/registry-slot-guard-step.ts +57 -0
  126. package/src/node/service-host/service-host.ts +168 -0
  127. package/src/node/service-host/single-process-service.ts +58 -0
  128. package/src/node.ts +69 -0
  129. package/src/service/config.api.ts +224 -0
  130. package/src/service/config.interface.ts +25 -0
  131. package/src/service/index.ts +74 -149
  132. package/src/service/registry-lifecycle.ts +144 -0
  133. package/src/service/registry.service.ts +564 -0
  134. package/src/service/service-bootstrap.ts +74 -0
  135. package/src/service/service.api.ts +13 -0
  136. package/src/service/service.interface.ts +36 -1
  137. package/src/shared/log/index.ts +13 -0
  138. package/src/shared/log/log-batch.ts +156 -0
  139. package/src/shared/log/log-persist-coordinator.ts +107 -0
  140. package/src/shared/log/log-persist-flush.ts +3 -3
  141. package/src/shared/log/log-persist.config.ts +0 -2
  142. package/src/shared/log/log-persist.interface.ts +10 -0
  143. package/src/shared/utils/config-path.utils.ts +35 -0
  144. package/src/shared/utils/get-value-at-path.ts +17 -0
  145. package/src/task/index.ts +2 -0
  146. package/src/task/task-manager.ts +10 -4
  147. package/src/task/task-post-retry.api.ts +61 -0
  148. package/src/task/task-repo-context.ts +8 -1
  149. package/src/trigger/trigger-manager.ts +4 -2
  150. package/src/trigger/trigger-node.ts +10 -4
  151. package/src/trigger/trigger.constant.ts +1 -1
  152. package/src/trigger/trigger.interface.ts +10 -0
  153. package/dist/chunks/create-logger-DyH3mZ8p.js.map +0 -1
  154. package/dist/chunks/enable-log-persist-C7GJo_Nc.js.map +0 -1
  155. package/dist/chunks/word.interface-DLgRobKV.js.map +0 -1
package/src/browser.ts CHANGED
@@ -9,6 +9,24 @@ export type { SupabaseClient } from "@supabase/supabase-js";
9
9
  export * from "./core.api";
10
10
  export * from "./core.interface";
11
11
  export * from "./core.utils";
12
+ export type {
13
+ GetExtractionPayload,
14
+ PatchExtractionObjectsPayload,
15
+ PostExtractionPayload,
16
+ ScanExtractionListPayload,
17
+ } from "./extraction/extraction.api";
18
+ export {
19
+ getExtraction,
20
+ getExtractionSchema,
21
+ patchExtractionObjects,
22
+ patchExtractionObjectsSchema,
23
+ postExtraction,
24
+ postExtractionSchema,
25
+ scanExtractionList,
26
+ scanExtractionListSchema,
27
+ } from "./extraction/extraction.api";
28
+ export type { Extraction, ExtractionDetails } from "./extraction/extraction.interface";
29
+ export { CategoryExtraction } from "./extraction/extraction.interface";
12
30
  export * from "./idea/idea.interface";
13
31
  export * from "./link/link.api";
14
32
  export * from "./link/link.interface";
@@ -86,32 +104,84 @@ export * from "./repo/repo.interface";
86
104
  export type {
87
105
  Api,
88
106
  ApiDetails,
89
- DiscoverServiceInput,
107
+ ClaimRegistrySlotInput,
108
+ Config,
109
+ ConfigDetails,
90
110
  GetApiPayload,
111
+ GetConfigPayload,
91
112
  GetServicePayload,
113
+ PatchServiceRuntimeInput,
92
114
  PostApiPayload,
115
+ PostServiceInstanceOptions,
93
116
  PostServicePayload,
117
+ PostSystemRegistryConfigPayload,
118
+ RegisterServiceInput,
119
+ RegistrySlotGuardResult,
120
+ RegistrySlotRuntimeState,
121
+ ResetSystemRegistryConfigPayload,
94
122
  Service,
123
+ ServiceBootstrapResult,
95
124
  ServiceDetails,
125
+ ServiceNodeSnapshot,
126
+ ServiceRegistrySession,
127
+ ServiceRuntime,
128
+ ServiceSlot,
129
+ SystemRegistrySeedSlot,
130
+ TargetSystemRegistrySlotView,
131
+ TargetSystemRegistryView,
96
132
  } from "./service/index";
97
133
  export {
98
134
  ApiMethod,
99
135
  apiDetailsSchema,
136
+ assertRegistrySlotAvailable,
137
+ assertRegistrySlotOwner,
138
+ buildEmptyServiceSlots,
139
+ buildSystemRegistryConfigDetails,
140
+ CategoryConfig,
100
141
  CategoryService,
101
- discoverService,
142
+ claimServiceRegistrySlot,
143
+ createActiveServiceLifecycle,
144
+ createClaimedRegistrySlotRuntimeState,
145
+ DEFAULT_SYSTEM_REGISTRY_SEED_SLOTS,
146
+ defaultL3ServiceDetails,
147
+ EMPTY_REGISTRY_SLOT_RUNTIME_STATE,
102
148
  fieldDefinitionSchema,
103
149
  getApi,
104
150
  getApiSchema,
151
+ getConfig,
152
+ getConfigSchema,
105
153
  getService,
106
154
  getServiceSchema,
155
+ getTargetSystemRegistry,
156
+ parseServiceSlot,
157
+ parseServiceSlots,
158
+ patchServiceRuntime,
107
159
  postApi,
108
160
  postApiSchema,
109
161
  postService,
162
+ postServiceInstance,
110
163
  postServiceSchema,
164
+ postSystemRegistryConfig,
165
+ postSystemRegistryConfigSchema,
166
+ registerService,
167
+ registerServiceAtStartup,
168
+ registrySlotRuntimePatchFromGuardResult,
169
+ resetSystemRegistryConfig,
170
+ resetSystemRegistryConfigSchema,
171
+ resolveActiveRegistryServiceId,
172
+ runRegistrySlotGuardCheck,
111
173
  ServiceLifecycleStatus,
174
+ ServiceRegistryError,
175
+ ServiceSlotStatus,
112
176
  schemaDefinitionSchema,
113
177
  serviceDetailsSchema,
114
178
  serviceLifecycleSchema,
179
+ serviceNodeSnapshotSchema,
180
+ serviceRuntimeSchema,
181
+ systemRegistrySeedSlotSchema,
182
+ TARGET_SYSTEM_REGISTRY_KEY,
183
+ unregisterService,
184
+ unregisterServiceAtShutdown,
115
185
  } from "./service/index";
116
186
  export type { FetchInitFactory, FetchRetryOptions } from "./shared/http/fetch-retry";
117
187
  export {
@@ -120,9 +190,31 @@ export {
120
190
  isRetryableHttpStatus,
121
191
  requestHasBody,
122
192
  } from "./shared/http/fetch-retry";
123
- export type { CreateLoggerInput, LoggerWithScope, LogScope, LogScopePatch } from "./shared/log";
124
- export { createLogger, LogLevel, logManager } from "./shared/log";
193
+ export type {
194
+ CreateLoggerInput,
195
+ DecodedLogBatch,
196
+ FlatLogEntry,
197
+ LogEntry,
198
+ LoggerWithScope,
199
+ LogScope,
200
+ LogScopePatch,
201
+ } from "./shared/log";
202
+ export {
203
+ createLogger,
204
+ decodeLogBatch,
205
+ decodeLogBatchList,
206
+ flattenLogBatch,
207
+ flattenLogBatches,
208
+ isLogBatchList,
209
+ isLogEntry,
210
+ LOG_BATCH_LOADER_KEY,
211
+ LOG_PERSIST_LOADER_KEY,
212
+ LogLevel,
213
+ logManager,
214
+ parseLogBatchMeta,
215
+ } from "./shared/log";
125
216
  export { isHttpUrl, resolveFetchUrl } from "./shared/utils/fetch-url";
217
+ export { getValueAtPath } from "./shared/utils/get-value-at-path";
126
218
  export type {
127
219
  ClassifiedNetworkError,
128
220
  FormatNetworkErrorOptions,
@@ -160,6 +252,8 @@ export {
160
252
  } from "./task/task.interface";
161
253
  export type { PostTaskPayload } from "./task/task-post.api";
162
254
  export { postTask, postTaskSchema } from "./task/task-post.api";
255
+ export type { PostTaskWithRetryOptions, PostTaskWithRetryResult } from "./task/task-post-retry.api";
256
+ export { POST_TASK_DEFAULT_MAX_ATTEMPTS, postTaskWithRetry } from "./task/task-post-retry.api";
163
257
  export type { TaskStatusCount } from "./task/task-queue";
164
258
  export { countTasksByType, summarizeTaskQueue } from "./task/task-queue";
165
259
  export * from "./word/word.interface";
@@ -0,0 +1,232 @@
1
+ import { z } from "zod";
2
+ import {
3
+ createTarget,
4
+ getPossibleTarget,
5
+ getTarget,
6
+ isCreateTargetAlreadyExistsError,
7
+ type QueryFilter,
8
+ scanTargetList,
9
+ updateTargetDetails,
10
+ validateWithSchema,
11
+ } from "../core.api";
12
+ import type { SupabaseResponse } from "../core.interface";
13
+ import { generateResponse } from "../core.interface";
14
+ import { createLogger } from "../shared/log";
15
+ import { CategoryExtraction, type Extraction, type ExtractionDetails } from "./extraction.interface";
16
+
17
+ const targetIdSchema = z.string().trim().min(1);
18
+ const loaderKeySchema = z.string().trim().min(1);
19
+
20
+ export const postExtractionSchema = z.object({
21
+ name: z.string().trim().min(1),
22
+ /** Source Target id — for log analysis this is the producing Task id. */
23
+ sourceId: targetIdSchema,
24
+ loaderKey: loaderKeySchema,
25
+ meta: z.unknown().optional(),
26
+ objects: z.array(z.unknown()).min(1),
27
+ tagList: z.array(z.string()).optional().default([]),
28
+ extra: z.string().optional(),
29
+ });
30
+
31
+ export type PostExtractionPayload = z.infer<typeof postExtractionSchema>;
32
+
33
+ export const getExtractionSchema = z
34
+ .object({
35
+ id: targetIdSchema.optional(),
36
+ sourceId: targetIdSchema.optional(),
37
+ loaderKey: loaderKeySchema.optional(),
38
+ })
39
+ .refine(
40
+ (payload) => {
41
+ const hasId = payload.id != null && payload.id !== "";
42
+ const hasSource = payload.sourceId != null && payload.sourceId !== "";
43
+ if (hasId) {
44
+ return !hasSource;
45
+ }
46
+ return hasSource && payload.loaderKey != null && payload.loaderKey !== "";
47
+ },
48
+ { message: "Provide id, or both sourceId and loaderKey" },
49
+ );
50
+
51
+ export type GetExtractionPayload = z.infer<typeof getExtractionSchema>;
52
+
53
+ export const scanExtractionListSchema = z.object({
54
+ loaderKey: loaderKeySchema.optional(),
55
+ sourceId: targetIdSchema.optional(),
56
+ maxRows: z.number().int().min(1).max(500).optional(),
57
+ ascending: z.boolean().optional(),
58
+ });
59
+
60
+ export type ScanExtractionListPayload = z.infer<typeof scanExtractionListSchema>;
61
+
62
+ export const patchExtractionObjectsSchema = z.object({
63
+ id: targetIdSchema,
64
+ objects: z.array(z.unknown()).min(1),
65
+ meta: z.unknown().optional(),
66
+ expectedRevision: z.number().int().min(0).optional(),
67
+ });
68
+
69
+ export type PatchExtractionObjectsPayload = z.infer<typeof patchExtractionObjectsSchema>;
70
+
71
+ const extractionCategoryFilter: QueryFilter = {
72
+ field: "category",
73
+ operator: "eq",
74
+ value: CategoryExtraction.EXTRACTION,
75
+ };
76
+
77
+ function extractionRedundancyFilters(sourceId: string, loaderKey: string): QueryFilter[] {
78
+ return [
79
+ extractionCategoryFilter,
80
+ { field: "value", operator: "eq", value: sourceId },
81
+ { field: "details->>loaderKey", operator: "eq", value: loaderKey },
82
+ ];
83
+ }
84
+
85
+ function normalizeExtractionMeta(meta: unknown, patch?: unknown): Record<string, unknown> {
86
+ const base =
87
+ meta != null && typeof meta === "object" && !Array.isArray(meta)
88
+ ? { ...(meta as Record<string, unknown>) }
89
+ : {};
90
+ if (patch != null && typeof patch === "object" && !Array.isArray(patch)) {
91
+ Object.assign(base, patch as Record<string, unknown>);
92
+ }
93
+ const revision = typeof base.revision === "number" && Number.isInteger(base.revision) ? base.revision : 0;
94
+ base.revision = revision;
95
+ base.updatedAt = new Date().toISOString();
96
+ return base;
97
+ }
98
+
99
+ /** Create an Extraction row for `(sourceId, loaderKey)`. Idempotent on redundancy key. */
100
+ export const postExtraction = validateWithSchema(
101
+ postExtractionSchema,
102
+ "postExtractionSchema",
103
+ )(async (payload): Promise<SupabaseResponse<Extraction>> => {
104
+ const logger = createLogger({ module: "postExtraction" });
105
+ const meta = normalizeExtractionMeta(payload.meta, { revision: 0 });
106
+
107
+ try {
108
+ return await createTarget<Extraction, PostExtractionPayload>({
109
+ payload,
110
+ checkRedundancyFilterList: extractionRedundancyFilters(payload.sourceId, payload.loaderKey),
111
+ createFn: () => ({
112
+ name: payload.name,
113
+ value: payload.sourceId,
114
+ category: CategoryExtraction.EXTRACTION,
115
+ tagList: payload.tagList,
116
+ extra: payload.extra,
117
+ details: {
118
+ manifestVersion: 0,
119
+ loaderKey: payload.loaderKey,
120
+ meta,
121
+ objects: payload.objects,
122
+ },
123
+ }),
124
+ });
125
+ } catch (error) {
126
+ if (isCreateTargetAlreadyExistsError(error)) {
127
+ logger.warn("Extraction already exists for source + loaderKey", {
128
+ topic: "extraction",
129
+ data: { sourceId: payload.sourceId, loaderKey: payload.loaderKey },
130
+ });
131
+ throw error;
132
+ }
133
+ throw error;
134
+ }
135
+ });
136
+
137
+ /** Fetch one Extraction by row id, or by `(sourceId, loaderKey)`. */
138
+ export const getExtraction = validateWithSchema(
139
+ getExtractionSchema,
140
+ "getExtractionSchema",
141
+ )(async (payload): Promise<SupabaseResponse<Extraction>> => {
142
+ if (payload.id != null && payload.id !== "") {
143
+ const result = await getTarget({
144
+ id: payload.id,
145
+ filterList: [extractionCategoryFilter],
146
+ });
147
+ return {
148
+ ...result,
149
+ data: result.data as Extraction | undefined,
150
+ };
151
+ }
152
+
153
+ const result = await getPossibleTarget({
154
+ filterList: extractionRedundancyFilters(payload.sourceId!, payload.loaderKey!),
155
+ });
156
+
157
+ if (result.data == null) {
158
+ return generateResponse.error("Extraction not found", undefined, "NOT_FOUND") as SupabaseResponse<Extraction>;
159
+ }
160
+
161
+ return {
162
+ ...result,
163
+ data: result.data as Extraction,
164
+ };
165
+ });
166
+
167
+ /** Scan Extraction rows (newest first by default). */
168
+ export const scanExtractionList = validateWithSchema(
169
+ scanExtractionListSchema,
170
+ "scanExtractionListSchema",
171
+ )(async (payload) => {
172
+ const filterList: QueryFilter[] = [extractionCategoryFilter];
173
+ if (payload.loaderKey != null) {
174
+ filterList.push({ field: "details->>loaderKey", operator: "eq", value: payload.loaderKey });
175
+ }
176
+ if (payload.sourceId != null) {
177
+ filterList.push({ field: "value", operator: "eq", value: payload.sourceId });
178
+ }
179
+
180
+ return scanTargetList<Extraction>({
181
+ category: CategoryExtraction.EXTRACTION,
182
+ filterList,
183
+ maxRows: payload.maxRows ?? 100,
184
+ orderBy: { field: "created_at", ascending: payload.ascending ?? false },
185
+ });
186
+ });
187
+
188
+ /** Replace {@link ExtractionDetails.objects} and bump `meta.revision`. */
189
+ export const patchExtractionObjects = validateWithSchema(
190
+ patchExtractionObjectsSchema,
191
+ "patchExtractionObjectsSchema",
192
+ )(async (payload): Promise<SupabaseResponse<Extraction>> => {
193
+ const optimisticLockFilterList: QueryFilter[] = [];
194
+ if (payload.expectedRevision != null) {
195
+ optimisticLockFilterList.push({
196
+ field: "details->meta->>revision",
197
+ operator: "eq",
198
+ value: String(payload.expectedRevision),
199
+ });
200
+ }
201
+
202
+ try {
203
+ const data = await updateTargetDetails<Extraction, ExtractionDetails>({
204
+ id: payload.id,
205
+ optimisticLockFilterList,
206
+ updateFn: (current) => {
207
+ const currentMeta =
208
+ current.meta != null && typeof current.meta === "object" && !Array.isArray(current.meta)
209
+ ? (current.meta as Record<string, unknown>)
210
+ : {};
211
+ const revision =
212
+ typeof currentMeta.revision === "number" && Number.isInteger(currentMeta.revision)
213
+ ? currentMeta.revision + 1
214
+ : 1;
215
+ return {
216
+ ...current,
217
+ meta: normalizeExtractionMeta(current.meta, {
218
+ ...(payload.meta != null && typeof payload.meta === "object" && !Array.isArray(payload.meta)
219
+ ? payload.meta
220
+ : {}),
221
+ revision,
222
+ }),
223
+ objects: payload.objects,
224
+ };
225
+ },
226
+ });
227
+ return generateResponse.success(data as Extraction);
228
+ } catch (error) {
229
+ const message = error instanceof Error ? error.message : String(error);
230
+ return generateResponse.error(message, undefined, "PATCH_FAILED") as SupabaseResponse<Extraction>;
231
+ }
232
+ });
@@ -0,0 +1,45 @@
1
+ import type { Target } from "../core.interface";
2
+
3
+ /**
4
+ * Interpretation layer for a source Target: pipeline output (systems, models, algorithms)
5
+ * materialized as structured data. Not a process (see Task) or raw bytes (see Parcel).
6
+ *
7
+ * @see `.cursor/skills/extraction/SKILL.md`
8
+ */
9
+ export interface Extraction extends Target {
10
+ /** Human-readable label. */
11
+ name: string;
12
+ /**
13
+ * Source Target id — the single input this row interprets (any category).
14
+ * Immutable after create. Indexed for lookup: all Extractions where `value = sourceId`.
15
+ * Not a dedup hash or pipeline run id.
16
+ */
17
+ value: string;
18
+ category: CategoryExtraction;
19
+ details: ExtractionDetails;
20
+ }
21
+
22
+ export enum CategoryExtraction {
23
+ EXTRACTION = "extraction",
24
+ }
25
+
26
+ export interface ExtractionDetails {
27
+ manifestVersion: 0;
28
+ /**
29
+ * Extractor identifier. Together with {@link Extraction.value} (source id) forms the
30
+ * logical unique key: one Extraction row per `(sourceTargetId, loaderKey)`.
31
+ * Determines how to parse and render {@link objects} (Extractor-owned schema).
32
+ */
33
+ loaderKey: string;
34
+ /**
35
+ * Extractor-owned metadata for this interpretation (e.g. revision, updatedAt, pipeline).
36
+ * Updated alongside {@link objects}; kept `unknown` at SDK boundary — see Extractor module.
37
+ */
38
+ meta: unknown;
39
+ /**
40
+ * Latest mutable analysis results for this source + Extractor — not an immutable snapshot.
41
+ * Shape is defined by the Extractor for {@link loaderKey}; SDK keeps `unknown[]` at core.
42
+ * Large payloads should reference Parcel (or similar) rather than inline in this array.
43
+ */
44
+ objects: Array<unknown>;
45
+ }
@@ -13,8 +13,8 @@ export interface List extends Target {
13
13
 
14
14
  export interface ListDetails {
15
15
  manifestVersion: number;
16
- /** If string, it is the meta brief of the list, TODO */
17
- meta: string;
16
+ /** Optional list metadata (shape depends on `loaderKey`). */
17
+ meta: unknown;
18
18
  /** If string, it is the url of the preview TODO */
19
19
  preview: string;
20
20
  /** How to render the List */
@@ -21,6 +21,16 @@ export interface NodeLoopContext {
21
21
  nodeId: string;
22
22
  }
23
23
 
24
+ /** Shared options for {@link BaseNodeRuntime} and subclasses (e.g. TriggerNode). */
25
+ export interface BaseNodeRuntimeOptions {
26
+ /**
27
+ * Called after node logout (or when the node was never registered),
28
+ * immediately before `process.exit`. Use for best-effort cleanup such as
29
+ * {@link unregisterServiceAtShutdown}. Errors are logged and ignored.
30
+ */
31
+ beforeProcessExit?: () => void | Promise<void>;
32
+ }
33
+
24
34
  export const LOG_TOPIC_NODE = "node";
25
35
  const LOG_TOPIC_COMMAND = "command";
26
36
  const LOG_TOPIC_PROCESS = "process";
@@ -35,6 +45,7 @@ abstract class BaseNodeRuntime {
35
45
  private static readonly NODE_ID_PENDING_ASSIGNMENT = "pending-node-id";
36
46
 
37
47
  protected readonly runtimeModule: string;
48
+ private readonly beforeProcessExitHook: (() => void | Promise<void>) | null;
38
49
 
39
50
  private _localNodeId: string | null = null;
40
51
  private _isNodeIdLocked = false;
@@ -63,14 +74,34 @@ abstract class BaseNodeRuntime {
63
74
  this._isNodeIdLocked = true;
64
75
  }
65
76
 
66
- protected constructor(runtimeModule: string) {
77
+ protected constructor(runtimeModule: string, options?: BaseNodeRuntimeOptions) {
67
78
  this.runtimeModule = runtimeModule;
79
+ this.beforeProcessExitHook = options?.beforeProcessExit ?? null;
68
80
  this.isRunning = false;
69
81
  this.loopCount = 0;
70
82
  this.startupTraceId = logManager.generateTraceId();
71
83
  this.startupScope = createScope({ module: runtimeModule, traceId: this.startupTraceId });
72
84
  }
73
85
 
86
+ private async runBeforeProcessExit(logger: LoggerWithScope): Promise<void> {
87
+ if (this.beforeProcessExitHook == null) {
88
+ return;
89
+ }
90
+ try {
91
+ await this.beforeProcessExitHook();
92
+ } catch (error) {
93
+ logger.warn("beforeProcessExit 钩子失败(best-effort)", {
94
+ topic: LOG_TOPIC_PROCESS,
95
+ data: { error: getErrorMessage(error) },
96
+ });
97
+ }
98
+ }
99
+
100
+ private async exitProcess(code: number, logger: LoggerWithScope): Promise<never> {
101
+ await this.runBeforeProcessExit(logger);
102
+ process.exit(code);
103
+ }
104
+
74
105
  /** Hook: task registration, trigger config load, etc. Throw to abort bootstrap. */
75
106
  protected abstract onBeforeRegisterNode(logger: LoggerWithScope): Promise<void>;
76
107
 
@@ -296,7 +327,7 @@ abstract class BaseNodeRuntime {
296
327
  }
297
328
 
298
329
  this.isRunning = false;
299
- this.shutdownPromise = this.runShutdown(options?.trigger).catch((error) => {
330
+ this.shutdownPromise = this.runShutdown(options?.trigger).catch(async (error) => {
300
331
  const logger = createLogger({
301
332
  scope: patchScope({ scope: this.startupScope, patch: { labels: { nodeId: this.getLogNodeId() } } }),
302
333
  });
@@ -307,7 +338,7 @@ abstract class BaseNodeRuntime {
307
338
  error: getErrorMessage(error),
308
339
  },
309
340
  });
310
- process.exit(1);
341
+ await this.exitProcess(1, logger);
311
342
  });
312
343
  return this.shutdownPromise;
313
344
  }
@@ -324,7 +355,7 @@ abstract class BaseNodeRuntime {
324
355
 
325
356
  if (this.localNodeId == null) {
326
357
  logger.warn("本地節點未註冊,進程退出", { topic: LOG_TOPIC_NODE });
327
- process.exit(0);
358
+ await this.exitProcess(0, logger);
328
359
  return;
329
360
  }
330
361
 
@@ -344,7 +375,7 @@ abstract class BaseNodeRuntime {
344
375
  data: { error: getErrorMessage(error) },
345
376
  });
346
377
  } finally {
347
- process.exit(0);
378
+ await this.exitProcess(0, logger);
348
379
  }
349
380
  }
350
381
 
@@ -391,7 +422,7 @@ abstract class BaseNodeRuntime {
391
422
  }
392
423
 
393
424
  this.consecutiveHeartbeatFailures = 0;
394
- logger.success("心跳更新成功", {
425
+ logger.info("心跳更新成功", {
395
426
  topic: LOG_TOPIC_NODE,
396
427
  data: { lastHeartBeat: formatHeartbeat(lastHeartBeat) },
397
428
  });
@@ -0,0 +1,27 @@
1
+ import { createLogger } from "../../shared/log/create-logger";
2
+ import type { ManagedChildProcessesOptions } from "./managed-child-processes";
3
+ import { ManagedChildProcesses } from "./managed-child-processes";
4
+
5
+ export type CreateManagedChildProcessesOptions = Pick<
6
+ ManagedChildProcessesOptions,
7
+ "projectRoot" | "preloadModules" | "useTsx" | "graceMs"
8
+ > & {
9
+ /** Defaults to `createLogger({ module: "launcher" })`. */
10
+ logger?: ManagedChildProcessesOptions["logger"];
11
+ };
12
+
13
+ const DEFAULT_PRELOAD_MODULES = ["./scripts/preload.mjs"] as const;
14
+
15
+ /**
16
+ * Standard L3 service child-process registry: node (not tsx), service preload, launcher logger.
17
+ * Each service keeps a singleton in `src/startup/child-processes.ts`.
18
+ */
19
+ export function createManagedChildProcesses(options: CreateManagedChildProcessesOptions): ManagedChildProcesses {
20
+ return new ManagedChildProcesses({
21
+ projectRoot: options.projectRoot,
22
+ preloadModules: options.preloadModules ?? DEFAULT_PRELOAD_MODULES,
23
+ useTsx: options.useTsx ?? false,
24
+ graceMs: options.graceMs,
25
+ logger: options.logger ?? createLogger({ module: "launcher" }),
26
+ });
27
+ }
@@ -1,5 +1,9 @@
1
+ export type { CreateManagedChildProcessesOptions } from "./create-managed-child-processes";
2
+ export { createManagedChildProcesses } from "./create-managed-child-processes";
1
3
  export type {
4
+ CriticalExitHandler,
2
5
  ManagedChildProcessesOptions,
6
+ SpawnChildOptions,
3
7
  SpawnChildResult,
4
8
  StopAllChildrenOptions,
5
9
  } from "./managed-child-processes";