@assemblyline/al-orc 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/CHANGELOG.md +78 -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 +68 -0
  8. package/src/cli/config/add.js +11 -0
  9. package/src/cli/config/current.js +21 -0
  10. package/src/cli/config/index.js +64 -0
  11. package/src/cli/config/list.js +23 -0
  12. package/src/cli/config/remove.js +7 -0
  13. package/src/cli/config/use.js +7 -0
  14. package/src/cli/config-store.js +128 -0
  15. package/src/cli/factory-sessions/index.js +36 -0
  16. package/src/cli/factory-sessions/list.js +94 -0
  17. package/src/cli/factory-sessions/show.js +84 -0
  18. package/src/cli/index.js +62 -0
  19. package/src/cli/lines/index.js +54 -0
  20. package/src/cli/lines/list.js +40 -0
  21. package/src/cli/lines/pull.js +47 -0
  22. package/src/cli/lines/push.js +83 -0
  23. package/src/cli/lines/show.js +37 -0
  24. package/src/cli/shared/edit-tags.js +44 -0
  25. package/src/cli/stations/archive.js +25 -0
  26. package/src/cli/stations/delete.js +9 -0
  27. package/src/cli/stations/edit.js +31 -0
  28. package/src/cli/stations/index.js +105 -0
  29. package/src/cli/stations/list.js +38 -0
  30. package/src/cli/stations/pull.js +74 -0
  31. package/src/cli/stations/push.js +213 -0
  32. package/src/cli/stations/run.js +94 -0
  33. package/src/cli/stations/show.js +75 -0
  34. package/src/cli/status.js +24 -0
  35. package/src/cli/supporting-docs/index.js +22 -0
  36. package/src/cli/supporting-docs/show.js +50 -0
  37. package/src/cli/tags/create.js +22 -0
  38. package/src/cli/tags/delete.js +9 -0
  39. package/src/cli/tags/edit.js +28 -0
  40. package/src/cli/tags/index.js +50 -0
  41. package/src/cli/tags/list.js +25 -0
  42. package/src/cli/utils/format.js +171 -0
  43. package/src/cli/utils/line-files.js +85 -0
  44. package/src/cli/utils/station-files.js +152 -0
  45. package/src/cli/utils/station-labels.js +49 -0
  46. package/src/cli/work-records/cancel.js +25 -0
  47. package/src/cli/work-records/edit.js +23 -0
  48. package/src/cli/work-records/index.js +61 -0
  49. package/src/cli/work-records/list.js +49 -0
  50. package/src/cli/work-records/show.js +104 -0
  51. package/src/http.js +254 -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 +78 -0
  59. package/src/types/api/Line.types.js +23 -0
  60. package/src/types/api/Station.types.js +36 -0
  61. package/src/types/api/SupportingDoc.types.js +20 -0
  62. package/src/types/api/Tag.types.js +12 -0
  63. package/src/types/api/WorkRecord.types.js +34 -0
  64. package/src/types/general/Credentials.types.js +18 -0
  65. package/src/types/general/Transport.types.js +22 -0
  66. package/src/types/general/index.js +9 -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,78 @@
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?: { page?: number, 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 {{ page?: number, limit?: number, status?: string, station?: string, line?: string, tag?: string }} [params] */
35
+ function toSearchParams({ page, limit, status, station, line, tag } = {}) {
36
+ /** @type {Record<string, any>} */
37
+ const sp = {};
38
+ if (page) sp.page = page;
39
+ if (limit) sp.limit = limit;
40
+ if (status) sp.status = status;
41
+ if (station) sp.station = station;
42
+ if (line) sp.line = line;
43
+ if (tag) sp.tag = tag;
44
+ return sp;
45
+ }
46
+
47
+ /**
48
+ * @param {Transport} ctx
49
+ * @returns {WorkRecordsApi}
50
+ */
51
+ export function buildWorkRecords(ctx) {
52
+ return {
53
+ /** One page of work records. Returns the `{ data, page_context }` envelope. */
54
+ list: (params = {}) => ctx.get('/api/v1/work-records', { searchParams: toSearchParams(params) }),
55
+
56
+ /** Every matching work record, walking pagination. Returns a flat array. */
57
+ getAll: async (params = {}) =>
58
+ (await ctx.getAll('/api/v1/work-records', { searchParams: toSearchParams(params) })).data,
59
+
60
+ /** One work record. `include` pulls optional blocks: 'report', 'step_outputs', 'supporting_docs'. */
61
+ get: (id, { include } = {}) =>
62
+ ctx
63
+ .get(`/api/v1/work-records/${enc(id)}`, include?.length ? { searchParams: { include: include.join(',') } } : undefined)
64
+ .then(unwrap),
65
+
66
+ /** Activity events for a work record. */
67
+ activity: (id) => ctx.get(`/api/v1/work-records/${enc(id)}/activity`).then((r) => r?.data ?? []),
68
+
69
+ /** Cancel a running or pending work record. */
70
+ cancel: (id) => ctx.post(`/api/v1/work-records/${enc(id)}/cancel`).then(unwrap),
71
+
72
+ /** Current tags on a work record. */
73
+ getTags: (id) => ctx.get(`/api/v1/work-records/${enc(id)}/tags`).then((r) => r?.data ?? []),
74
+
75
+ /** Replace a work record's tags (full set, by tag id). */
76
+ setTags: (id, tagIds) => ctx.put(`/api/v1/work-records/${enc(id)}/tags`, { tags: tagIds }).then(unwrap),
77
+ };
78
+ }
@@ -0,0 +1,23 @@
1
+ // @ts-check
2
+ /**
3
+ * A line, as returned by `/api/v1/lines`. A sequence of stations that runs at
4
+ * exactly one location; the line owns `location`, stations inherit it.
5
+ * @typedef {Object} Line
6
+ * @property {string} id
7
+ * @property {string} code Operator-facing handle, unique per org (`VM`, `BK-SR`).
8
+ * @property {string} name
9
+ * @property {string} [description]
10
+ * @property {string} location Location serving every station on the line.
11
+ * @property {LineStationRef[]} [stations] Live stations on the line (read-only, server-derived).
12
+ * @property {string} [created_at]
13
+ * @property {string|null} [archived_at]
14
+ */
15
+
16
+ /**
17
+ * @typedef {Object} LineStationRef
18
+ * @property {string} id
19
+ * @property {string|null} short_code
20
+ * @property {string} name
21
+ */
22
+
23
+ export {};
@@ -0,0 +1,36 @@
1
+ // @ts-check
2
+ /**
3
+ * A station, as returned by `/api/v1/stations`.
4
+ * @typedef {Object} Station
5
+ * @property {string} id
6
+ * @property {string} name
7
+ * @property {string} [short_code]
8
+ * @property {string} line Line code. The line owns the location; a station has none of its own.
9
+ * @property {string} [description]
10
+ * @property {StationStep[]} [steps]
11
+ * @property {(string|{ id: string, short_code?: string })[]} [dependencies] Station ids or short_codes this one depends on (or station objects).
12
+ * @property {(string|Tag)[]} [tags]
13
+ * @property {string} [applies_to]
14
+ * @property {boolean} [archived]
15
+ * @property {string|null} [archived_at]
16
+ * @property {string} [created_at]
17
+ */
18
+
19
+ /**
20
+ * One step within a station definition.
21
+ * @typedef {Object} StationStep
22
+ * @property {string} slug
23
+ * @property {Record<string, any>} [config]
24
+ */
25
+
26
+ /**
27
+ * The result of triggering a station run.
28
+ * @typedef {Object} StationRunResult
29
+ * @property {string} work_record_id
30
+ */
31
+
32
+ /**
33
+ * @typedef {import('./Tag.types.js').Tag} Tag
34
+ */
35
+
36
+ export {};
@@ -0,0 +1,20 @@
1
+ // @ts-check
2
+ /**
3
+ * A supporting document, as returned by `/api/v1/supporting-docs/{id}`
4
+ * (metadata; `content` present for markdown docs).
5
+ * @typedef {Object} SupportingDoc
6
+ * @property {string} id
7
+ * @property {string} [name]
8
+ * @property {string} [type]
9
+ * @property {string} [mime_type]
10
+ * @property {string} [content]
11
+ * @property {string} [url]
12
+ * @property {string} [title]
13
+ * @property {string} [filename]
14
+ * @property {number} [size_bytes]
15
+ * @property {string} [step_slug] The step that produced it.
16
+ * @property {string} [work_record_id]
17
+ * @property {string} [created_at]
18
+ */
19
+
20
+ export {};
@@ -0,0 +1,12 @@
1
+ // @ts-check
2
+ /**
3
+ * A tag, as returned by `/api/v1/tags`.
4
+ * @typedef {Object} Tag
5
+ * @property {string} id
6
+ * @property {string} name
7
+ * @property {string} [color]
8
+ * @property {string} [description]
9
+ * @property {{ total?: number }} [usage] How many records carry the tag.
10
+ */
11
+
12
+ export {};
@@ -0,0 +1,34 @@
1
+ // @ts-check
2
+ /**
3
+ * A work record — one run of a station, as returned by
4
+ * `/api/v1/work-records`.
5
+ * @typedef {Object} WorkRecord
6
+ * @property {string} id
7
+ * @property {string} [station] Station id.
8
+ * @property {string} [status]
9
+ * @property {(string|Tag)[]} [tags]
10
+ * @property {Record<string, any>} [report]
11
+ * @property {Record<string, any>[]} [step_outputs]
12
+ * @property {string} [created_at]
13
+ * @property {string} [updated_at]
14
+ * @property {string|null} [started_at]
15
+ * @property {string|null} [completed_at]
16
+ * @property {string|null} [performed_by] Who or what ran it (a factory session, or a user).
17
+ * @property {string|null} [error]
18
+ * @property {WorkRecordActivity[]} [activity] With `show --activity`.
19
+ * @property {import('./SupportingDoc.types.js').SupportingDoc[]} [supporting_docs] With `show --supporting-docs`.
20
+ */
21
+
22
+ /**
23
+ * One activity event on a work record.
24
+ * @typedef {Object} WorkRecordActivity
25
+ * @property {string} [type]
26
+ * @property {string} [message]
27
+ * @property {string} [created_at]
28
+ */
29
+
30
+ /**
31
+ * @typedef {import('./Tag.types.js').Tag} Tag
32
+ */
33
+
34
+ export {};
@@ -0,0 +1,18 @@
1
+ // @ts-check
2
+ /**
3
+ * Orchestrator API credentials, passed explicitly to `alOrc(...)`. The library
4
+ * never reads these from the environment — the caller injects them.
5
+ * @typedef {Object} Credentials
6
+ * @property {string} api_key
7
+ * @property {string} api_secret
8
+ * @property {string} [api_url] Origin-only base URL (no /api/v1). Defaults to https://orchestrator.assemblyline.sh.
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,22 @@
1
+ // @ts-check
2
+ /**
3
+ * @typedef {import('./Credentials.types.js').Credentials} Credentials
4
+ * @typedef {import('./Credentials.types.js').RequestOptions} RequestOptions
5
+ */
6
+
7
+ /**
8
+ * A credential-bound transport (see `http.js` `createTransport`). Every method
9
+ * has the caller's credentials pre-applied, so the resource layer never threads
10
+ * them.
11
+ * @typedef {Object} Transport
12
+ * @property {(path: string, options?: RequestOptions) => Promise<any>} get
13
+ * @property {(path: string, body?: any, options?: RequestOptions) => Promise<any>} post
14
+ * @property {(path: string, body?: any, options?: RequestOptions) => Promise<any>} put
15
+ * @property {(path: string, body?: any, options?: RequestOptions) => Promise<any>} patch
16
+ * @property {(path: string, options?: RequestOptions) => Promise<any>} delete
17
+ * @property {(path: string, options?: RequestOptions, pageOpts?: { maxRows?: number, pageSize?: number }) => Promise<{ data: any[], page_context: object|null, truncated: boolean }>} getAll
18
+ * @property {(path: string) => Promise<{ response: Response, url: URL }>} getStream
19
+ * @property {(path: string, options?: RequestOptions) => URL} buildUrl
20
+ */
21
+
22
+ export {};
@@ -0,0 +1,9 @@
1
+ // @ts-check
2
+ /**
3
+ * General/shared types — the credential + transport plumbing.
4
+ * @typedef {import('./Credentials.types.js').Credentials} Credentials
5
+ * @typedef {import('./Credentials.types.js').RequestOptions} RequestOptions
6
+ * @typedef {import('./Transport.types.js').Transport} Transport
7
+ */
8
+
9
+ export {};