@indigoai-us/hq-cloud 5.19.1 → 5.21.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.
- package/.github/workflows/ci.yml +8 -4
- package/.github/workflows/publish.yml +9 -3
- package/dist/bin/sync-runner.d.ts +9 -0
- package/dist/bin/sync-runner.d.ts.map +1 -1
- package/dist/bin/sync-runner.js +58 -0
- package/dist/bin/sync-runner.js.map +1 -1
- package/dist/entity-resolver.d.ts +53 -0
- package/dist/entity-resolver.d.ts.map +1 -0
- package/dist/entity-resolver.js +127 -0
- package/dist/entity-resolver.js.map +1 -0
- package/dist/entity-resolver.test.d.ts +10 -0
- package/dist/entity-resolver.test.d.ts.map +1 -0
- package/dist/entity-resolver.test.js +244 -0
- package/dist/entity-resolver.test.js.map +1 -0
- package/dist/index.d.ts +17 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -1
- package/dist/schemas/signal-types.d.ts +16 -0
- package/dist/schemas/signal-types.d.ts.map +1 -0
- package/dist/schemas/signal-types.js +30 -0
- package/dist/schemas/signal-types.js.map +1 -0
- package/dist/schemas/signal-types.test.d.ts +2 -0
- package/dist/schemas/signal-types.test.d.ts.map +1 -0
- package/dist/schemas/signal-types.test.js +65 -0
- package/dist/schemas/signal-types.test.js.map +1 -0
- package/dist/schemas/source-channels.d.ts +15 -0
- package/dist/schemas/source-channels.d.ts.map +1 -0
- package/dist/schemas/source-channels.js +28 -0
- package/dist/schemas/source-channels.js.map +1 -0
- package/dist/schemas/source-channels.test.d.ts +2 -0
- package/dist/schemas/source-channels.test.d.ts.map +1 -0
- package/dist/schemas/source-channels.test.js +65 -0
- package/dist/schemas/source-channels.test.js.map +1 -0
- package/dist/signals/get.d.ts +13 -0
- package/dist/signals/get.d.ts.map +1 -0
- package/dist/signals/get.js +74 -0
- package/dist/signals/get.js.map +1 -0
- package/dist/signals/get.test.d.ts +5 -0
- package/dist/signals/get.test.d.ts.map +1 -0
- package/dist/signals/get.test.js +170 -0
- package/dist/signals/get.test.js.map +1 -0
- package/dist/signals/internals.d.ts +16 -0
- package/dist/signals/internals.d.ts.map +1 -0
- package/dist/signals/internals.js +39 -0
- package/dist/signals/internals.js.map +1 -0
- package/dist/signals/list.d.ts +10 -0
- package/dist/signals/list.d.ts.map +1 -0
- package/dist/signals/list.js +76 -0
- package/dist/signals/list.js.map +1 -0
- package/dist/signals/list.test.d.ts +9 -0
- package/dist/signals/list.test.d.ts.map +1 -0
- package/dist/signals/list.test.js +227 -0
- package/dist/signals/list.test.js.map +1 -0
- package/dist/signals/parse.d.ts +8 -0
- package/dist/signals/parse.d.ts.map +1 -0
- package/dist/signals/parse.js +8 -0
- package/dist/signals/parse.js.map +1 -0
- package/dist/signals/types.d.ts +69 -0
- package/dist/signals/types.d.ts.map +1 -0
- package/dist/signals/types.js +10 -0
- package/dist/signals/types.js.map +1 -0
- package/dist/sources/get.d.ts +11 -0
- package/dist/sources/get.d.ts.map +1 -0
- package/dist/sources/get.js +67 -0
- package/dist/sources/get.js.map +1 -0
- package/dist/sources/get.test.d.ts +5 -0
- package/dist/sources/get.test.d.ts.map +1 -0
- package/dist/sources/get.test.js +132 -0
- package/dist/sources/get.test.js.map +1 -0
- package/dist/sources/internals.d.ts +16 -0
- package/dist/sources/internals.d.ts.map +1 -0
- package/dist/sources/internals.js +39 -0
- package/dist/sources/internals.js.map +1 -0
- package/dist/sources/list.d.ts +10 -0
- package/dist/sources/list.d.ts.map +1 -0
- package/dist/sources/list.js +76 -0
- package/dist/sources/list.js.map +1 -0
- package/dist/sources/list.test.d.ts +8 -0
- package/dist/sources/list.test.d.ts.map +1 -0
- package/dist/sources/list.test.js +198 -0
- package/dist/sources/list.test.js.map +1 -0
- package/dist/sources/parse.d.ts +18 -0
- package/dist/sources/parse.d.ts.map +1 -0
- package/dist/sources/parse.js +35 -0
- package/dist/sources/parse.js.map +1 -0
- package/dist/sources/types.d.ts +62 -0
- package/dist/sources/types.d.ts.map +1 -0
- package/dist/sources/types.js +8 -0
- package/dist/sources/types.js.map +1 -0
- package/dist/telemetry.d.ts +87 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +349 -0
- package/dist/telemetry.js.map +1 -0
- package/dist/telemetry.test.d.ts +11 -0
- package/dist/telemetry.test.d.ts.map +1 -0
- package/dist/telemetry.test.js +309 -0
- package/dist/telemetry.test.js.map +1 -0
- package/dist/vault-client.d.ts +43 -0
- package/dist/vault-client.d.ts.map +1 -1
- package/dist/vault-client.js +28 -0
- package/dist/vault-client.js.map +1 -1
- package/package.json +5 -3
- package/src/bin/sync-runner.ts +73 -0
- package/src/entity-resolver.test.ts +315 -0
- package/src/entity-resolver.ts +180 -0
- package/src/index.ts +76 -0
- package/src/schemas/signal-types.test.ts +82 -0
- package/src/schemas/signal-types.ts +38 -0
- package/src/schemas/source-channels.test.ts +82 -0
- package/src/schemas/source-channels.ts +36 -0
- package/src/signals/get.test.ts +204 -0
- package/src/signals/get.ts +79 -0
- package/src/signals/internals.ts +46 -0
- package/src/signals/list.test.ts +283 -0
- package/src/signals/list.ts +92 -0
- package/src/signals/parse.ts +8 -0
- package/src/signals/types.ts +74 -0
- package/src/sources/get.test.ts +166 -0
- package/src/sources/get.ts +75 -0
- package/src/sources/internals.ts +46 -0
- package/src/sources/list.test.ts +247 -0
- package/src/sources/list.ts +95 -0
- package/src/sources/parse.ts +43 -0
- package/src/sources/types.ts +67 -0
- package/src/telemetry.test.ts +394 -0
- package/src/telemetry.ts +436 -0
- package/src/vault-client.ts +60 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for signals/list.ts.
|
|
3
|
+
*
|
|
4
|
+
* Uses an in-memory S3Client stub installed via the internal
|
|
5
|
+
* `_setSignalsS3Factory` hook so we don't have to stand up vi.mock for
|
|
6
|
+
* @aws-sdk/client-s3.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
10
|
+
import {
|
|
11
|
+
GetObjectCommand,
|
|
12
|
+
ListObjectsV2Command,
|
|
13
|
+
type S3Client,
|
|
14
|
+
} from "@aws-sdk/client-s3";
|
|
15
|
+
import { listSignals } from "./list.js";
|
|
16
|
+
import {
|
|
17
|
+
_setSignalsS3Factory,
|
|
18
|
+
_resetSignalsS3Factory,
|
|
19
|
+
} from "./internals.js";
|
|
20
|
+
import type { EntityContext } from "../types.js";
|
|
21
|
+
import { InvalidSignalTypeError, SIGNAL_TYPES } from "../schemas/signal-types.js";
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// Fixtures + helpers
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
const ENTITY: EntityContext = {
|
|
28
|
+
uid: "cmp_test_001",
|
|
29
|
+
slug: "test",
|
|
30
|
+
bucketName: "hq-vault-test",
|
|
31
|
+
region: "us-east-1",
|
|
32
|
+
credentials: {
|
|
33
|
+
accessKeyId: "ASIATEST",
|
|
34
|
+
secretAccessKey: "secret",
|
|
35
|
+
sessionToken: "session",
|
|
36
|
+
},
|
|
37
|
+
expiresAt: new Date(Date.now() + 3600_000).toISOString(),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const ACTION_ITEM_MD = `---
|
|
41
|
+
signal_id: sig_action_001
|
|
42
|
+
signal_type: action_item
|
|
43
|
+
source_ref: abc-meeting-001
|
|
44
|
+
citations:
|
|
45
|
+
- "Stefan to review the sources pipeline implementation by end of week"
|
|
46
|
+
entity_refs:
|
|
47
|
+
- stefan@indigoai.us
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
Stefan will review the sources pipeline implementation.
|
|
51
|
+
`;
|
|
52
|
+
|
|
53
|
+
interface StoredObject {
|
|
54
|
+
key: string;
|
|
55
|
+
content: string;
|
|
56
|
+
lastModified: Date;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function buildStub(
|
|
60
|
+
objects: StoredObject[],
|
|
61
|
+
observed: { commands: unknown[] } = { commands: [] },
|
|
62
|
+
): S3Client {
|
|
63
|
+
async function send(command: unknown): Promise<unknown> {
|
|
64
|
+
observed.commands.push(command);
|
|
65
|
+
|
|
66
|
+
if (command instanceof ListObjectsV2Command) {
|
|
67
|
+
const { Prefix, MaxKeys, ContinuationToken } = command.input as {
|
|
68
|
+
Prefix?: string;
|
|
69
|
+
MaxKeys?: number;
|
|
70
|
+
ContinuationToken?: string;
|
|
71
|
+
};
|
|
72
|
+
const filtered = objects.filter((o) => !Prefix || o.key.startsWith(Prefix));
|
|
73
|
+
const offset = ContinuationToken ? parseInt(ContinuationToken, 10) : 0;
|
|
74
|
+
const max = MaxKeys ?? 1000;
|
|
75
|
+
const page = filtered.slice(offset, offset + max);
|
|
76
|
+
const nextOffset = offset + page.length;
|
|
77
|
+
const truncated = nextOffset < filtered.length;
|
|
78
|
+
return {
|
|
79
|
+
Contents: page.map((o) => ({
|
|
80
|
+
Key: o.key,
|
|
81
|
+
Size: Buffer.byteLength(o.content, "utf-8"),
|
|
82
|
+
LastModified: o.lastModified,
|
|
83
|
+
ETag: '"mock"',
|
|
84
|
+
})),
|
|
85
|
+
IsTruncated: truncated,
|
|
86
|
+
NextContinuationToken: truncated ? String(nextOffset) : undefined,
|
|
87
|
+
$metadata: {},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (command instanceof GetObjectCommand) {
|
|
92
|
+
const { Key } = command.input as { Key: string };
|
|
93
|
+
const found = objects.find((o) => o.key === Key);
|
|
94
|
+
if (!found) {
|
|
95
|
+
const err = new Error(`NoSuchKey: ${Key}`);
|
|
96
|
+
(err as Error & { name: string }).name = "NoSuchKey";
|
|
97
|
+
throw err;
|
|
98
|
+
}
|
|
99
|
+
const content = found.content;
|
|
100
|
+
async function* stream(): AsyncIterable<Uint8Array> {
|
|
101
|
+
yield Buffer.from(content, "utf-8");
|
|
102
|
+
}
|
|
103
|
+
return { Body: stream(), $metadata: {} };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
throw new Error(
|
|
107
|
+
`unhandled command: ${(command as { constructor?: { name?: string } }).constructor?.name}`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
return { send } as unknown as S3Client;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function installStub(objects: StoredObject[]): { commands: unknown[] } {
|
|
114
|
+
const observed = { commands: [] as unknown[] };
|
|
115
|
+
const stub = buildStub(objects, observed);
|
|
116
|
+
_setSignalsS3Factory(() => stub);
|
|
117
|
+
return observed;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
beforeEach(() => {
|
|
121
|
+
_resetSignalsS3Factory();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
afterEach(() => {
|
|
125
|
+
_resetSignalsS3Factory();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// ---------------------------------------------------------------------------
|
|
129
|
+
// Tests
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
|
|
132
|
+
describe("listSignals", () => {
|
|
133
|
+
it("happy path: lists one signal under action_item", async () => {
|
|
134
|
+
installStub([
|
|
135
|
+
{
|
|
136
|
+
key: "signals/action_item/foo.md",
|
|
137
|
+
content: ACTION_ITEM_MD,
|
|
138
|
+
lastModified: new Date("2026-03-15T15:00:00Z"),
|
|
139
|
+
},
|
|
140
|
+
]);
|
|
141
|
+
|
|
142
|
+
const result = await listSignals({ entity: ENTITY, signalType: "action_item" });
|
|
143
|
+
|
|
144
|
+
expect(result.entries).toHaveLength(1);
|
|
145
|
+
expect(result.entries[0].signalId).toBe("foo");
|
|
146
|
+
expect(result.entries[0].signalType).toBe("action_item");
|
|
147
|
+
expect(result.entries[0].key).toBe("signals/action_item/foo.md");
|
|
148
|
+
expect(result.entries[0].size).toBeGreaterThan(0);
|
|
149
|
+
expect(result.entries[0].frontmatter).toBeUndefined();
|
|
150
|
+
expect(result.entries[0].sourceRef).toBeUndefined();
|
|
151
|
+
expect(result.nextToken).toBeUndefined();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("supports all six canonical signal types", async () => {
|
|
155
|
+
// Build one object per signal type, verify each is listable.
|
|
156
|
+
const objects: StoredObject[] = SIGNAL_TYPES.map((t) => ({
|
|
157
|
+
key: `signals/${t}/sig-${t}.md`,
|
|
158
|
+
content: ACTION_ITEM_MD,
|
|
159
|
+
lastModified: new Date("2026-03-15T15:00:00Z"),
|
|
160
|
+
}));
|
|
161
|
+
|
|
162
|
+
for (const signalType of SIGNAL_TYPES) {
|
|
163
|
+
installStub(objects);
|
|
164
|
+
const result = await listSignals({ entity: ENTITY, signalType });
|
|
165
|
+
expect(result.entries).toHaveLength(1);
|
|
166
|
+
expect(result.entries[0].signalId).toBe(`sig-${signalType}`);
|
|
167
|
+
expect(result.entries[0].signalType).toBe(signalType);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("pagination: returns nextToken and accepts it on the next call", async () => {
|
|
172
|
+
const objects: StoredObject[] = Array.from({ length: 5 }, (_, i) => ({
|
|
173
|
+
key: `signals/action_item/s${i}.md`,
|
|
174
|
+
content: ACTION_ITEM_MD,
|
|
175
|
+
lastModified: new Date("2026-03-15T15:00:00Z"),
|
|
176
|
+
}));
|
|
177
|
+
installStub(objects);
|
|
178
|
+
|
|
179
|
+
const page1 = await listSignals({
|
|
180
|
+
entity: ENTITY,
|
|
181
|
+
signalType: "action_item",
|
|
182
|
+
limit: 2,
|
|
183
|
+
});
|
|
184
|
+
expect(page1.entries.map((e) => e.signalId)).toEqual(["s0", "s1"]);
|
|
185
|
+
expect(page1.nextToken).toBe("2");
|
|
186
|
+
|
|
187
|
+
const page2 = await listSignals({
|
|
188
|
+
entity: ENTITY,
|
|
189
|
+
signalType: "action_item",
|
|
190
|
+
limit: 2,
|
|
191
|
+
continuationToken: page1.nextToken,
|
|
192
|
+
});
|
|
193
|
+
expect(page2.entries.map((e) => e.signalId)).toEqual(["s2", "s3"]);
|
|
194
|
+
expect(page2.nextToken).toBe("4");
|
|
195
|
+
|
|
196
|
+
const page3 = await listSignals({
|
|
197
|
+
entity: ENTITY,
|
|
198
|
+
signalType: "action_item",
|
|
199
|
+
limit: 2,
|
|
200
|
+
continuationToken: page2.nextToken,
|
|
201
|
+
});
|
|
202
|
+
expect(page3.entries.map((e) => e.signalId)).toEqual(["s4"]);
|
|
203
|
+
expect(page3.nextToken).toBeUndefined();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("includeFrontmatter:true fetches each object, parses YAML, surfaces sourceRef", async () => {
|
|
207
|
+
const observed = installStub([
|
|
208
|
+
{
|
|
209
|
+
key: "signals/action_item/foo.md",
|
|
210
|
+
content: ACTION_ITEM_MD,
|
|
211
|
+
lastModified: new Date("2026-03-15T15:00:00Z"),
|
|
212
|
+
},
|
|
213
|
+
]);
|
|
214
|
+
|
|
215
|
+
const result = await listSignals({
|
|
216
|
+
entity: ENTITY,
|
|
217
|
+
signalType: "action_item",
|
|
218
|
+
includeFrontmatter: true,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
expect(result.entries).toHaveLength(1);
|
|
222
|
+
expect(result.entries[0].sourceRef).toBe("abc-meeting-001");
|
|
223
|
+
expect(result.entries[0].frontmatter).toMatchObject({
|
|
224
|
+
signal_id: "sig_action_001",
|
|
225
|
+
signal_type: "action_item",
|
|
226
|
+
source_ref: "abc-meeting-001",
|
|
227
|
+
});
|
|
228
|
+
expect(observed.commands.filter((c) => c instanceof GetObjectCommand)).toHaveLength(1);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("includeFrontmatter:true tolerates malformed frontmatter (sourceRef stays undefined)", async () => {
|
|
232
|
+
installStub([
|
|
233
|
+
{
|
|
234
|
+
key: "signals/action_item/bad.md",
|
|
235
|
+
content: "no frontmatter here, just body",
|
|
236
|
+
lastModified: new Date(),
|
|
237
|
+
},
|
|
238
|
+
]);
|
|
239
|
+
|
|
240
|
+
const result = await listSignals({
|
|
241
|
+
entity: ENTITY,
|
|
242
|
+
signalType: "action_item",
|
|
243
|
+
includeFrontmatter: true,
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
expect(result.entries).toHaveLength(1);
|
|
247
|
+
expect(result.entries[0].sourceRef).toBeUndefined();
|
|
248
|
+
expect(result.entries[0].frontmatter).toBeUndefined();
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("includeFrontmatter:false (default) does not perform GETs", async () => {
|
|
252
|
+
const observed = installStub([
|
|
253
|
+
{
|
|
254
|
+
key: "signals/action_item/foo.md",
|
|
255
|
+
content: ACTION_ITEM_MD,
|
|
256
|
+
lastModified: new Date(),
|
|
257
|
+
},
|
|
258
|
+
]);
|
|
259
|
+
|
|
260
|
+
await listSignals({ entity: ENTITY, signalType: "action_item" });
|
|
261
|
+
|
|
262
|
+
expect(observed.commands.filter((c) => c instanceof GetObjectCommand)).toHaveLength(0);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("uses the correct prefix per signal type", async () => {
|
|
266
|
+
const observed = installStub([]);
|
|
267
|
+
await listSignals({ entity: ENTITY, signalType: "decision" });
|
|
268
|
+
const listCmd = observed.commands[0] as ListObjectsV2Command;
|
|
269
|
+
expect((listCmd.input as { Prefix?: string }).Prefix).toBe("signals/decision/");
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("rejects an invalid signalType via assertSignalType BEFORE any S3 call", async () => {
|
|
273
|
+
const observed = installStub([]);
|
|
274
|
+
await expect(
|
|
275
|
+
listSignals({
|
|
276
|
+
entity: ENTITY,
|
|
277
|
+
// @ts-expect-error — invalid signal type intentional for negative test
|
|
278
|
+
signalType: "ramble",
|
|
279
|
+
}),
|
|
280
|
+
).rejects.toBeInstanceOf(InvalidSignalTypeError);
|
|
281
|
+
expect(observed.commands).toHaveLength(0);
|
|
282
|
+
});
|
|
283
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* listSignals — page through `signals/{signalType}/` keys in an entity bucket
|
|
3
|
+
* and return typed summaries.
|
|
4
|
+
*
|
|
5
|
+
* `sourceRef` is surfaced on each summary when the caller passes
|
|
6
|
+
* `includeFrontmatter: true` (otherwise the field is undefined).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
GetObjectCommand,
|
|
11
|
+
ListObjectsV2Command,
|
|
12
|
+
} from "@aws-sdk/client-s3";
|
|
13
|
+
import { assertSignalType } from "../schemas/signal-types.js";
|
|
14
|
+
import { getS3Client, streamToString } from "./internals.js";
|
|
15
|
+
import { parseFrontmatter } from "./parse.js";
|
|
16
|
+
import type {
|
|
17
|
+
ListSignalsOptions,
|
|
18
|
+
ListSignalsResult,
|
|
19
|
+
SignalSummary,
|
|
20
|
+
} from "./types.js";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Strip the `signals/{signalType}/` prefix and `.md` suffix from a key.
|
|
24
|
+
* Returns `null` if the key shape isn't recognised.
|
|
25
|
+
*/
|
|
26
|
+
function deriveSignalId(key: string, prefix: string): string | null {
|
|
27
|
+
if (!key.startsWith(prefix)) return null;
|
|
28
|
+
const tail = key.slice(prefix.length);
|
|
29
|
+
if (!tail.endsWith(".md")) return null;
|
|
30
|
+
return tail.slice(0, -".md".length);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function listSignals(opts: ListSignalsOptions): Promise<ListSignalsResult> {
|
|
34
|
+
// Validate signalType BEFORE any S3 call (acceptance criterion + agent-mitigation).
|
|
35
|
+
assertSignalType(opts.signalType);
|
|
36
|
+
|
|
37
|
+
const client = getS3Client(opts.entity);
|
|
38
|
+
const prefix = `signals/${opts.signalType}/`;
|
|
39
|
+
|
|
40
|
+
const response = await client.send(
|
|
41
|
+
new ListObjectsV2Command({
|
|
42
|
+
Bucket: opts.entity.bucketName,
|
|
43
|
+
Prefix: prefix,
|
|
44
|
+
MaxKeys: opts.limit,
|
|
45
|
+
ContinuationToken: opts.continuationToken,
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const entries: SignalSummary[] = [];
|
|
50
|
+
for (const obj of response.Contents ?? []) {
|
|
51
|
+
if (!obj.Key) continue;
|
|
52
|
+
const signalId = deriveSignalId(obj.Key, prefix);
|
|
53
|
+
if (!signalId) continue;
|
|
54
|
+
|
|
55
|
+
const summary: SignalSummary = {
|
|
56
|
+
signalId,
|
|
57
|
+
signalType: opts.signalType,
|
|
58
|
+
key: obj.Key,
|
|
59
|
+
lastModified: obj.LastModified ?? new Date(0),
|
|
60
|
+
size: obj.Size ?? 0,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
if (opts.includeFrontmatter) {
|
|
64
|
+
try {
|
|
65
|
+
const get = await client.send(
|
|
66
|
+
new GetObjectCommand({
|
|
67
|
+
Bucket: opts.entity.bucketName,
|
|
68
|
+
Key: obj.Key,
|
|
69
|
+
}),
|
|
70
|
+
);
|
|
71
|
+
const text = await streamToString(get.Body);
|
|
72
|
+
const fm = parseFrontmatter(text);
|
|
73
|
+
if (fm) {
|
|
74
|
+
summary.frontmatter = fm;
|
|
75
|
+
const sourceRef = fm.source_ref;
|
|
76
|
+
if (typeof sourceRef === "string") {
|
|
77
|
+
summary.sourceRef = sourceRef;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
} catch {
|
|
81
|
+
// Frontmatter fetch is best-effort; leave undefined on failure.
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
entries.push(summary);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
entries,
|
|
90
|
+
nextToken: response.IsTruncated ? response.NextContinuationToken : undefined,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* YAML frontmatter parsing for signal markdown documents.
|
|
3
|
+
*
|
|
4
|
+
* Reuses the same logic as sources/parse.ts — both surfaces emit the
|
|
5
|
+
* same `---\nYAML\n---\nbody` shape from the pipeline.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export { parseMarkdown, parseFrontmatter, type ParsedMarkdown } from "../sources/parse.js";
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public types for the signals read surface.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the sources types so downstream consumers (CLI, MCP) get a
|
|
5
|
+
* uniform shape across both surfaces. `sourceRef`, `citations`, and
|
|
6
|
+
* `entity_refs` are surfaced as typed fields on the full document
|
|
7
|
+
* because they're the cross-reference primitives an agent needs.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { SignalType } from "../schemas/signal-types.js";
|
|
11
|
+
import type { EntityContext } from "../types.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Lightweight summary returned by listSignals(). `frontmatter` and
|
|
15
|
+
* `sourceRef` are only populated when the caller passes
|
|
16
|
+
* `includeFrontmatter: true`.
|
|
17
|
+
*/
|
|
18
|
+
export interface SignalSummary {
|
|
19
|
+
/** Stable ID derived from the object key (filename minus `.md`). */
|
|
20
|
+
signalId: string;
|
|
21
|
+
signalType: SignalType;
|
|
22
|
+
/** Full S3 object key (e.g. "signals/action_item/foo.md"). */
|
|
23
|
+
key: string;
|
|
24
|
+
lastModified: Date;
|
|
25
|
+
size: number;
|
|
26
|
+
/** Convenience field parsed from frontmatter.source_ref when includeFrontmatter:true. */
|
|
27
|
+
sourceRef?: string;
|
|
28
|
+
/** Parsed YAML frontmatter — only present when includeFrontmatter:true. */
|
|
29
|
+
frontmatter?: Record<string, unknown>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Full signal document returned by getSignal().
|
|
34
|
+
*
|
|
35
|
+
* `sourceRef`, `citations`, and `entity_refs` are surfaced as typed
|
|
36
|
+
* fields so callers don't have to fish inside `frontmatter`. They're
|
|
37
|
+
* still present on `frontmatter` too.
|
|
38
|
+
*/
|
|
39
|
+
export interface SignalDocument {
|
|
40
|
+
key: string;
|
|
41
|
+
/** Parsed YAML frontmatter, or `null` if the document had no frontmatter block. */
|
|
42
|
+
frontmatter: Record<string, unknown> | null;
|
|
43
|
+
/** Markdown body (everything after the closing `---`). */
|
|
44
|
+
body: string;
|
|
45
|
+
/** frontmatter.source_ref, if present and a string. */
|
|
46
|
+
sourceRef?: string;
|
|
47
|
+
/** frontmatter.citations[], if present and an array. */
|
|
48
|
+
citations?: string[];
|
|
49
|
+
/** frontmatter.entity_refs[], if present and an array. */
|
|
50
|
+
entityRefs?: string[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ListSignalsOptions {
|
|
54
|
+
entity: EntityContext;
|
|
55
|
+
signalType: SignalType;
|
|
56
|
+
/** Max keys per page; defaults to S3 default (1000). */
|
|
57
|
+
limit?: number;
|
|
58
|
+
/** Opaque continuation token returned by a prior page. */
|
|
59
|
+
continuationToken?: string;
|
|
60
|
+
/** When true, fetches+parses each entry's frontmatter (extra GETs). */
|
|
61
|
+
includeFrontmatter?: boolean;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface ListSignalsResult {
|
|
65
|
+
entries: SignalSummary[];
|
|
66
|
+
/** Present when the result was truncated; pass back as continuationToken. */
|
|
67
|
+
nextToken?: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface GetSignalOptions {
|
|
71
|
+
entity: EntityContext;
|
|
72
|
+
signalType: SignalType;
|
|
73
|
+
signalId: string;
|
|
74
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for sources/get.ts.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
6
|
+
import { GetObjectCommand, type S3Client } from "@aws-sdk/client-s3";
|
|
7
|
+
import { getSource, SourceNotFoundError } from "./get.js";
|
|
8
|
+
import {
|
|
9
|
+
_setSourcesS3Factory,
|
|
10
|
+
_resetSourcesS3Factory,
|
|
11
|
+
} from "./internals.js";
|
|
12
|
+
import type { EntityContext } from "../types.js";
|
|
13
|
+
import { InvalidSourceChannelError } from "../schemas/source-channels.js";
|
|
14
|
+
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
// Fixtures
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
|
|
19
|
+
const ENTITY: EntityContext = {
|
|
20
|
+
uid: "cmp_test_001",
|
|
21
|
+
slug: "test",
|
|
22
|
+
bucketName: "hq-vault-test",
|
|
23
|
+
region: "us-east-1",
|
|
24
|
+
credentials: {
|
|
25
|
+
accessKeyId: "ASIATEST",
|
|
26
|
+
secretAccessKey: "secret",
|
|
27
|
+
sessionToken: "session",
|
|
28
|
+
},
|
|
29
|
+
expiresAt: new Date(Date.now() + 3600_000).toISOString(),
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const MEETING_MD = `---
|
|
33
|
+
source_id: abc
|
|
34
|
+
source_type: meeting
|
|
35
|
+
title: Q1 Planning
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
Body text line 1.
|
|
39
|
+
Body text line 2.
|
|
40
|
+
`;
|
|
41
|
+
|
|
42
|
+
const RAW_PAYLOAD = { foo: "bar", n: 42 };
|
|
43
|
+
|
|
44
|
+
function installStub(objects: Record<string, string>): void {
|
|
45
|
+
async function send(command: unknown): Promise<unknown> {
|
|
46
|
+
if (command instanceof GetObjectCommand) {
|
|
47
|
+
const { Key } = command.input as { Key: string };
|
|
48
|
+
const content = objects[Key];
|
|
49
|
+
if (content === undefined) {
|
|
50
|
+
const err = new Error(`NoSuchKey: ${Key}`);
|
|
51
|
+
(err as Error & { name: string }).name = "NoSuchKey";
|
|
52
|
+
throw err;
|
|
53
|
+
}
|
|
54
|
+
async function* stream(): AsyncIterable<Uint8Array> {
|
|
55
|
+
yield Buffer.from(content, "utf-8");
|
|
56
|
+
}
|
|
57
|
+
return { Body: stream(), $metadata: {} };
|
|
58
|
+
}
|
|
59
|
+
throw new Error(`unhandled command`);
|
|
60
|
+
}
|
|
61
|
+
_setSourcesS3Factory(() => ({ send } as unknown as S3Client));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
beforeEach(() => _resetSourcesS3Factory());
|
|
65
|
+
afterEach(() => _resetSourcesS3Factory());
|
|
66
|
+
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
// Tests
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
|
|
71
|
+
describe("getSource", () => {
|
|
72
|
+
it("happy path: parses frontmatter and body", async () => {
|
|
73
|
+
installStub({ "sources/meeting/abc.md": MEETING_MD });
|
|
74
|
+
|
|
75
|
+
const doc = await getSource({
|
|
76
|
+
entity: ENTITY,
|
|
77
|
+
channel: "meeting",
|
|
78
|
+
sourceId: "abc",
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
expect(doc.key).toBe("sources/meeting/abc.md");
|
|
82
|
+
expect(doc.frontmatter).toEqual({
|
|
83
|
+
source_id: "abc",
|
|
84
|
+
source_type: "meeting",
|
|
85
|
+
title: "Q1 Planning",
|
|
86
|
+
});
|
|
87
|
+
expect(doc.body).toContain("Body text line 1.");
|
|
88
|
+
expect(doc.body).toContain("Body text line 2.");
|
|
89
|
+
expect(doc.raw).toBeUndefined();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("throws SourceNotFoundError on NoSuchKey", async () => {
|
|
93
|
+
installStub({});
|
|
94
|
+
|
|
95
|
+
await expect(
|
|
96
|
+
getSource({ entity: ENTITY, channel: "meeting", sourceId: "missing" }),
|
|
97
|
+
).rejects.toBeInstanceOf(SourceNotFoundError);
|
|
98
|
+
|
|
99
|
+
await expect(
|
|
100
|
+
getSource({ entity: ENTITY, channel: "meeting", sourceId: "missing" }),
|
|
101
|
+
).rejects.toThrow(/sources\/meeting\/missing\.md/);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("includeRaw:true fetches the .raw.json sibling", async () => {
|
|
105
|
+
installStub({
|
|
106
|
+
"sources/meeting/abc.md": MEETING_MD,
|
|
107
|
+
"sources/meeting/abc.raw.json": JSON.stringify(RAW_PAYLOAD),
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const doc = await getSource({
|
|
111
|
+
entity: ENTITY,
|
|
112
|
+
channel: "meeting",
|
|
113
|
+
sourceId: "abc",
|
|
114
|
+
includeRaw: true,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
expect(doc.raw).toEqual(RAW_PAYLOAD);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("includeRaw:true tolerates missing .raw.json (raw stays undefined)", async () => {
|
|
121
|
+
installStub({ "sources/meeting/abc.md": MEETING_MD });
|
|
122
|
+
|
|
123
|
+
const doc = await getSource({
|
|
124
|
+
entity: ENTITY,
|
|
125
|
+
channel: "meeting",
|
|
126
|
+
sourceId: "abc",
|
|
127
|
+
includeRaw: true,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
expect(doc.frontmatter).not.toBeNull();
|
|
131
|
+
expect(doc.raw).toBeUndefined();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("rejects an invalid channel via assertSourceChannel BEFORE any S3 call", async () => {
|
|
135
|
+
let called = 0;
|
|
136
|
+
_setSourcesS3Factory(() => ({
|
|
137
|
+
send: async () => {
|
|
138
|
+
called++;
|
|
139
|
+
return {};
|
|
140
|
+
},
|
|
141
|
+
} as unknown as S3Client));
|
|
142
|
+
|
|
143
|
+
await expect(
|
|
144
|
+
getSource({
|
|
145
|
+
entity: ENTITY,
|
|
146
|
+
// @ts-expect-error — invalid channel intentional
|
|
147
|
+
channel: "pigeon",
|
|
148
|
+
sourceId: "abc",
|
|
149
|
+
}),
|
|
150
|
+
).rejects.toBeInstanceOf(InvalidSourceChannelError);
|
|
151
|
+
expect(called).toBe(0);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("returns frontmatter:null for malformed (no frontmatter block) documents", async () => {
|
|
155
|
+
installStub({ "sources/meeting/plain.md": "Just a body, no frontmatter." });
|
|
156
|
+
|
|
157
|
+
const doc = await getSource({
|
|
158
|
+
entity: ENTITY,
|
|
159
|
+
channel: "meeting",
|
|
160
|
+
sourceId: "plain",
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
expect(doc.frontmatter).toBeNull();
|
|
164
|
+
expect(doc.body).toBe("Just a body, no frontmatter.");
|
|
165
|
+
});
|
|
166
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* getSource — fetch a single source document (and optionally its `.raw.json`
|
|
3
|
+
* sibling) from an entity bucket and return parsed frontmatter + body.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { GetObjectCommand } from "@aws-sdk/client-s3";
|
|
7
|
+
import { assertSourceChannel } from "../schemas/source-channels.js";
|
|
8
|
+
import { getS3Client, streamToString } from "./internals.js";
|
|
9
|
+
import { parseMarkdown } from "./parse.js";
|
|
10
|
+
import type { GetSourceOptions, SourceDocument } from "./types.js";
|
|
11
|
+
|
|
12
|
+
export class SourceNotFoundError extends Error {
|
|
13
|
+
constructor(public readonly key: string) {
|
|
14
|
+
super(`Source not found: ${key}`);
|
|
15
|
+
this.name = "SourceNotFoundError";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function isNoSuchKey(err: unknown): boolean {
|
|
20
|
+
if (!err || typeof err !== "object") return false;
|
|
21
|
+
const name = (err as { name?: unknown }).name;
|
|
22
|
+
// AWS SDK v3 throws either a `NoSuchKey` exception or a `NotFound` head error.
|
|
23
|
+
return name === "NoSuchKey" || name === "NoSuchKeyException";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function getSource(opts: GetSourceOptions): Promise<SourceDocument> {
|
|
27
|
+
// Validate channel BEFORE any S3 call (acceptance criterion).
|
|
28
|
+
assertSourceChannel(opts.channel);
|
|
29
|
+
|
|
30
|
+
const client = getS3Client(opts.entity);
|
|
31
|
+
const key = `sources/${opts.channel}/${opts.sourceId}.md`;
|
|
32
|
+
|
|
33
|
+
let body: string;
|
|
34
|
+
try {
|
|
35
|
+
const response = await client.send(
|
|
36
|
+
new GetObjectCommand({
|
|
37
|
+
Bucket: opts.entity.bucketName,
|
|
38
|
+
Key: key,
|
|
39
|
+
}),
|
|
40
|
+
);
|
|
41
|
+
body = await streamToString(response.Body);
|
|
42
|
+
} catch (err) {
|
|
43
|
+
if (isNoSuchKey(err)) {
|
|
44
|
+
throw new SourceNotFoundError(key);
|
|
45
|
+
}
|
|
46
|
+
throw err;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const parsed = parseMarkdown(body);
|
|
50
|
+
|
|
51
|
+
const document: SourceDocument = {
|
|
52
|
+
key,
|
|
53
|
+
frontmatter: parsed.frontmatter,
|
|
54
|
+
body: parsed.body,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
if (opts.includeRaw) {
|
|
58
|
+
const rawKey = `sources/${opts.channel}/${opts.sourceId}.raw.json`;
|
|
59
|
+
try {
|
|
60
|
+
const rawResponse = await client.send(
|
|
61
|
+
new GetObjectCommand({
|
|
62
|
+
Bucket: opts.entity.bucketName,
|
|
63
|
+
Key: rawKey,
|
|
64
|
+
}),
|
|
65
|
+
);
|
|
66
|
+
const rawText = await streamToString(rawResponse.Body);
|
|
67
|
+
document.raw = JSON.parse(rawText);
|
|
68
|
+
} catch (err) {
|
|
69
|
+
if (!isNoSuchKey(err)) throw err;
|
|
70
|
+
// Raw sibling is optional; leave undefined when absent.
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return document;
|
|
75
|
+
}
|