@assemblyline/al-orc 0.0.0-stage → 0.1.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.
Files changed (66) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +5 -0
  4. package/README.md +98 -2
  5. package/bin/cli.js +5 -0
  6. package/package.json +59 -4
  7. package/src/cli/_helpers.js +67 -0
  8. package/src/cli/config/add.js +10 -0
  9. package/src/cli/config/current.js +20 -0
  10. package/src/cli/config/index.js +63 -0
  11. package/src/cli/config/list.js +22 -0
  12. package/src/cli/config/remove.js +6 -0
  13. package/src/cli/config/use.js +6 -0
  14. package/src/cli/config-store.js +127 -0
  15. package/src/cli/factory-sessions/index.js +35 -0
  16. package/src/cli/factory-sessions/list.js +93 -0
  17. package/src/cli/factory-sessions/show.js +83 -0
  18. package/src/cli/index.js +61 -0
  19. package/src/cli/lines/index.js +53 -0
  20. package/src/cli/lines/list.js +36 -0
  21. package/src/cli/lines/pull.js +46 -0
  22. package/src/cli/lines/push.js +82 -0
  23. package/src/cli/lines/show.js +36 -0
  24. package/src/cli/shared/edit-tags.js +43 -0
  25. package/src/cli/stations/archive.js +24 -0
  26. package/src/cli/stations/delete.js +8 -0
  27. package/src/cli/stations/edit.js +30 -0
  28. package/src/cli/stations/index.js +104 -0
  29. package/src/cli/stations/list.js +34 -0
  30. package/src/cli/stations/pull.js +73 -0
  31. package/src/cli/stations/push.js +212 -0
  32. package/src/cli/stations/run.js +87 -0
  33. package/src/cli/stations/show.js +74 -0
  34. package/src/cli/status.js +23 -0
  35. package/src/cli/supporting-docs/index.js +21 -0
  36. package/src/cli/supporting-docs/show.js +49 -0
  37. package/src/cli/tags/create.js +21 -0
  38. package/src/cli/tags/delete.js +8 -0
  39. package/src/cli/tags/edit.js +27 -0
  40. package/src/cli/tags/index.js +49 -0
  41. package/src/cli/tags/list.js +24 -0
  42. package/src/cli/utils/format.js +206 -0
  43. package/src/cli/utils/line-files.js +84 -0
  44. package/src/cli/utils/station-files.js +151 -0
  45. package/src/cli/utils/station-labels.js +48 -0
  46. package/src/cli/work-records/cancel.js +24 -0
  47. package/src/cli/work-records/edit.js +22 -0
  48. package/src/cli/work-records/index.js +59 -0
  49. package/src/cli/work-records/list.js +48 -0
  50. package/src/cli/work-records/show.js +103 -0
  51. package/src/http.js +255 -0
  52. package/src/index.js +104 -0
  53. package/src/resources/factory-sessions.js +86 -0
  54. package/src/resources/lines.js +57 -0
  55. package/src/resources/stations.js +90 -0
  56. package/src/resources/supporting-docs.js +37 -0
  57. package/src/resources/tags.js +68 -0
  58. package/src/resources/work-records.js +77 -0
  59. package/src/types/api/Line.types.js +22 -0
  60. package/src/types/api/Station.types.js +34 -0
  61. package/src/types/api/SupportingDoc.types.js +14 -0
  62. package/src/types/api/Tag.types.js +10 -0
  63. package/src/types/api/WorkRecord.types.js +27 -0
  64. package/src/types/general/Credentials.types.js +18 -0
  65. package/src/types/general/Transport.types.js +21 -0
  66. package/src/types/general/index.js +8 -0
@@ -0,0 +1,68 @@
1
+ // @ts-check
2
+ /**
3
+ * The `tags` resource — every tag endpoint, once, plus two name↔id convenience
4
+ * helpers the CLI's tag-editing flows lean on.
5
+ */
6
+
7
+ /**
8
+ * @typedef {import('../types/general/index.js').Transport} Transport
9
+ * @typedef {import('../types/api/Tag.types.js').Tag} Tag
10
+ */
11
+
12
+ /**
13
+ * @typedef {Object} TagsApi
14
+ * @property {() => Promise<{ data: Tag[], page_context: object|null }>} list
15
+ * @property {(data: object) => Promise<Tag|null>} create
16
+ * @property {(id: string, data: object) => Promise<Tag|null>} update
17
+ * @property {(id: string) => Promise<any>} delete
18
+ * @property {(names: string[]) => Promise<string[]>} resolveNames
19
+ * @property {(name: string) => Promise<Tag>} ensure
20
+ */
21
+
22
+ /** @param {any} r @returns {any} */
23
+ const unwrap = (r) => r?.data ?? null;
24
+ /** @param {string} s */
25
+ const enc = (s) => encodeURIComponent(s);
26
+
27
+ /**
28
+ * @param {Transport} ctx
29
+ * @returns {TagsApi}
30
+ */
31
+ export function buildTags(ctx) {
32
+ /** @type {TagsApi} */
33
+ const api = {
34
+ /** All tags. Returns the `{ data, page_context }` envelope. */
35
+ list: () => ctx.get('/api/v1/tags'),
36
+
37
+ /** Create a tag (`{ name, color?, description? }`). Returns the created record. */
38
+ create: (data) => ctx.post('/api/v1/tags', data).then(unwrap),
39
+
40
+ /** Update a tag's fields. Returns the updated record. */
41
+ update: (id, data) => ctx.put(`/api/v1/tags/${enc(id)}`, data).then(unwrap),
42
+
43
+ /** Delete a tag by id. */
44
+ delete: (id) => ctx.delete(`/api/v1/tags/${enc(id)}`).then(unwrap),
45
+
46
+ /** Resolve tag names → ids (throws if any name is unknown). */
47
+ resolveNames: async (names) => {
48
+ const { data = [] } = await api.list();
49
+ const byName = new Map(data.map((t) => [t.name, t.id]));
50
+ return names.map((name) => {
51
+ const id = byName.get(name);
52
+ if (!id) throw new Error(`Unknown tag: ${name}`);
53
+ return id;
54
+ });
55
+ },
56
+
57
+ /** Get a tag by name, creating it if it doesn't exist. */
58
+ ensure: async (name) => {
59
+ const { data = [] } = await api.list();
60
+ const existing = data.find((t) => t.name === name);
61
+ if (existing) return existing;
62
+ const created = await api.create({ name });
63
+ if (!created) throw new Error(`Failed to create tag: ${name}`);
64
+ return created;
65
+ },
66
+ };
67
+ return api;
68
+ }
@@ -0,0 +1,77 @@
1
+ // @ts-check
2
+ /**
3
+ * The `work-records` resource — every orchestrator work-record endpoint, once.
4
+ *
5
+ * Callers filter by `station` (an id or a short_code), sent as `?station=`.
6
+ */
7
+
8
+ /**
9
+ * @typedef {import('../types/general/index.js').Transport} Transport
10
+ * @typedef {import('../types/api/WorkRecord.types.js').WorkRecord} WorkRecord
11
+ * @typedef {import('../types/api/WorkRecord.types.js').WorkRecordActivity} WorkRecordActivity
12
+ * @typedef {import('../types/api/Tag.types.js').Tag} Tag
13
+ */
14
+
15
+ /**
16
+ * @typedef {Object} WorkRecordsApi
17
+ * @property {(params?: { limit?: number, status?: string, station?: string, line?: string, tag?: string }) => Promise<{ data: WorkRecord[], page_context: object|null }>} list
18
+ * @property {(params?: { status?: string, station?: string, line?: string, tag?: string }) => Promise<WorkRecord[]>} getAll
19
+ * @property {(id: string, opts?: { include?: string[] }) => Promise<WorkRecord|null>} get
20
+ * @property {(id: string) => Promise<WorkRecordActivity[]>} activity
21
+ * @property {(id: string) => Promise<any>} cancel
22
+ * @property {(id: string) => Promise<Tag[]>} getTags
23
+ * @property {(id: string, tagIds: string[]) => Promise<any>} setTags
24
+ */
25
+
26
+ /** @param {any} r @returns {any} */
27
+ const unwrap = (r) => r?.data ?? null;
28
+ /** @param {string} s */
29
+ const enc = (s) => encodeURIComponent(s);
30
+
31
+ /** Map caller params → API search params.
32
+ * `station` accepts an id or a short_code; the API resolves either. `line` is
33
+ * passed through as-is and matches every station on that line.
34
+ * @param {{ limit?: number, status?: string, station?: string, line?: string, tag?: string }} [params] */
35
+ function toSearchParams({ limit, status, station, line, tag } = {}) {
36
+ /** @type {Record<string, any>} */
37
+ const sp = {};
38
+ if (limit) sp.limit = limit;
39
+ if (status) sp.status = status;
40
+ if (station) sp.station = station;
41
+ if (line) sp.line = line;
42
+ if (tag) sp.tag = tag;
43
+ return sp;
44
+ }
45
+
46
+ /**
47
+ * @param {Transport} ctx
48
+ * @returns {WorkRecordsApi}
49
+ */
50
+ export function buildWorkRecords(ctx) {
51
+ return {
52
+ /** One page of work records. Returns the `{ data, page_context }` envelope. */
53
+ list: (params = {}) => ctx.get('/api/v1/work-records', { searchParams: toSearchParams(params) }),
54
+
55
+ /** Every matching work record, walking pagination. Returns a flat array. */
56
+ getAll: async (params = {}) =>
57
+ (await ctx.getAll('/api/v1/work-records', { searchParams: toSearchParams(params) })).data,
58
+
59
+ /** One work record. `include` pulls optional blocks: 'report', 'step_outputs', 'supporting_docs'. */
60
+ get: (id, { include } = {}) =>
61
+ ctx
62
+ .get(`/api/v1/work-records/${enc(id)}`, include?.length ? { searchParams: { include: include.join(',') } } : undefined)
63
+ .then(unwrap),
64
+
65
+ /** Activity events for a work record. */
66
+ activity: (id) => ctx.get(`/api/v1/work-records/${enc(id)}/activity`).then((r) => r?.data ?? []),
67
+
68
+ /** Cancel a running or pending work record. */
69
+ cancel: (id) => ctx.post(`/api/v1/work-records/${enc(id)}/cancel`).then(unwrap),
70
+
71
+ /** Current tags on a work record. */
72
+ getTags: (id) => ctx.get(`/api/v1/work-records/${enc(id)}/tags`).then((r) => r?.data ?? []),
73
+
74
+ /** Replace a work record's tags (full set, by tag id). */
75
+ setTags: (id, tagIds) => ctx.put(`/api/v1/work-records/${enc(id)}/tags`, { tags: tagIds }).then(unwrap),
76
+ };
77
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * A line, as returned by `/api/v1/lines`. A sequence of stations that runs at
3
+ * exactly one location; the line owns `location`, stations inherit it.
4
+ * @typedef {Object} Line
5
+ * @property {string} id
6
+ * @property {string} code Operator-facing handle, unique per org (`VM`, `BK-SR`).
7
+ * @property {string} name
8
+ * @property {string} [description]
9
+ * @property {string} location Location serving every station on the line.
10
+ * @property {LineStationRef[]} [stations] Live stations on the line (read-only, server-derived).
11
+ * @property {string} [created_at]
12
+ * @property {string|null} [archived_at]
13
+ */
14
+
15
+ /**
16
+ * @typedef {Object} LineStationRef
17
+ * @property {string} id
18
+ * @property {string|null} short_code
19
+ * @property {string} name
20
+ */
21
+
22
+ export {};
@@ -0,0 +1,34 @@
1
+ /**
2
+ * A station, as returned by `/api/v1/stations`.
3
+ * @typedef {Object} Station
4
+ * @property {string} id
5
+ * @property {string} name
6
+ * @property {string} [short_code]
7
+ * @property {string} line Line code. The line owns the location; a station has none of its own.
8
+ * @property {string} [description]
9
+ * @property {StationStep[]} [steps]
10
+ * @property {string[]} [dependencies] Station ids or short_codes this one depends on.
11
+ * @property {(string|Tag)[]} [tags]
12
+ * @property {string} [applies_to]
13
+ * @property {boolean} [archived]
14
+ * @property {string} [created_at]
15
+ */
16
+
17
+ /**
18
+ * One step within a station definition.
19
+ * @typedef {Object} StationStep
20
+ * @property {string} slug
21
+ * @property {Record<string, any>} [config]
22
+ */
23
+
24
+ /**
25
+ * The result of triggering a station run.
26
+ * @typedef {Object} StationRunResult
27
+ * @property {string} work_record_id
28
+ */
29
+
30
+ /**
31
+ * @typedef {import('./Tag.types.js').Tag} Tag
32
+ */
33
+
34
+ export {};
@@ -0,0 +1,14 @@
1
+ /**
2
+ * A supporting document, as returned by `/api/v1/supporting-docs/{id}`
3
+ * (metadata; `content` present for markdown docs).
4
+ * @typedef {Object} SupportingDoc
5
+ * @property {string} id
6
+ * @property {string} [name]
7
+ * @property {string} [type]
8
+ * @property {string} [mime_type]
9
+ * @property {string} [content]
10
+ * @property {string} [url]
11
+ * @property {string} [created_at]
12
+ */
13
+
14
+ export {};
@@ -0,0 +1,10 @@
1
+ /**
2
+ * A tag, as returned by `/api/v1/tags`.
3
+ * @typedef {Object} Tag
4
+ * @property {string} id
5
+ * @property {string} name
6
+ * @property {string} [color]
7
+ * @property {string} [description]
8
+ */
9
+
10
+ export {};
@@ -0,0 +1,27 @@
1
+ /**
2
+ * A work record — one run of a station, as returned by
3
+ * `/api/v1/work-records`.
4
+ * @typedef {Object} WorkRecord
5
+ * @property {string} id
6
+ * @property {string} [station] Station id.
7
+ * @property {string} [status]
8
+ * @property {(string|Tag)[]} [tags]
9
+ * @property {Record<string, any>} [report]
10
+ * @property {Record<string, any>[]} [step_outputs]
11
+ * @property {string} [created_at]
12
+ * @property {string} [updated_at]
13
+ */
14
+
15
+ /**
16
+ * One activity event on a work record.
17
+ * @typedef {Object} WorkRecordActivity
18
+ * @property {string} [type]
19
+ * @property {string} [message]
20
+ * @property {string} [created_at]
21
+ */
22
+
23
+ /**
24
+ * @typedef {import('./Tag.types.js').Tag} Tag
25
+ */
26
+
27
+ export {};
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Orchestrator API credentials, passed explicitly to `alOrc(...)`. The library
3
+ * never reads these from the environment — the caller injects them.
4
+ * @typedef {Object} Credentials
5
+ * @property {string} api_key
6
+ * @property {string} api_secret
7
+ * @property {string} [api_url] Origin-only base URL (no /api/v1). Defaults to https://orchestrator.assemblyline.sh.
8
+ * @property {string} [location] Optional location, sent as `X-Location`.
9
+ */
10
+
11
+ /**
12
+ * Options accepted by the transport request methods.
13
+ * @typedef {Object} RequestOptions
14
+ * @property {Credentials} [credentials]
15
+ * @property {Record<string, any>} [searchParams]
16
+ */
17
+
18
+ export {};
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @typedef {import('./Credentials.types.js').Credentials} Credentials
3
+ * @typedef {import('./Credentials.types.js').RequestOptions} RequestOptions
4
+ */
5
+
6
+ /**
7
+ * A credential-bound transport (see `http.js` `createTransport`). Every method
8
+ * has the caller's credentials pre-applied, so the resource layer never threads
9
+ * them.
10
+ * @typedef {Object} Transport
11
+ * @property {(path: string, options?: RequestOptions) => Promise<any>} get
12
+ * @property {(path: string, body?: any, options?: RequestOptions) => Promise<any>} post
13
+ * @property {(path: string, body?: any, options?: RequestOptions) => Promise<any>} put
14
+ * @property {(path: string, body?: any, options?: RequestOptions) => Promise<any>} patch
15
+ * @property {(path: string, options?: RequestOptions) => Promise<any>} delete
16
+ * @property {(path: string, options?: RequestOptions, pageOpts?: { maxRows?: number, pageSize?: number }) => Promise<{ data: any[], page_context: object|null, truncated: boolean }>} getAll
17
+ * @property {(path: string) => Promise<{ response: Response, url: URL }>} getStream
18
+ * @property {(path: string, options?: RequestOptions) => URL} buildUrl
19
+ */
20
+
21
+ export {};
@@ -0,0 +1,8 @@
1
+ /**
2
+ * General/shared types — the credential + transport plumbing.
3
+ * @typedef {import('./Credentials.types.js').Credentials} Credentials
4
+ * @typedef {import('./Credentials.types.js').RequestOptions} RequestOptions
5
+ * @typedef {import('./Transport.types.js').Transport} Transport
6
+ */
7
+
8
+ export {};