@xano/cli 1.0.6-beta.0 → 1.0.6-beta.10

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 (101) hide show
  1. package/README.md +124 -7
  2. package/dist/base-command.d.ts +2 -0
  3. package/dist/base-command.js +10 -4
  4. package/dist/commands/branch/list/index.js +5 -1
  5. package/dist/commands/ephemeral/impersonate/index.d.ts +1 -0
  6. package/dist/commands/ephemeral/impersonate/index.js +11 -3
  7. package/dist/commands/ephemeral/list/index.js +2 -1
  8. package/dist/commands/ephemeral/pull/index.d.ts +3 -7
  9. package/dist/commands/ephemeral/pull/index.js +25 -109
  10. package/dist/commands/ephemeral/push/index.d.ts +13 -1
  11. package/dist/commands/ephemeral/push/index.js +94 -16
  12. package/dist/commands/ephemeral/static_host/build/delete/index.d.ts +20 -0
  13. package/dist/commands/ephemeral/static_host/build/delete/index.js +121 -0
  14. package/dist/commands/ephemeral/static_host/build/get/index.d.ts +18 -0
  15. package/dist/commands/ephemeral/static_host/build/get/index.js +137 -0
  16. package/dist/commands/ephemeral/static_host/build/list/index.d.ts +18 -0
  17. package/dist/commands/ephemeral/static_host/build/list/index.js +137 -0
  18. package/dist/commands/ephemeral/static_host/build/pull/index.d.ts +53 -0
  19. package/dist/commands/ephemeral/static_host/build/pull/index.js +308 -0
  20. package/dist/commands/ephemeral/static_host/build/push/index.d.ts +24 -0
  21. package/dist/commands/ephemeral/static_host/build/push/index.js +250 -0
  22. package/dist/commands/ephemeral/static_host/create/index.d.ts +18 -0
  23. package/dist/commands/ephemeral/static_host/create/index.js +92 -0
  24. package/dist/commands/ephemeral/static_host/deploy/index.d.ts +19 -0
  25. package/dist/commands/ephemeral/static_host/deploy/index.js +112 -0
  26. package/dist/commands/ephemeral/static_host/edit/index.d.ts +24 -0
  27. package/dist/commands/ephemeral/static_host/edit/index.js +158 -0
  28. package/dist/commands/ephemeral/static_host/get/index.d.ts +19 -0
  29. package/dist/commands/ephemeral/static_host/get/index.js +101 -0
  30. package/dist/commands/ephemeral/static_host/list/index.d.ts +17 -0
  31. package/dist/commands/ephemeral/static_host/list/index.js +132 -0
  32. package/dist/commands/flatten/index.d.ts +34 -0
  33. package/dist/commands/flatten/index.js +140 -0
  34. package/dist/commands/function/list/index.d.ts +4 -4
  35. package/dist/commands/function/list/index.js +30 -38
  36. package/dist/commands/knowledge/list/index.js +2 -1
  37. package/dist/commands/platform/list/index.js +13 -14
  38. package/dist/commands/release/list/index.js +13 -14
  39. package/dist/commands/release/pull/index.d.ts +0 -6
  40. package/dist/commands/release/pull/index.js +10 -95
  41. package/dist/commands/sandbox/env/list/index.js +4 -0
  42. package/dist/commands/sandbox/pull/index.d.ts +0 -1
  43. package/dist/commands/sandbox/pull/index.js +10 -77
  44. package/dist/commands/sandbox/unit_test/list/index.d.ts +2 -0
  45. package/dist/commands/sandbox/unit_test/list/index.js +9 -14
  46. package/dist/commands/sandbox/unit_test/run_all/index.d.ts +1 -0
  47. package/dist/commands/sandbox/unit_test/run_all/index.js +67 -37
  48. package/dist/commands/sandbox/workflow_test/list/index.d.ts +2 -0
  49. package/dist/commands/sandbox/workflow_test/list/index.js +9 -14
  50. package/dist/commands/sandbox/workflow_test/run_all/index.d.ts +1 -0
  51. package/dist/commands/sandbox/workflow_test/run_all/index.js +65 -35
  52. package/dist/commands/static_host/build/list/index.d.ts +2 -3
  53. package/dist/commands/static_host/build/list/index.js +19 -40
  54. package/dist/commands/static_host/build/pull/index.js +12 -9
  55. package/dist/commands/static_host/list/index.d.ts +2 -3
  56. package/dist/commands/static_host/list/index.js +20 -40
  57. package/dist/commands/static_host/migrate/index.js +14 -10
  58. package/dist/commands/tenant/backup/list/index.js +13 -14
  59. package/dist/commands/tenant/cluster/list/index.js +9 -10
  60. package/dist/commands/tenant/env/list/index.js +4 -0
  61. package/dist/commands/tenant/list/index.js +19 -20
  62. package/dist/commands/tenant/pull/index.d.ts +0 -6
  63. package/dist/commands/tenant/pull/index.js +10 -95
  64. package/dist/commands/tenant/snapshot/list/index.js +5 -1
  65. package/dist/commands/tenant/unit_test/list/index.d.ts +4 -2
  66. package/dist/commands/tenant/unit_test/list/index.js +9 -14
  67. package/dist/commands/tenant/unit_test/run_all/index.d.ts +1 -0
  68. package/dist/commands/tenant/unit_test/run_all/index.js +67 -37
  69. package/dist/commands/tenant/workflow_test/list/index.d.ts +4 -2
  70. package/dist/commands/tenant/workflow_test/list/index.js +9 -14
  71. package/dist/commands/tenant/workflow_test/run_all/index.d.ts +1 -0
  72. package/dist/commands/tenant/workflow_test/run_all/index.js +65 -35
  73. package/dist/commands/unit_test/list/index.d.ts +3 -1
  74. package/dist/commands/unit_test/list/index.js +9 -14
  75. package/dist/commands/unit_test/run_all/index.d.ts +1 -0
  76. package/dist/commands/unit_test/run_all/index.js +68 -40
  77. package/dist/commands/workflow_test/list/index.d.ts +3 -1
  78. package/dist/commands/workflow_test/list/index.js +10 -13
  79. package/dist/commands/workflow_test/run_all/index.d.ts +1 -0
  80. package/dist/commands/workflow_test/run_all/index.js +67 -36
  81. package/dist/commands/workspace/git/pull/index.js +53 -3
  82. package/dist/commands/workspace/list/index.js +7 -13
  83. package/dist/commands/workspace/pull/index.d.ts +0 -6
  84. package/dist/commands/workspace/pull/index.js +10 -95
  85. package/dist/utils/concurrency.d.ts +29 -0
  86. package/dist/utils/concurrency.js +54 -0
  87. package/dist/utils/document-parser.d.ts +116 -0
  88. package/dist/utils/document-parser.js +274 -1
  89. package/dist/utils/flatten.d.ts +36 -0
  90. package/dist/utils/flatten.js +68 -0
  91. package/dist/utils/knowledge-sync.d.ts +1 -0
  92. package/dist/utils/knowledge-sync.js +11 -5
  93. package/dist/utils/microservice-wait.d.ts +53 -0
  94. package/dist/utils/microservice-wait.js +83 -0
  95. package/dist/utils/multidoc-push.d.ts +12 -0
  96. package/dist/utils/multidoc-push.js +84 -0
  97. package/dist/utils/paging.d.ts +145 -0
  98. package/dist/utils/paging.js +199 -0
  99. package/dist/utils/reference-checker.js +13 -0
  100. package/oclif.manifest.json +5038 -3635
  101. package/package.json +7 -1
@@ -3,7 +3,7 @@ import * as fs from 'node:fs';
3
3
  import * as path from 'node:path';
4
4
  import snakeCase from 'lodash.snakecase';
5
5
  import BaseCommand from '../../../base-command.js';
6
- import { buildApiGroupFolderResolver, parseDocument } from '../../../utils/document-parser.js';
6
+ import { buildApiGroupFolderResolver, buildChannelServerResolver, parseDocument, resolveDocumentPath, } from '../../../utils/document-parser.js';
7
7
  import { fetchKnowledge, writeKnowledge } from '../../../utils/knowledge-sync.js';
8
8
  export default class Pull extends BaseCommand {
9
9
  static description = 'Pull a workspace multidoc from the Xano Metadata API and split into individual files';
@@ -134,96 +134,19 @@ Pulled 58 documents
134
134
  // Resolve api_group names to unique folder names, disambiguating collisions
135
135
  // where different names produce the same snakeCase (e.g., "Authentication" vs "authentication")
136
136
  const getApiGroupFolder = buildApiGroupFolderResolver(documents, snakeCase);
137
+ // Resolve a realtime v2 channel path -> owning realtime_server name, so
138
+ // messages can nest under their channel's server (see resolver docs).
139
+ const getChannelServer = buildChannelServerResolver(documents);
137
140
  // Track filenames per type to handle duplicates
138
141
  const filenameCounters = new Map();
139
142
  let writtenCount = 0;
140
143
  for (const doc of documents) {
141
- let typeDir;
142
- let baseName;
143
- if (doc.type === 'workspace') {
144
- // workspace → workspace/{name}.xs
145
- typeDir = path.join(outputDir, 'workspace');
146
- baseName = this.sanitizeFilename(doc.name);
147
- }
148
- else if (doc.type === 'workspace_trigger') {
149
- // workspace_trigger → workspace/trigger/{name}.xs
150
- typeDir = path.join(outputDir, 'workspace', 'trigger');
151
- baseName = this.sanitizeFilename(doc.name);
152
- }
153
- else if (doc.type === 'error_trigger') {
154
- // error_trigger → workspace/trigger/{name}.xs (singleton, colocated with workspace triggers)
155
- typeDir = path.join(outputDir, 'workspace', 'trigger');
156
- baseName = this.sanitizeFilename(doc.name);
157
- }
158
- else if (doc.type === 'agent') {
159
- // agent → ai/agent/{name}.xs
160
- typeDir = path.join(outputDir, 'ai', 'agent');
161
- baseName = this.sanitizeFilename(doc.name);
162
- }
163
- else if (doc.type === 'mcp_server') {
164
- // mcp_server → ai/mcp_server/{name}.xs
165
- typeDir = path.join(outputDir, 'ai', 'mcp_server');
166
- baseName = this.sanitizeFilename(doc.name);
167
- }
168
- else if (doc.type === 'tool') {
169
- // tool → ai/tool/{name}.xs
170
- typeDir = path.join(outputDir, 'ai', 'tool');
171
- baseName = this.sanitizeFilename(doc.name);
172
- }
173
- else if (doc.type === 'agent_trigger') {
174
- // agent_trigger → ai/agent/trigger/{name}.xs
175
- typeDir = path.join(outputDir, 'ai', 'agent', 'trigger');
176
- baseName = this.sanitizeFilename(doc.name);
177
- }
178
- else if (doc.type === 'mcp_server_trigger') {
179
- // mcp_server_trigger → ai/mcp_server/trigger/{name}.xs
180
- typeDir = path.join(outputDir, 'ai', 'mcp_server', 'trigger');
181
- baseName = this.sanitizeFilename(doc.name);
182
- }
183
- else if (doc.type === 'table_trigger') {
184
- // table_trigger → table/trigger/{name}.xs
185
- typeDir = path.join(outputDir, 'table', 'trigger');
186
- baseName = this.sanitizeFilename(doc.name);
187
- }
188
- else if (doc.type === 'realtime_channel') {
189
- // realtime_channel → realtime/channel/{name}.xs
190
- typeDir = path.join(outputDir, 'realtime', 'channel');
191
- baseName = this.sanitizeFilename(doc.name);
192
- }
193
- else if (doc.type === 'realtime_trigger') {
194
- // realtime_trigger → realtime/trigger/{name}.xs
195
- typeDir = path.join(outputDir, 'realtime', 'trigger');
196
- baseName = this.sanitizeFilename(doc.name);
197
- }
198
- else if (doc.type === 'api_group') {
199
- // api_group "test" → api/{resolved_folder}/{name}.xs
200
- const groupFolder = getApiGroupFolder(doc.name);
201
- typeDir = path.join(outputDir, 'api', groupFolder);
202
- baseName = this.sanitizeFilename(doc.name);
203
- }
204
- else if (doc.type === 'query' && doc.apiGroup) {
205
- // query in group "test" → api/{resolved_folder}/{query_name}.xs
206
- const groupFolder = getApiGroupFolder(doc.apiGroup);
207
- const nameParts = doc.name.split('/');
208
- const leafName = nameParts.pop();
209
- const folderParts = nameParts.map((part) => snakeCase(part));
210
- typeDir = path.join(outputDir, 'api', groupFolder, ...folderParts);
211
- baseName = this.sanitizeFilename(leafName);
212
- if (doc.verb) {
213
- baseName = `${baseName}_${doc.verb}`;
214
- }
215
- }
216
- else {
217
- // Default: split folder path from name
218
- const nameParts = doc.name.split('/');
219
- const leafName = nameParts.pop();
220
- const folderParts = nameParts.map((part) => snakeCase(part));
221
- typeDir = path.join(outputDir, doc.type, ...folderParts);
222
- baseName = this.sanitizeFilename(leafName);
223
- if (doc.verb) {
224
- baseName = `${baseName}_${doc.verb}`;
225
- }
226
- }
144
+ const { baseName, typeDir } = resolveDocumentPath(doc, outputDir, {
145
+ getApiGroupFolder,
146
+ getChannelServer,
147
+ join: path.join,
148
+ snakeCase,
149
+ });
227
150
  fs.mkdirSync(typeDir, { recursive: true });
228
151
  // Track duplicates per directory
229
152
  const dirKey = path.relative(outputDir, typeDir);
@@ -252,12 +175,4 @@ Pulled 58 documents
252
175
  parts.push(`${knowledgeCount} knowledge file${knowledgeCount === 1 ? '' : 's'}`);
253
176
  this.log(`Pulled ${parts.join(' + ')} to ${flags.directory}`);
254
177
  }
255
- /**
256
- * Sanitize a document name for use as a filename.
257
- * Strips quotes, replaces spaces with underscores, and removes
258
- * characters that are unsafe in filenames.
259
- */
260
- sanitizeFilename(name) {
261
- return snakeCase(name.replaceAll('"', ''));
262
- }
263
178
  }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Run `worker` over `items` with at most `limit` in flight at once.
3
+ *
4
+ * Results come back in **input order**, not completion order. That matters for
5
+ * test runners: a concurrent run must still produce output a human can diff
6
+ * against a sequential one, and a JSON results array whose order does not shift
7
+ * between runs.
8
+ *
9
+ * `limit` is clamped to at least 1, so a limit of 1 is exactly sequential
10
+ * execution and callers do not need a separate code path for it.
11
+ *
12
+ * The worker is expected to capture its own failures — one rejected worker
13
+ * rejects the whole call. Callers that want per-item error reporting (as the
14
+ * test runners do) should resolve a failure result instead of throwing.
15
+ */
16
+ export declare function mapWithConcurrency<T, R>(items: T[], limit: number, worker: (item: T, index: number) => Promise<R>): Promise<R[]>;
17
+ /**
18
+ * Emit results in input order as soon as each becomes available, without
19
+ * waiting for the whole batch.
20
+ *
21
+ * Progress lines are the reason this exists: with concurrency the third test may
22
+ * finish before the first, but printing them out of order makes the output hard
23
+ * to follow and non-deterministic between runs. This buffers out-of-order
24
+ * completions and flushes the moment the next expected index arrives, so the
25
+ * user sees an ordered stream that still starts before the run ends.
26
+ */
27
+ export declare function createOrderedEmitter<R>(onReady: (result: R, index: number) => void): {
28
+ settle(index: number, result: R): void;
29
+ };
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Run `worker` over `items` with at most `limit` in flight at once.
3
+ *
4
+ * Results come back in **input order**, not completion order. That matters for
5
+ * test runners: a concurrent run must still produce output a human can diff
6
+ * against a sequential one, and a JSON results array whose order does not shift
7
+ * between runs.
8
+ *
9
+ * `limit` is clamped to at least 1, so a limit of 1 is exactly sequential
10
+ * execution and callers do not need a separate code path for it.
11
+ *
12
+ * The worker is expected to capture its own failures — one rejected worker
13
+ * rejects the whole call. Callers that want per-item error reporting (as the
14
+ * test runners do) should resolve a failure result instead of throwing.
15
+ */
16
+ export async function mapWithConcurrency(items, limit, worker) {
17
+ const effectiveLimit = Math.max(1, Math.floor(limit));
18
+ const results = Array.from({ length: items.length });
19
+ let cursor = 0;
20
+ const runners = Array.from({ length: Math.min(effectiveLimit, items.length) }, async () => {
21
+ while (cursor < items.length) {
22
+ const index = cursor;
23
+ cursor++;
24
+ // eslint-disable-next-line no-await-in-loop
25
+ results[index] = await worker(items[index], index);
26
+ }
27
+ });
28
+ await Promise.all(runners);
29
+ return results;
30
+ }
31
+ /**
32
+ * Emit results in input order as soon as each becomes available, without
33
+ * waiting for the whole batch.
34
+ *
35
+ * Progress lines are the reason this exists: with concurrency the third test may
36
+ * finish before the first, but printing them out of order makes the output hard
37
+ * to follow and non-deterministic between runs. This buffers out-of-order
38
+ * completions and flushes the moment the next expected index arrives, so the
39
+ * user sees an ordered stream that still starts before the run ends.
40
+ */
41
+ export function createOrderedEmitter(onReady) {
42
+ const pending = new Map();
43
+ let next = 0;
44
+ return {
45
+ settle(index, result) {
46
+ pending.set(index, result);
47
+ while (pending.has(next)) {
48
+ onReady(pending.get(next), next);
49
+ pending.delete(next);
50
+ next++;
51
+ }
52
+ },
53
+ };
54
+ }
@@ -1,9 +1,13 @@
1
1
  export interface ParsedDocument {
2
2
  apiGroup?: string;
3
3
  canonical?: string;
4
+ /** Owning channel path for realtime v2 `message` documents. */
5
+ channel?: string;
4
6
  content: string;
5
7
  guid?: string;
6
8
  name: string;
9
+ /** Owning realtime_server name for realtime v2 `channel` documents. */
10
+ server?: string;
7
11
  type: string;
8
12
  verb?: string;
9
13
  }
@@ -12,6 +16,87 @@ export interface ParsedDocument {
12
16
  * Skips leading comment lines (starting with //) to find the first meaningful line.
13
17
  */
14
18
  export declare function parseDocument(content: string): null | ParsedDocument;
19
+ /**
20
+ * Map a realtime v2 channel path onto directory segments.
21
+ *
22
+ * A channel name IS a path template — "rooms/{room_id}" — so the directory tree
23
+ * mirrors it rather than flattening it. That is the whole reason messages nest
24
+ * under channels: the tree should show the structure, and
25
+ * "channel/rooms/[room_id]/post.xs" says immediately that `post` lives on a
26
+ * parameterized rooms channel.
27
+ *
28
+ * "rooms" -> ["rooms"]
29
+ * "rooms/{room_id}" -> ["rooms", "[room_id]"]
30
+ * "org/{org_id}/room/{id}" -> ["org", "[org_id]", "room", "[id]"]
31
+ *
32
+ * Braces become brackets because "{" and "}" are awkward in shells, while "["
33
+ * and "]" are legal on every filesystem. They are still glob character classes,
34
+ * so scripting against a checkout needs the path quoted.
35
+ *
36
+ * Escaping the braces rather than dropping them is what keeps a parameterized
37
+ * channel distinct from a literal one: without it, "rooms/{room_id}" and a
38
+ * literal channel named "rooms/room_id" collide on the same directory.
39
+ *
40
+ * Mirrors Vfs::channelSegment() on the server.
41
+ */
42
+ export declare function channelPathSegments(name: string, snakeCaseFn: (s: string) => string): string[];
43
+ /**
44
+ * Build a map of realtime v2 channel path -> owning realtime_server name.
45
+ *
46
+ * This is the cross-document lookup the on-disk nesting needs: a `message`
47
+ * document names its channel (`channel = "..."`), while a `channel` document
48
+ * names its server (`realtime_server = "..."`). To place a message under
49
+ * `realtime/server/<server>/channel/<path>/...` we resolve the message's channel
50
+ * to that channel's server — which the channel document carries. (Messages now
51
+ * also carry `realtime_server` directly, but resolving via the channel keeps a
52
+ * single source of truth for the channel→server mapping.)
53
+ *
54
+ * Because every pull path parses the WHOLE multidoc into memory before writing,
55
+ * we can build this map from the `channel` docs in the same batch (a two-pass
56
+ * resolve), keyed by the channel's own name (its path). A channel with no
57
+ * `realtime_server` (e.g. a pre-server export) simply isn't in the map, and
58
+ * callers fall back accordingly.
59
+ */
60
+ export declare function buildChannelServerResolver(documents: ParsedDocument[]): (channelName: string) => string | undefined;
61
+ /**
62
+ * Sanitize a document name for use as a single on-disk filename segment.
63
+ * Strips quotes and snake_cases the rest. Mirrors the per-command
64
+ * `sanitizeFilename` the pull commands used to each define privately.
65
+ */
66
+ export declare function sanitizeDocumentName(name: string, snakeCaseFn: (s: string) => string): string;
67
+ /**
68
+ * The directory + base filename (no extension, no duplicate suffix) a document
69
+ * is written to on disk. `resolveDocumentPath` returns this; callers append the
70
+ * `.xs` extension and any `_N` duplicate suffix.
71
+ */
72
+ export interface DocumentPlacement {
73
+ baseName: string;
74
+ typeDir: string;
75
+ }
76
+ /**
77
+ * Resolve where a parsed document lands on disk — the SINGLE source of truth for
78
+ * the pull layout, shared by every pull command (workspace, tenant, release,
79
+ * sandbox, ephemeral). Previously each command inlined a ~130-line copy of this
80
+ * decision; the copies drifted (the realtime v2 realtime_server/channel/message
81
+ * branches existed only in workspace/tenant), so an ephemeral pull dumped v2
82
+ * objects into flat top-level channel/, message/ and realtime_server/ folders —
83
+ * detaching messages from their channels and risking name collisions (message
84
+ * names are unique only WITHIN a channel).
85
+ *
86
+ * @param doc - the parsed document
87
+ * @param outputDir - the resolved pull output root
88
+ * @param deps - injected dependencies
89
+ * @param deps.getApiGroupFolder - resolves an api_group name to its unique folder
90
+ * @param deps.getChannelServer - resolves a v2 channel path to its owning realtime_server
91
+ * @param deps.join - the path.join implementation (injectable for testing)
92
+ * @param deps.snakeCase - the snakeCase implementation the caller uses
93
+ */
94
+ export declare function resolveDocumentPath(doc: ParsedDocument, outputDir: string, deps: {
95
+ getApiGroupFolder: (groupName: string) => string;
96
+ getChannelServer: (channelName: string) => string | undefined;
97
+ join: (...parts: string[]) => string;
98
+ snakeCase: (s: string) => string;
99
+ }): DocumentPlacement;
15
100
  /**
16
101
  * Build a unique key for a document based on its type, name, verb, and api_group.
17
102
  * Used to match server GUID map entries back to local files.
@@ -37,3 +122,34 @@ export declare function findFilesWithGuid(entries: Array<{
37
122
  content: string;
38
123
  filePath: string;
39
124
  }>, guid: string): string[];
125
+ /**
126
+ * Split a multi-document XanoScript blob (documents joined by `\n---\n`) into
127
+ * parsed documents, skipping empties and any fragment that fails to parse.
128
+ *
129
+ * This is the same split the pull commands apply to the server's multidoc
130
+ * response. A hand-authored bundle is one file of many `---`-separated
131
+ * documents; this turns it back into the per-document units every downstream
132
+ * step (placement, GUID writeback, partial-diff matching) assumes.
133
+ */
134
+ export declare function splitMultidoc(blob: string): ParsedDocument[];
135
+ /** A document's resolved on-disk location relative to the output root, plus its content. */
136
+ export interface PlacedDocument {
137
+ content: string;
138
+ /** POSIX-style path relative to the output root, e.g. "api/pdf/documents_GET.xs". */
139
+ relPath: string;
140
+ }
141
+ /**
142
+ * Compute where each document lands on disk — a PURE function (no filesystem)
143
+ * mirroring the split-and-place loop the pull commands run. It resolves each
144
+ * document via `resolveDocumentPath`, appends `.xs`, and disambiguates
145
+ * collisions within a directory with a `_N` suffix (the second file named
146
+ * `say.xs` in a dir becomes `say_2.xs`), exactly as the pull writer does.
147
+ *
148
+ * Kept filesystem-free so it can be unit-tested and reused by both the pull
149
+ * commands and `flatten` without duplicating the placement rules.
150
+ */
151
+ export declare function placeDocuments(documents: ParsedDocument[], deps: {
152
+ join: (...parts: string[]) => string;
153
+ relative: (from: string, to: string) => string;
154
+ snakeCase: (s: string) => string;
155
+ }): PlacedDocument[];
@@ -45,6 +45,25 @@ export function parseDocument(content) {
45
45
  if (apiGroupMatch) {
46
46
  apiGroup = apiGroupMatch[1];
47
47
  }
48
+ // Extract channel if present (e.g., channel = "rooms/{room_id}").
49
+ // Realtime v2 messages reference their owning channel by PATH, which is what
50
+ // nests them under it on disk.
51
+ let channel;
52
+ const channelMatch = content.match(/^\s*channel\s*=\s*"([^"]*)"/m);
53
+ if (channelMatch) {
54
+ channel = channelMatch[1];
55
+ }
56
+ // Extract realtime_server if present (e.g., realtime_server = "chat").
57
+ // Realtime v2 channels/messages reference their owning realtime_server by NAME
58
+ // via a top-level `realtime_server = "..."`, which is what nests them under it
59
+ // on disk (mirrors how a message names its channel via `channel = "..."`).
60
+ // The keyword is `realtime_server` (renamed from a bare `server` to match the
61
+ // api_group/mcp_server convention — reference the container by its full type).
62
+ let server;
63
+ const serverMatch = content.match(/^\s*realtime_server\s*=\s*"([^"]*)"/m);
64
+ if (serverMatch) {
65
+ server = serverMatch[1];
66
+ }
48
67
  // Extract canonical if present (e.g., canonical = "abc123")
49
68
  let canonical;
50
69
  const canonicalMatch = content.match(/canonical\s*=\s*"([^"]*)"/);
@@ -57,7 +76,201 @@ export function parseDocument(content) {
57
76
  if (guidMatch) {
58
77
  guid = guidMatch[1];
59
78
  }
60
- return { apiGroup, canonical, content, guid, name, type, verb };
79
+ return { apiGroup, canonical, channel, content, guid, name, server, type, verb };
80
+ }
81
+ /**
82
+ * Map a realtime v2 channel path onto directory segments.
83
+ *
84
+ * A channel name IS a path template — "rooms/{room_id}" — so the directory tree
85
+ * mirrors it rather than flattening it. That is the whole reason messages nest
86
+ * under channels: the tree should show the structure, and
87
+ * "channel/rooms/[room_id]/post.xs" says immediately that `post` lives on a
88
+ * parameterized rooms channel.
89
+ *
90
+ * "rooms" -> ["rooms"]
91
+ * "rooms/{room_id}" -> ["rooms", "[room_id]"]
92
+ * "org/{org_id}/room/{id}" -> ["org", "[org_id]", "room", "[id]"]
93
+ *
94
+ * Braces become brackets because "{" and "}" are awkward in shells, while "["
95
+ * and "]" are legal on every filesystem. They are still glob character classes,
96
+ * so scripting against a checkout needs the path quoted.
97
+ *
98
+ * Escaping the braces rather than dropping them is what keeps a parameterized
99
+ * channel distinct from a literal one: without it, "rooms/{room_id}" and a
100
+ * literal channel named "rooms/room_id" collide on the same directory.
101
+ *
102
+ * Mirrors Vfs::channelSegment() on the server.
103
+ */
104
+ export function channelPathSegments(name, snakeCaseFn) {
105
+ // A parameter keeps the author's name verbatim so it matches the `input`
106
+ // block; only the delimiters are swapped.
107
+ const isParam = /^\{.*\}$/;
108
+ return name.split('/').map((segment) => (isParam.test(segment) ? `[${segment.slice(1, -1)}]` : snakeCaseFn(segment)));
109
+ }
110
+ /**
111
+ * Build a map of realtime v2 channel path -> owning realtime_server name.
112
+ *
113
+ * This is the cross-document lookup the on-disk nesting needs: a `message`
114
+ * document names its channel (`channel = "..."`), while a `channel` document
115
+ * names its server (`realtime_server = "..."`). To place a message under
116
+ * `realtime/server/<server>/channel/<path>/...` we resolve the message's channel
117
+ * to that channel's server — which the channel document carries. (Messages now
118
+ * also carry `realtime_server` directly, but resolving via the channel keeps a
119
+ * single source of truth for the channel→server mapping.)
120
+ *
121
+ * Because every pull path parses the WHOLE multidoc into memory before writing,
122
+ * we can build this map from the `channel` docs in the same batch (a two-pass
123
+ * resolve), keyed by the channel's own name (its path). A channel with no
124
+ * `realtime_server` (e.g. a pre-server export) simply isn't in the map, and
125
+ * callers fall back accordingly.
126
+ */
127
+ export function buildChannelServerResolver(documents) {
128
+ const channelToServer = new Map();
129
+ for (const doc of documents) {
130
+ if (doc.type === 'channel' && doc.server) {
131
+ channelToServer.set(doc.name, doc.server);
132
+ }
133
+ }
134
+ return (channelName) => channelToServer.get(channelName);
135
+ }
136
+ /**
137
+ * Sanitize a document name for use as a single on-disk filename segment.
138
+ * Strips quotes and snake_cases the rest. Mirrors the per-command
139
+ * `sanitizeFilename` the pull commands used to each define privately.
140
+ */
141
+ export function sanitizeDocumentName(name, snakeCaseFn) {
142
+ return snakeCaseFn(name.replaceAll('"', ''));
143
+ }
144
+ /**
145
+ * Resolve where a parsed document lands on disk — the SINGLE source of truth for
146
+ * the pull layout, shared by every pull command (workspace, tenant, release,
147
+ * sandbox, ephemeral). Previously each command inlined a ~130-line copy of this
148
+ * decision; the copies drifted (the realtime v2 realtime_server/channel/message
149
+ * branches existed only in workspace/tenant), so an ephemeral pull dumped v2
150
+ * objects into flat top-level channel/, message/ and realtime_server/ folders —
151
+ * detaching messages from their channels and risking name collisions (message
152
+ * names are unique only WITHIN a channel).
153
+ *
154
+ * @param doc - the parsed document
155
+ * @param outputDir - the resolved pull output root
156
+ * @param deps - injected dependencies
157
+ * @param deps.getApiGroupFolder - resolves an api_group name to its unique folder
158
+ * @param deps.getChannelServer - resolves a v2 channel path to its owning realtime_server
159
+ * @param deps.join - the path.join implementation (injectable for testing)
160
+ * @param deps.snakeCase - the snakeCase implementation the caller uses
161
+ */
162
+ export function resolveDocumentPath(doc, outputDir, deps) {
163
+ const { getApiGroupFolder, getChannelServer, join, snakeCase } = deps;
164
+ const sanitize = (name) => sanitizeDocumentName(name, snakeCase);
165
+ if (doc.type === 'workspace') {
166
+ // workspace → workspace/{name}.xs
167
+ return { baseName: sanitize(doc.name), typeDir: join(outputDir, 'workspace') };
168
+ }
169
+ if (doc.type === 'workspace_trigger' || doc.type === 'error_trigger') {
170
+ // workspace_trigger / error_trigger → workspace/trigger/{name}.xs
171
+ // (error_trigger is a singleton, colocated with workspace triggers)
172
+ return { baseName: sanitize(doc.name), typeDir: join(outputDir, 'workspace', 'trigger') };
173
+ }
174
+ if (doc.type === 'agent') {
175
+ // agent → ai/agent/{name}.xs
176
+ return { baseName: sanitize(doc.name), typeDir: join(outputDir, 'ai', 'agent') };
177
+ }
178
+ if (doc.type === 'mcp_server') {
179
+ // mcp_server → ai/mcp_server/{name}.xs
180
+ return { baseName: sanitize(doc.name), typeDir: join(outputDir, 'ai', 'mcp_server') };
181
+ }
182
+ if (doc.type === 'tool') {
183
+ // tool → ai/tool/{name}.xs
184
+ return { baseName: sanitize(doc.name), typeDir: join(outputDir, 'ai', 'tool') };
185
+ }
186
+ if (doc.type === 'agent_trigger') {
187
+ // agent_trigger → ai/agent/trigger/{name}.xs
188
+ return { baseName: sanitize(doc.name), typeDir: join(outputDir, 'ai', 'agent', 'trigger') };
189
+ }
190
+ if (doc.type === 'mcp_server_trigger') {
191
+ // mcp_server_trigger → ai/mcp_server/trigger/{name}.xs
192
+ return { baseName: sanitize(doc.name), typeDir: join(outputDir, 'ai', 'mcp_server', 'trigger') };
193
+ }
194
+ if (doc.type === 'table_trigger') {
195
+ // table_trigger → table/trigger/{name}.xs
196
+ return { baseName: sanitize(doc.name), typeDir: join(outputDir, 'table', 'trigger') };
197
+ }
198
+ if (doc.type === 'realtime_channel') {
199
+ // Realtime v1: realtime_channel → realtime/channel/{name}.xs
200
+ return { baseName: sanitize(doc.name), typeDir: join(outputDir, 'realtime', 'channel') };
201
+ }
202
+ if (doc.type === 'realtime_trigger') {
203
+ // Realtime v1: realtime_trigger → realtime/trigger/{name}.xs
204
+ return { baseName: sanitize(doc.name), typeDir: join(outputDir, 'realtime', 'trigger') };
205
+ }
206
+ if (doc.type === 'realtime_server') {
207
+ // Realtime v2. The realtime_server is the top-level container that owns
208
+ // channels (which own messages). Its own document is named after itself
209
+ // inside its name directory, mirroring api_group (api/<group>/<group>.xs).
210
+ // realtime_server "chat" → realtime/server/chat/chat.xs
211
+ return { baseName: sanitize(doc.name), typeDir: join(outputDir, 'realtime', 'server', sanitize(doc.name)) };
212
+ }
213
+ if (doc.type === 'channel') {
214
+ // Realtime v2. The channel owns a directory named after itself (the full
215
+ // channel name, snake_cased into a single flat segment), and its messages
216
+ // live in a message/ subfolder inside it. It nests under its owning
217
+ // realtime_server (from `realtime_server = "..."`):
218
+ // channel "rooms/{room_id}" (server "chat")
219
+ // → realtime/server/chat/channel/rooms_room_id/rooms_room_id.xs
220
+ //
221
+ // A channel with no resolvable server (e.g. a pre-server export) falls back
222
+ // to the legacy flat channel/<path>/_channel.xs layout.
223
+ if (doc.server) {
224
+ return {
225
+ baseName: snakeCase(doc.name),
226
+ typeDir: join(outputDir, 'realtime', 'server', sanitize(doc.server), 'channel', snakeCase(doc.name)),
227
+ };
228
+ }
229
+ return { baseName: '_channel', typeDir: join(outputDir, 'channel', ...channelPathSegments(doc.name, snakeCase)) };
230
+ }
231
+ if (doc.type === 'message' && doc.channel) {
232
+ // Realtime v2 message → nests in a message/ subfolder under its channel,
233
+ // under that channel's server (resolved via the two-pass channel→server map).
234
+ // message "post" on channel "rooms/{room_id}" (server "chat")
235
+ // → realtime/server/chat/channel/rooms_room_id/message/post.xs
236
+ //
237
+ // Nesting matters beyond tidiness: message names are unique only WITHIN a
238
+ // channel, so a flat message/ directory would collide when two channels
239
+ // both define e.g. "say". A channel whose server can't be resolved falls
240
+ // back to the legacy flat channel/ layout.
241
+ const messageServer = getChannelServer(doc.channel);
242
+ return {
243
+ baseName: sanitize(doc.name),
244
+ typeDir: messageServer
245
+ ? join(outputDir, 'realtime', 'server', sanitize(messageServer), 'channel', snakeCase(doc.channel), 'message')
246
+ : join(outputDir, 'channel', ...channelPathSegments(doc.channel, snakeCase)),
247
+ };
248
+ }
249
+ if (doc.type === 'api_group') {
250
+ // api_group "test" → api/{resolved_folder}/{name}.xs
251
+ return { baseName: sanitize(doc.name), typeDir: join(outputDir, 'api', getApiGroupFolder(doc.name)) };
252
+ }
253
+ if (doc.type === 'query' && doc.apiGroup) {
254
+ // query in group "test" → api/{resolved_folder}/{folders}/{query_name}[_verb].xs
255
+ const groupFolder = getApiGroupFolder(doc.apiGroup);
256
+ const nameParts = doc.name.split('/');
257
+ const leafName = nameParts.pop();
258
+ const folderParts = nameParts.map((part) => snakeCase(part));
259
+ const baseName = sanitize(leafName);
260
+ return {
261
+ baseName: doc.verb ? `${baseName}_${doc.verb}` : baseName,
262
+ typeDir: join(outputDir, 'api', groupFolder, ...folderParts),
263
+ };
264
+ }
265
+ // Default: split folder path from name
266
+ const nameParts = doc.name.split('/');
267
+ const leafName = nameParts.pop();
268
+ const folderParts = nameParts.map((part) => snakeCase(part));
269
+ const baseName = sanitize(leafName);
270
+ return {
271
+ baseName: doc.verb ? `${baseName}_${doc.verb}` : baseName,
272
+ typeDir: join(outputDir, doc.type, ...folderParts),
273
+ };
61
274
  }
62
275
  /**
63
276
  * Build a unique key for a document based on its type, name, verb, and api_group.
@@ -112,3 +325,63 @@ export function buildApiGroupFolderResolver(documents, snakeCaseFn) {
112
325
  export function findFilesWithGuid(entries, guid) {
113
326
  return entries.filter((e) => e.content.includes(guid)).map((e) => e.filePath);
114
327
  }
328
+ /**
329
+ * Split a multi-document XanoScript blob (documents joined by `\n---\n`) into
330
+ * parsed documents, skipping empties and any fragment that fails to parse.
331
+ *
332
+ * This is the same split the pull commands apply to the server's multidoc
333
+ * response. A hand-authored bundle is one file of many `---`-separated
334
+ * documents; this turns it back into the per-document units every downstream
335
+ * step (placement, GUID writeback, partial-diff matching) assumes.
336
+ */
337
+ export function splitMultidoc(blob) {
338
+ const documents = [];
339
+ for (const raw of blob.split('\n---\n')) {
340
+ const trimmed = raw.trim();
341
+ if (!trimmed)
342
+ continue;
343
+ const parsed = parseDocument(trimmed);
344
+ if (parsed)
345
+ documents.push(parsed);
346
+ }
347
+ return documents;
348
+ }
349
+ /**
350
+ * Compute where each document lands on disk — a PURE function (no filesystem)
351
+ * mirroring the split-and-place loop the pull commands run. It resolves each
352
+ * document via `resolveDocumentPath`, appends `.xs`, and disambiguates
353
+ * collisions within a directory with a `_N` suffix (the second file named
354
+ * `say.xs` in a dir becomes `say_2.xs`), exactly as the pull writer does.
355
+ *
356
+ * Kept filesystem-free so it can be unit-tested and reused by both the pull
357
+ * commands and `flatten` without duplicating the placement rules.
358
+ */
359
+ export function placeDocuments(documents, deps) {
360
+ const { join, relative, snakeCase } = deps;
361
+ const getApiGroupFolder = buildApiGroupFolderResolver(documents, snakeCase);
362
+ const getChannelServer = buildChannelServerResolver(documents);
363
+ // Per-directory filename counters, so duplicate leaf names get a _N suffix.
364
+ const filenameCounters = new Map();
365
+ const placed = [];
366
+ for (const doc of documents) {
367
+ const { baseName, typeDir } = resolveDocumentPath(doc, '', {
368
+ getApiGroupFolder,
369
+ getChannelServer,
370
+ join,
371
+ snakeCase,
372
+ });
373
+ const dirKey = typeDir;
374
+ if (!filenameCounters.has(dirKey))
375
+ filenameCounters.set(dirKey, new Map());
376
+ const typeCounters = filenameCounters.get(dirKey);
377
+ const count = typeCounters.get(baseName) || 0;
378
+ typeCounters.set(baseName, count + 1);
379
+ const filename = count === 0 ? `${baseName}.xs` : `${baseName}_${count + 1}.xs`;
380
+ const abs = join(typeDir, filename);
381
+ // typeDir was built off an empty root, so `abs` is already root-relative;
382
+ // normalize any leading separator via relative() for a clean POSIX path.
383
+ const relPath = relative('', abs);
384
+ placed.push({ content: doc.content, relPath });
385
+ }
386
+ return placed;
387
+ }