@inflexa-ai/harness 0.15.1 → 0.17.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 (112) hide show
  1. package/dist/agents/conversation-agent.d.ts +3 -0
  2. package/dist/agents/conversation-agent.js +5 -2
  3. package/dist/agents/sandbox/shared.d.ts +3 -0
  4. package/dist/agents/sandbox/shared.js +5 -0
  5. package/dist/agents/sandbox/types.d.ts +1 -1
  6. package/dist/app/chat-turn.d.ts +2 -0
  7. package/dist/app/chat-turn.js +11 -2
  8. package/dist/app/synthesize-run.d.ts +3 -0
  9. package/dist/app/synthesize-run.js +2 -1
  10. package/dist/citations/__fixtures__/resolver.d.ts +3 -0
  11. package/dist/citations/__fixtures__/resolver.js +9 -0
  12. package/dist/citations/aggregate.d.ts +3 -0
  13. package/dist/citations/aggregate.js +44 -0
  14. package/dist/citations/clients/arxiv.d.ts +6 -0
  15. package/dist/citations/clients/arxiv.js +34 -0
  16. package/dist/citations/clients/common.d.ts +11 -0
  17. package/dist/citations/clients/common.js +43 -0
  18. package/dist/citations/clients/crossref.d.ts +8 -0
  19. package/dist/citations/clients/crossref.js +98 -0
  20. package/dist/citations/clients/doi-registry.d.ts +3 -0
  21. package/dist/citations/clients/doi-registry.js +95 -0
  22. package/dist/citations/clients/pubmed.d.ts +6 -0
  23. package/dist/citations/clients/pubmed.js +83 -0
  24. package/dist/citations/clients/semantic-scholar.d.ts +6 -0
  25. package/dist/citations/clients/semantic-scholar.js +60 -0
  26. package/dist/citations/compare.d.ts +9 -0
  27. package/dist/citations/compare.js +72 -0
  28. package/dist/citations/match.d.ts +11 -0
  29. package/dist/citations/match.js +173 -0
  30. package/dist/citations/normalize.d.ts +31 -0
  31. package/dist/citations/normalize.js +187 -0
  32. package/dist/citations/plan.d.ts +2 -0
  33. package/dist/citations/plan.js +59 -0
  34. package/dist/citations/resolve.d.ts +28 -0
  35. package/dist/citations/resolve.js +238 -0
  36. package/dist/citations/types.d.ts +583 -0
  37. package/dist/citations/types.js +129 -0
  38. package/dist/contracts/chat-events.d.ts +16 -0
  39. package/dist/contracts/message.d.ts +13 -8
  40. package/dist/contracts/schemas/chat-events.d.ts +7 -0
  41. package/dist/contracts/schemas/chat-events.js +2 -0
  42. package/dist/contracts/schemas/chat-parts.d.ts +8 -8
  43. package/dist/contracts/target-dossier.d.ts +20 -20
  44. package/dist/execution/run-synthesis.d.ts +3 -0
  45. package/dist/execution/run-synthesis.js +1 -0
  46. package/dist/index.d.ts +14 -10
  47. package/dist/index.js +20 -16
  48. package/dist/lib/cache.d.ts +8 -0
  49. package/dist/lib/cache.js +35 -0
  50. package/dist/lib/rate-limit.d.ts +35 -0
  51. package/dist/lib/rate-limit.js +121 -0
  52. package/dist/literature/sources/arxiv.d.ts +22 -0
  53. package/dist/literature/sources/arxiv.js +103 -0
  54. package/dist/literature/sources/http.d.ts +28 -0
  55. package/dist/literature/sources/http.js +97 -0
  56. package/dist/literature/sources/pubmed.d.ts +68 -0
  57. package/dist/literature/sources/pubmed.js +266 -0
  58. package/dist/literature/sources/semantic-scholar.d.ts +24 -0
  59. package/dist/literature/sources/semantic-scholar.js +71 -0
  60. package/dist/memory/content-to-cortex.d.ts +12 -11
  61. package/dist/memory/content-to-cortex.js +17 -14
  62. package/dist/memory/conversation-display-backfill.d.ts +25 -0
  63. package/dist/memory/conversation-display-backfill.js +97 -0
  64. package/dist/memory/conversation-display-recorder.d.ts +17 -0
  65. package/dist/memory/conversation-display-recorder.js +156 -0
  66. package/dist/memory/conversation-display-replay.d.ts +19 -0
  67. package/dist/memory/conversation-display-replay.js +39 -0
  68. package/dist/memory/conversation-display-storage.d.ts +86 -0
  69. package/dist/memory/conversation-display-storage.js +168 -0
  70. package/dist/memory/reconstruct-cards.d.ts +7 -1
  71. package/dist/memory/reconstruct-cards.js +5 -1
  72. package/dist/memory/thread-history.d.ts +46 -11
  73. package/dist/memory/thread-history.js +55 -13
  74. package/dist/memory/thread-store.d.ts +142 -25
  75. package/dist/memory/thread-store.js +228 -40
  76. package/dist/prompts/literature-reviewer.d.ts +1 -1
  77. package/dist/prompts/literature-reviewer.js +2 -0
  78. package/dist/runtime/assemble.d.ts +50 -4
  79. package/dist/runtime/assemble.js +47 -3
  80. package/dist/runtime/boot.d.ts +8 -2
  81. package/dist/runtime/boot.js +20 -2
  82. package/dist/state/init.js +50 -1
  83. package/dist/state/schema.d.ts +2 -2
  84. package/dist/tools/bio/gene-preclinical-profile.d.ts +1 -1
  85. package/dist/tools/bio/keys.d.ts +1 -0
  86. package/dist/tools/bio/pubmed.js +4 -4
  87. package/dist/tools/lib/ncbi-utils.d.ts +3 -58
  88. package/dist/tools/lib/ncbi-utils.js +2 -286
  89. package/dist/tools/lib/pubmed-client.d.ts +5 -12
  90. package/dist/tools/lib/pubmed-client.js +20 -79
  91. package/dist/tools/research/generate-analogy-report.js +2 -2
  92. package/dist/tools/research/index.d.ts +1 -0
  93. package/dist/tools/research/index.js +1 -0
  94. package/dist/tools/research/inspect-run.d.ts +1 -1
  95. package/dist/tools/research/literature-reviewer.d.ts +3 -0
  96. package/dist/tools/research/literature-reviewer.js +2 -0
  97. package/dist/tools/research/resolve-citation.d.ts +3 -0
  98. package/dist/tools/research/resolve-citation.js +15 -0
  99. package/dist/tools/research/search-arxiv.d.ts +3 -19
  100. package/dist/tools/research/search-arxiv.js +9 -105
  101. package/dist/tools/research/search-semantic-scholar.d.ts +8 -33
  102. package/dist/tools/research/search-semantic-scholar.js +29 -88
  103. package/dist/tools/sandbox/list-available-refs.js +1 -2
  104. package/dist/workflows/execute-analysis.d.ts +3 -0
  105. package/dist/workflows/execute-analysis.js +1 -0
  106. package/dist/workflows/sandbox-step.d.ts +5 -0
  107. package/dist/workflows/sandbox-step.js +1 -0
  108. package/dist/workflows/target-assessment/phase4-assemble.d.ts +2 -2
  109. package/dist/workflows/target-assessment/schemas.d.ts +2 -2
  110. package/dist/workflows/target-assessment/steps/phase2-aggregate.d.ts +1 -1
  111. package/dist/workflows/target-assessment/steps/phase3-aggregate.d.ts +1 -1
  112. package/package.json +1 -1
@@ -0,0 +1,156 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { PART_REGISTRY } from "../contracts/part-registry.js";
3
+ import { CortexChatPartSchema } from "../contracts/schemas/chat-parts.js";
4
+ import { conversationDisplayPart } from "./conversation-display-storage.js";
5
+ function jsonCopy(value) {
6
+ return JSON.parse(JSON.stringify(value));
7
+ }
8
+ function isSubAgent(event, topLevelCallPath) {
9
+ if (!("source" in event) || event.source === undefined)
10
+ return false;
11
+ const path = event.source.callPath;
12
+ return path.length > topLevelCallPath.length && topLevelCallPath.every((entry, index) => path[index] === entry);
13
+ }
14
+ function durableConversationType(type) {
15
+ if (!Object.hasOwn(PART_REGISTRY, type))
16
+ return false;
17
+ const descriptor = PART_REGISTRY[type];
18
+ return descriptor.emitter === "conversation" && descriptor.consumer === "conversation" && !descriptor.transient;
19
+ }
20
+ function dataPart(event) {
21
+ const parsed = CortexChatPartSchema.safeParse({ type: event.type, ...event.data });
22
+ if (!parsed.success)
23
+ throw new Error(`Invalid emitted conversation part ${event.type}: ${parsed.error.message}`);
24
+ const { type, ...data } = parsed.data;
25
+ return {
26
+ type,
27
+ id: "id" in data && typeof data.id === "string" ? data.id : undefined,
28
+ data: jsonCopy(data),
29
+ };
30
+ }
31
+ export function createConversationDisplayRecorder(options) {
32
+ const assistantParts = [];
33
+ // Where a reconciling part lives, keyed by `type:id`. Type-qualified because a
34
+ // stable id is only unique within its own family — a plan and a run card may
35
+ // legitimately share one.
36
+ const reconcileIndexes = new Map();
37
+ let finished = false;
38
+ /**
39
+ * Place a part, replacing the one it reconciles with when it has a stable id.
40
+ * A tool call reaching its `finished` state and an `ask` reaching its resolved
41
+ * status are the same operation: latest-wins, in the position the part first
42
+ * took, so a later part never jumps the ordering the user saw.
43
+ */
44
+ function upsert(part, reconciling) {
45
+ if (!reconciling || part.id === undefined) {
46
+ assistantParts.push(part);
47
+ return;
48
+ }
49
+ const key = `${part.type}:${part.id}`;
50
+ const index = reconcileIndexes.get(key);
51
+ if (index !== undefined) {
52
+ assistantParts[index] = part;
53
+ return;
54
+ }
55
+ reconcileIndexes.set(key, assistantParts.length);
56
+ assistantParts.push(part);
57
+ }
58
+ /**
59
+ * Place or update a call's recorded state.
60
+ *
61
+ * A call is recorded `incomplete` the moment it is dispatched and overwritten with
62
+ * its real outcome when it finishes. The record is therefore honest at every
63
+ * instant, and a turn that ends mid-call needs no closing pass — the calls that
64
+ * never finished are already saying so.
65
+ */
66
+ function recordToolCall(toolCallId, toolName, outcome, detail) {
67
+ upsert(conversationDisplayPart({
68
+ type: "tool-call",
69
+ toolCallId,
70
+ toolName,
71
+ outcome,
72
+ ...(detail === undefined ? {} : { detail }),
73
+ }), true);
74
+ }
75
+ function appendText(text) {
76
+ if (text.length === 0)
77
+ return;
78
+ const previous = assistantParts.at(-1);
79
+ if (previous?.type === "text") {
80
+ previous.text += text;
81
+ return;
82
+ }
83
+ assistantParts.push({ type: "text", text, state: "streaming" });
84
+ }
85
+ function recordData(event) {
86
+ if (!durableConversationType(event.type))
87
+ return;
88
+ upsert(dataPart(event), PART_REGISTRY[event.type].reconciling);
89
+ }
90
+ const emit = (event) => {
91
+ if (finished)
92
+ return options.sink(event);
93
+ if (!isSubAgent(event, options.topLevelCallPath)) {
94
+ switch (event.type) {
95
+ case "text-delta":
96
+ appendText(event.text);
97
+ break;
98
+ // The detail the loop computed at dispatch is recorded as shown. It is
99
+ // NOT re-derived on reload: doing that would need the tool's schema and
100
+ // the raw input, which is the tool-name coupling the display projection
101
+ // exists to remove.
102
+ case "tool-started":
103
+ recordToolCall(event.toolUseId, event.name, "incomplete", event.detail);
104
+ break;
105
+ case "tool-finished":
106
+ recordToolCall(event.toolUseId, event.name, event.outcome, event.detail);
107
+ break;
108
+ case "done":
109
+ case "iteration":
110
+ break;
111
+ default:
112
+ recordData(event);
113
+ break;
114
+ }
115
+ }
116
+ return options.sink(event);
117
+ };
118
+ function finish(finishOptions) {
119
+ if (!finished) {
120
+ finished = true;
121
+ if (!assistantParts.some((part) => part.type === "text") && finishOptions?.fallbackText) {
122
+ appendText(finishOptions.fallbackText);
123
+ }
124
+ // Calls need no closing pass: one that never reached `tool-finished` still holds
125
+ // the `incomplete` it was recorded with at dispatch, which is the whole of what
126
+ // the harness observed. An approval does need one — `pending` is a question, and
127
+ // a turn that ends without answering it aborted it.
128
+ for (const part of assistantParts) {
129
+ if (part.type === "data-ask" && part.data.status === "pending") {
130
+ part.data.status = "aborted";
131
+ }
132
+ }
133
+ for (const part of assistantParts) {
134
+ if (part.type === "text")
135
+ part.state = "done";
136
+ }
137
+ }
138
+ const messages = [
139
+ {
140
+ id: options.userMessageId ?? randomUUID(),
141
+ role: "user",
142
+ parts: [{ type: "text", text: options.userText, state: "done" }],
143
+ },
144
+ ];
145
+ if (assistantParts.length > 0) {
146
+ messages.push({
147
+ id: options.assistantMessageId ?? randomUUID(),
148
+ role: "assistant",
149
+ ...(finishOptions?.interrupted ? { metadata: { interrupted: true } } : {}),
150
+ parts: jsonCopy(assistantParts),
151
+ });
152
+ }
153
+ return messages;
154
+ }
155
+ return { emit, finish };
156
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The runtime transcript read: stored display projections → `CortexMessage[]`.
3
+ *
4
+ * `ThreadHistory.loadPage` returns a thread's rows in `seq` order, and every
5
+ * append's first row carries the display projection for that append. Replay is
6
+ * therefore a concatenation: walk the rows, emit each envelope's messages in
7
+ * order, done. No turn grouping, no tool-name recognition, no card rebuilding,
8
+ * no filesystem or database lookup — nothing that could render a reloaded
9
+ * conversation differently from the one the user was shown.
10
+ *
11
+ * A row with no envelope predates the display projection and is skipped. The
12
+ * startup backfill (`conversation-display-backfill.ts`) writes one for every such
13
+ * row before the runtime serves traffic, so in a booted harness there are none;
14
+ * skipping rather than falling back to the migration renderer is what keeps the
15
+ * reconstruction path out of the runtime entirely.
16
+ */
17
+ import type { CortexMessage } from "../contracts/message.js";
18
+ import type { StoredMessage } from "./thread-history.js";
19
+ export declare function storedMessagesToCortex(messages: readonly StoredMessage[]): CortexMessage[];
@@ -0,0 +1,39 @@
1
+ /**
2
+ * The runtime transcript read: stored display projections → `CortexMessage[]`.
3
+ *
4
+ * `ThreadHistory.loadPage` returns a thread's rows in `seq` order, and every
5
+ * append's first row carries the display projection for that append. Replay is
6
+ * therefore a concatenation: walk the rows, emit each envelope's messages in
7
+ * order, done. No turn grouping, no tool-name recognition, no card rebuilding,
8
+ * no filesystem or database lookup — nothing that could render a reloaded
9
+ * conversation differently from the one the user was shown.
10
+ *
11
+ * A row with no envelope predates the display projection and is skipped. The
12
+ * startup backfill (`conversation-display-backfill.ts`) writes one for every such
13
+ * row before the runtime serves traffic, so in a booted harness there are none;
14
+ * skipping rather than falling back to the migration renderer is what keeps the
15
+ * reconstruction path out of the runtime entirely.
16
+ */
17
+ import { conversationUIToCortexMessages } from "./conversation-display-storage.js";
18
+ export function storedMessagesToCortex(messages) {
19
+ const out = [];
20
+ // The messages the append currently being walked produced. A row bearing an
21
+ // envelope opens one; every row after it belongs to it until the next does.
22
+ let append = null;
23
+ for (const row of messages) {
24
+ if (row.displayEnvelope) {
25
+ append = conversationUIToCortexMessages(row.displayEnvelope.messages);
26
+ out.push(...append);
27
+ }
28
+ // The turn's reported rollup rides the model row that ENDED the turn, not the
29
+ // display projection — it is a fact about what the turn cost, not about what it
30
+ // showed, and persisting it in both places would let the two disagree. Fold it
31
+ // onto the assistant reply a reader associates with the figure.
32
+ if (row.usage && append) {
33
+ const last = append.at(-1);
34
+ if (last?.role === "assistant")
35
+ last.usage = row.usage;
36
+ }
37
+ }
38
+ return out;
39
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * The durable conversation display projection — what the user was shown,
3
+ * persisted verbatim.
4
+ *
5
+ * AI SDK `UIMessage` is the CONTAINER: it supplies message identity, role,
6
+ * ordering, metadata, and `validateUIMessages` for runtime validation. The PARTS
7
+ * vocabulary is Cortex's own, because ours is the richer one — a `CortexPart`
8
+ * carries a tool call's four-way outcome and its one-line detail, and AI SDK's
9
+ * `dynamic-tool` part has nowhere to put either. Mapping through `dynamic-tool`
10
+ * would silently drop both on every reload, so every non-text part rides as a
11
+ * typed `DataUIPart` whose payload IS the Cortex part minus its discriminant.
12
+ *
13
+ * The projection is therefore lossless in both directions, and the read path
14
+ * ({@link conversationUIToCortexMessages}) is a near-identity rather than a
15
+ * reconstruction.
16
+ */
17
+ import { type DataUIPart, type UIMessage } from "ai";
18
+ import type { AskPart, FileReferencePart, PlanPart, PresentationPart, PreviewPart, DataPreviewFailedPart, RunCardPart } from "../contracts/chat-parts.js";
19
+ import type { ToolCallOutcome } from "../contracts/chat-events.js";
20
+ import type { CortexMessage, CortexPart, ToolCallPart } from "../contracts/message.js";
21
+ export declare const SUPPORTED_DISPLAY_SCHEMA_VERSION = 1;
22
+ type Payload<T extends {
23
+ type: string;
24
+ }> = Omit<T, "type">;
25
+ /**
26
+ * The AI SDK custom-data vocabulary persisted for one conversation display.
27
+ *
28
+ * Each key `k` becomes a `data-${k}` part whose payload is the matching Cortex
29
+ * part without its `type` field, so a stored part and a wire part differ only in
30
+ * where the discriminant lives.
31
+ */
32
+ export type ConversationUIData = {
33
+ "tool-call": Omit<Payload<ToolCallPart>, "outcome"> & {
34
+ outcome: ToolCallOutcome;
35
+ };
36
+ presentation: Payload<PresentationPart>;
37
+ plan: Payload<PlanPart>;
38
+ "run-card": Payload<RunCardPart>;
39
+ "file-reference": Payload<FileReferencePart>;
40
+ ask: Payload<AskPart>;
41
+ "report-preview": Payload<PreviewPart>;
42
+ "report-preview-failed": Payload<DataPreviewFailedPart>;
43
+ };
44
+ export interface ConversationDisplayMetadata {
45
+ interrupted?: boolean;
46
+ }
47
+ export type ConversationUIMessage = UIMessage<ConversationDisplayMetadata, ConversationUIData>;
48
+ export interface StoredDisplayEnvelope {
49
+ readonly kind: "ai-sdk-ui-messages";
50
+ readonly aiSdkMajor: 7;
51
+ readonly schemaVersion: typeof SUPPORTED_DISPLAY_SCHEMA_VERSION;
52
+ readonly messages: ConversationUIMessage[];
53
+ }
54
+ export declare function envelopeDisplayMessages(messages: readonly ConversationUIMessage[]): StoredDisplayEnvelope;
55
+ export declare function parseStoredDisplayEnvelope(value: unknown, identity: string): Promise<StoredDisplayEnvelope>;
56
+ /**
57
+ * Build the stored form of one non-text Cortex part.
58
+ *
59
+ * A tool call is the one part whose stored payload is not simply the Cortex part
60
+ * minus its discriminant: `outcome` is optional on the live part (absent means in
61
+ * flight) and required here, because a record is of a call that is no longer
62
+ * running. An in-flight call being recorded is one the turn never saw finish, so
63
+ * absence resolves to `incomplete` at exactly this boundary — once, where the
64
+ * lifetime changes — rather than at each reader.
65
+ */
66
+ export declare function conversationDisplayPart(part: Exclude<CortexPart, {
67
+ type: "text";
68
+ }>): DataUIPart<ConversationUIData>;
69
+ /**
70
+ * Project Cortex display messages into the durable representation.
71
+ *
72
+ * The startup backfill's only path into storage: it renders a legacy turn from
73
+ * the model transcript and freezes the result. The live recorder builds the same
74
+ * shape directly from the event stream instead of round-tripping through
75
+ * `CortexMessage`.
76
+ */
77
+ export declare function cortexMessagesToConversationUI(messages: readonly CortexMessage[]): ConversationUIMessage[];
78
+ /**
79
+ * Read the durable representation back as Cortex display messages.
80
+ *
81
+ * Every part round-trips by moving its discriminant back out of the part type,
82
+ * so nothing here consults a tool name, a registry, or the filesystem. A message
83
+ * whose parts all render empty is dropped so the transcript has no empty bubbles.
84
+ */
85
+ export declare function conversationUIToCortexMessages(messages: readonly ConversationUIMessage[]): CortexMessage[];
86
+ export {};
@@ -0,0 +1,168 @@
1
+ /**
2
+ * The durable conversation display projection — what the user was shown,
3
+ * persisted verbatim.
4
+ *
5
+ * AI SDK `UIMessage` is the CONTAINER: it supplies message identity, role,
6
+ * ordering, metadata, and `validateUIMessages` for runtime validation. The PARTS
7
+ * vocabulary is Cortex's own, because ours is the richer one — a `CortexPart`
8
+ * carries a tool call's four-way outcome and its one-line detail, and AI SDK's
9
+ * `dynamic-tool` part has nowhere to put either. Mapping through `dynamic-tool`
10
+ * would silently drop both on every reload, so every non-text part rides as a
11
+ * typed `DataUIPart` whose payload IS the Cortex part minus its discriminant.
12
+ *
13
+ * The projection is therefore lossless in both directions, and the read path
14
+ * ({@link conversationUIToCortexMessages}) is a near-identity rather than a
15
+ * reconstruction.
16
+ */
17
+ import { validateUIMessages } from "ai";
18
+ import { z } from "zod";
19
+ import { AskPartSchema, DataPreviewFailedPartSchema, FileReferencePartSchema, PlanPartSchema, PresentationPartSchema, PreviewPartSchema, RunCardPartSchema, } from "../contracts/schemas/chat-parts.js";
20
+ import { ToolCallOutcomeSchema } from "../contracts/schemas/chat-events.js";
21
+ export const SUPPORTED_DISPLAY_SCHEMA_VERSION = 1;
22
+ const DisplayEnvelopeHeaderSchema = z.object({
23
+ kind: z.literal("ai-sdk-ui-messages"),
24
+ aiSdkMajor: z.literal(7),
25
+ schemaVersion: z.literal(SUPPORTED_DISPLAY_SCHEMA_VERSION),
26
+ messages: z.array(z.unknown()).min(1),
27
+ });
28
+ /**
29
+ * Declared here rather than derived from `contracts/schemas/chat-parts.ts`: a
30
+ * tool call is not a chat DATA part on the wire — it reaches a host as the
31
+ * `tool-started`/`tool-finished` event pair — so `chat-parts.ts` has no member to
32
+ * omit a discriminant from. Storage is the only place the pair is flattened into
33
+ * one part, and this is that shape.
34
+ *
35
+ * `outcome` is REQUIRED, and carries the whole terminal state including
36
+ * `incomplete`. A record describes a call that is no longer running, so "still in
37
+ * flight" is not a state it can be in — a turn cut off mid-call recorded a
38
+ * dispatch with no completion, which is what `incomplete` says. Storing a
39
+ * lifecycle field beside an optional outcome instead would put the meaning of the
40
+ * combination in each reader's head, and readers would disagree.
41
+ */
42
+ const ToolCallDisplaySchema = z
43
+ .object({
44
+ toolCallId: z.string(),
45
+ toolName: z.string(),
46
+ outcome: ToolCallOutcomeSchema,
47
+ detail: z.string().optional(),
48
+ })
49
+ .strict();
50
+ const dataSchemas = {
51
+ "tool-call": ToolCallDisplaySchema,
52
+ presentation: PresentationPartSchema.omit({ type: true }).strict(),
53
+ plan: PlanPartSchema.omit({ type: true }).strict(),
54
+ "run-card": RunCardPartSchema.omit({ type: true }).strict(),
55
+ "file-reference": FileReferencePartSchema.omit({ type: true }).strict(),
56
+ ask: AskPartSchema.omit({ type: true }).strict(),
57
+ "report-preview": PreviewPartSchema.omit({ type: true }).strict(),
58
+ "report-preview-failed": DataPreviewFailedPartSchema.omit({ type: true }).strict(),
59
+ };
60
+ export function envelopeDisplayMessages(messages) {
61
+ if (messages.length === 0)
62
+ throw new Error("A conversation display envelope must contain at least one UI message");
63
+ return {
64
+ kind: "ai-sdk-ui-messages",
65
+ aiSdkMajor: 7,
66
+ schemaVersion: SUPPORTED_DISPLAY_SCHEMA_VERSION,
67
+ messages: structuredClone(messages),
68
+ };
69
+ }
70
+ export async function parseStoredDisplayEnvelope(value, identity) {
71
+ const header = DisplayEnvelopeHeaderSchema.safeParse(value);
72
+ if (!header.success) {
73
+ throw new Error(`Invalid stored conversation display envelope at ${identity}: ${header.error.message}`);
74
+ }
75
+ try {
76
+ const messages = await validateUIMessages({
77
+ messages: header.data.messages,
78
+ dataSchemas,
79
+ metadataSchema: z.object({ interrupted: z.boolean().optional() }).optional(),
80
+ });
81
+ return { ...header.data, messages };
82
+ }
83
+ catch (error) {
84
+ const message = error instanceof Error ? error.message : String(error);
85
+ throw new Error(`Invalid stored conversation display envelope at ${identity}: ${message}`, { cause: error });
86
+ }
87
+ }
88
+ /** The data-part key a Cortex part is stored under — its `type` minus the `data-` prefix. */
89
+ function displayPartType(part) {
90
+ return (part.type === "tool-call" ? "data-tool-call" : part.type);
91
+ }
92
+ /** The id a part reconciles on: a tool call by its call id, a card by its own stable id. */
93
+ function displayPartId(part) {
94
+ if (part.type === "tool-call")
95
+ return part.toolCallId;
96
+ return "id" in part && typeof part.id === "string" ? part.id : undefined;
97
+ }
98
+ /**
99
+ * Build the stored form of one non-text Cortex part.
100
+ *
101
+ * A tool call is the one part whose stored payload is not simply the Cortex part
102
+ * minus its discriminant: `outcome` is optional on the live part (absent means in
103
+ * flight) and required here, because a record is of a call that is no longer
104
+ * running. An in-flight call being recorded is one the turn never saw finish, so
105
+ * absence resolves to `incomplete` at exactly this boundary — once, where the
106
+ * lifetime changes — rather than at each reader.
107
+ */
108
+ export function conversationDisplayPart(part) {
109
+ const { type: _type, ...rest } = part;
110
+ const data = part.type === "tool-call" ? { ...rest, outcome: part.outcome ?? "incomplete" } : rest;
111
+ return { type: displayPartType(part), id: displayPartId(part), data };
112
+ }
113
+ /**
114
+ * Project Cortex display messages into the durable representation.
115
+ *
116
+ * The startup backfill's only path into storage: it renders a legacy turn from
117
+ * the model transcript and freezes the result. The live recorder builds the same
118
+ * shape directly from the event stream instead of round-tripping through
119
+ * `CortexMessage`.
120
+ */
121
+ export function cortexMessagesToConversationUI(messages) {
122
+ return messages.map((message) => {
123
+ const parts = message.parts.map((part) => part.type === "text" ? { type: "text", text: part.text, state: "done" } : conversationDisplayPart(part));
124
+ return {
125
+ id: message.id,
126
+ role: message.role,
127
+ ...(message.interrupted ? { metadata: { interrupted: true } } : {}),
128
+ parts,
129
+ };
130
+ });
131
+ }
132
+ /**
133
+ * Read the durable representation back as Cortex display messages.
134
+ *
135
+ * Every part round-trips by moving its discriminant back out of the part type,
136
+ * so nothing here consults a tool name, a registry, or the filesystem. A message
137
+ * whose parts all render empty is dropped so the transcript has no empty bubbles.
138
+ */
139
+ export function conversationUIToCortexMessages(messages) {
140
+ const out = [];
141
+ for (const message of messages) {
142
+ const parts = [];
143
+ for (const part of message.parts) {
144
+ if (part.type === "text") {
145
+ if (part.text.length > 0)
146
+ parts.push({ type: "text", text: part.text });
147
+ continue;
148
+ }
149
+ if (part.type === "data-tool-call") {
150
+ parts.push({ type: "tool-call", ...part.data });
151
+ continue;
152
+ }
153
+ if (part.type.startsWith("data-")) {
154
+ const data = part;
155
+ parts.push({ type: data.type, ...data.data });
156
+ }
157
+ }
158
+ if (parts.length === 0)
159
+ continue;
160
+ out.push({
161
+ id: message.id,
162
+ role: message.role,
163
+ parts,
164
+ ...(message.metadata?.interrupted ? { interrupted: true } : {}),
165
+ });
166
+ }
167
+ return out;
168
+ }
@@ -1,5 +1,8 @@
1
1
  /**
2
- * Read-path reconstruction of display cards from a persisted turn.
2
+ * @deprecated Compatibility-only reconstruction for pre-display-envelope
3
+ * turns. New runtime reads use stored AI SDK UI messages. Retain this module
4
+ * through the pre-v1 direct-upgrade window, then remove its runtime fallback
5
+ * in a dedicated compatibility change.
3
6
  *
4
7
  * `show_plan` / `show_user` / `show_file` / `execute_analysis` / `iterate_report` emit
5
8
  * `data-plan` / `data-presentation` / `data-file-reference` / `data-run-card` /
@@ -13,11 +16,14 @@
13
16
  */
14
17
  import type { CortexPart } from "@inflexa-ai/harness/contracts/message.js";
15
18
  import type { Pool } from "pg";
19
+ /** @deprecated Used only by the legacy display backfill/fallback. */
16
20
  export interface StoredToolCallForCard {
17
21
  readonly type: "tool_use";
18
22
  readonly id: string;
19
23
  readonly name: string;
20
24
  readonly input: unknown;
21
25
  }
26
+ /** @deprecated Used only by the legacy display backfill/fallback. */
22
27
  export type ToolCardResolver = (block: StoredToolCallForCard) => Promise<CortexPart | null>;
28
+ /** @deprecated Use persisted conversation display envelopes for runtime reads. */
23
29
  export declare function createCardResolver(pool: Pool, analysisId: string, workspaceRoot: string): ToolCardResolver;
@@ -1,5 +1,8 @@
1
1
  /**
2
- * Read-path reconstruction of display cards from a persisted turn.
2
+ * @deprecated Compatibility-only reconstruction for pre-display-envelope
3
+ * turns. New runtime reads use stored AI SDK UI messages. Retain this module
4
+ * through the pre-v1 direct-upgrade window, then remove its runtime fallback
5
+ * in a dedicated compatibility change.
3
6
  *
4
7
  * `show_plan` / `show_user` / `show_file` / `execute_analysis` / `iterate_report` emit
5
8
  * `data-plan` / `data-presentation` / `data-file-reference` / `data-run-card` /
@@ -15,6 +18,7 @@ import { unwrapOrThrow } from "../lib/result.js";
15
18
  import { adHocPlanId, adHocRunId } from "../tools/analysis-invocation.js";
16
19
  import { validatePath } from "../tools/lib/path-validation.js";
17
20
  import { buildFileReferenceCardData, buildPlanCardData, buildPresentationCardData, buildPreviewCardData, buildRunCardData } from "./card-builders.js";
21
+ /** @deprecated Use persisted conversation display envelopes for runtime reads. */
18
22
  export function createCardResolver(pool, analysisId, workspaceRoot) {
19
23
  return async (block) => {
20
24
  if (block.type !== "tool_use")
@@ -26,11 +26,18 @@ import type { Pool } from "pg";
26
26
  import type { TokenUsageRollup } from "../contracts/usage.js";
27
27
  import { type DbError } from "../lib/db-result.js";
28
28
  import { type StoredMessageEnvelope } from "./ai-sdk-message-storage.js";
29
+ import { type ConversationUIMessage, type StoredDisplayEnvelope } from "./conversation-display-storage.js";
29
30
  /** One stored message, as returned by the display read (`loadPage`). */
30
31
  export interface StoredMessage {
31
32
  readonly seq: number;
32
33
  readonly envelope: StoredMessageEnvelope;
33
34
  readonly message: ModelMessage;
35
+ /**
36
+ * The display projection of the append this row OPENED — present on the first
37
+ * row of each `appendTurn` and absent on every other row. A conversation turn
38
+ * and a host-appended record are both appends, so both carry one.
39
+ */
40
+ readonly displayEnvelope?: StoredDisplayEnvelope;
34
41
  /**
35
42
  * What providers reported for the whole TURN this row completed — present
36
43
  * only on the last assistant row of a turn appended with a rollup, absent
@@ -41,6 +48,41 @@ export interface StoredMessage {
41
48
  */
42
49
  readonly usage?: TokenUsageRollup;
43
50
  }
51
+ /**
52
+ * One atomic append: the exact provider-facing history, the display projection of
53
+ * what the user was shown, and what the turn reported spending.
54
+ *
55
+ * The two message projections are independent by design. `modelMessages` is what
56
+ * the provider sees and the only input to token accounting; `displayMessages` is
57
+ * what the transcript replays. Neither is derived from the other, and a failed
58
+ * write commits neither.
59
+ */
60
+ export interface ConversationTurn {
61
+ /** Exact provider-facing history; token accounting and model reads use only this projection. */
62
+ readonly modelMessages: readonly ModelMessage[];
63
+ /** Complete ordered AI SDK UI messages recorded from the live display event stream. */
64
+ readonly displayMessages: readonly ConversationUIMessage[];
65
+ /**
66
+ * The turn's reported rollup, as `AgentFinish.turnUsage` carries it out of the
67
+ * loop. Stored on the LAST assistant message of the turn — the row a reader
68
+ * associates with the reply. Omitting it, and supplying one that reports no
69
+ * quantity at all, both leave the row without one: a turn that reported nothing
70
+ * must not read back as a turn that cost zero.
71
+ */
72
+ readonly turnUsage?: TokenUsageRollup;
73
+ }
74
+ /**
75
+ * The append a host uses to record out-of-band work in a thread — an analysis
76
+ * run's outcome, typically.
77
+ *
78
+ * Both projections are built here because a record has no live turn behind it:
79
+ * no recorder observed it, so nothing else would produce its display projection,
80
+ * and a record appended without one would be stored, read by the model, and
81
+ * never shown. The harness owns the record-to-`system` mapping for the same
82
+ * reason it owns the marker — a host that hand-assembled either could produce a
83
+ * message the turn-boundary predicates fail to recognise.
84
+ */
85
+ export declare function conversationRecordTurn(text: string): ConversationTurn;
44
86
  /**
45
87
  * A page of stored messages plus pagination metadata. Pagination is
46
88
  * turn-based: `total` is the thread's turn count and `page`/`perPage` index
@@ -74,20 +116,13 @@ export type RetractOutcome = {
74
116
  */
75
117
  export interface ThreadHistory {
76
118
  /**
77
- * Append one conversation turn — every message written in a single
119
+ * Append one {@link ConversationTurn} — every message written in a single
78
120
  * transaction with a `seq` monotonically increasing per thread.
79
121
  *
80
- * `turnUsage` is the turn's reported rollup, as `AgentFinish.turnUsage`
81
- * carries it out of the loop and the `finish` event carries it to a live
82
- * surface. It is stored on the LAST assistant message of the turn — the row a
83
- * reader associates with the reply — so a reloaded transcript renders the
84
- * figure the live turn showed. Optional at every layer: omitting it, and
85
- * supplying one that reports no quantity at all, both leave the row without
86
- * one, because a turn that reported nothing must not read back as a turn that
87
- * cost zero. A turn writing no assistant message stores none and still
88
- * succeeds — there is no row on which the figure would mean anything.
122
+ * A turn writing no assistant message stores no rollup and still succeeds:
123
+ * there is no row on which the figure would mean anything.
89
124
  */
90
- appendTurn(threadId: string, messages: readonly ModelMessage[], turnUsage?: TokenUsageRollup): ResultAsync<void, DbError>;
125
+ appendTurn(threadId: string, turn: ConversationTurn): ResultAsync<void, DbError>;
91
126
  /**
92
127
  * Return a recent-turns window that fits `tokenBudget`, oldest-first,
93
128
  * snapped to a valid AI SDK model-message sequence. The window START advances