agent-trajectory 0.3.0__tar.gz → 0.4.4__tar.gz

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 (18) hide show
  1. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/PKG-INFO +37 -1
  2. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/README.md +36 -0
  3. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/pyproject.toml +1 -1
  4. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/python/src/agent_trajectory.egg-info/PKG-INFO +37 -1
  5. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/python/src/agent_trajectory.egg-info/SOURCES.txt +1 -0
  6. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/python/src/trajectory/__init__.py +1 -1
  7. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/python/src/trajectory/_vendor/trajectory-cli.mjs +694 -5
  8. agent_trajectory-0.4.4/python/src/trajectory/conversations.py +117 -0
  9. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/LICENSE +0 -0
  10. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/python/src/agent_trajectory.egg-info/dependency_links.txt +0 -0
  11. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/python/src/agent_trajectory.egg-info/requires.txt +0 -0
  12. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/python/src/agent_trajectory.egg-info/top_level.txt +0 -0
  13. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/python/src/trajectory/_client.py +0 -0
  14. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/python/src/trajectory/_errors.py +0 -0
  15. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/python/src/trajectory/_types.py +0 -0
  16. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/python/src/trajectory/_vendor/deepagents_checkpoint.py +0 -0
  17. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/python/src/trajectory/py.typed +0 -0
  18. {agent_trajectory-0.3.0 → agent_trajectory-0.4.4}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: agent-trajectory
3
- Version: 0.3.0
3
+ Version: 0.4.4
4
4
  Summary: Normalize native agent transcripts into a shared trajectory format.
5
5
  Author: Letta
6
6
  License-Expression: Apache-2.0
@@ -52,6 +52,12 @@ The Python wrapper is published as
52
52
  pip install agent-trajectory
53
53
  ```
54
54
 
55
+ If publishing fails after the version tag was pushed, run **Publish existing
56
+ release tag** on that tag (`gh workflow run publish-existing-tag.yml --ref vX.Y.Z`).
57
+ It rebuilds and publishes missing npm/PyPI distributions without another bump
58
+ or tag. The retry requires a tag containing this workflow, so it cannot recover
59
+ older tags. Do not rerun the bump workflow after a tag was pushed.
60
+
55
61
  ## Quick start
56
62
 
57
63
  ```ts
@@ -177,6 +183,36 @@ The public function is:
177
183
  normalizeTranscript(input: NormalizeInput): NormalizeResult
178
184
  ```
179
185
 
186
+ ## Imported conversations (separate API)
187
+
188
+ Slack channels are multi-party conversations, not agent execution traces. They use
189
+ an independent [conversation schema](schema/conversation-v1.schema.json) and API:
190
+
191
+ ```ts
192
+ import { normalizeConversation } from "@letta-ai/trajectory/conversations";
193
+
194
+ const { records, diagnostics } = normalizeConversation({
195
+ source: "slack",
196
+ transcript: rawJsonl, // one channel's messages, as fetched
197
+ channel: "C0AB…",
198
+ channelName: "eng-deploys", // optional
199
+ users, // optional users.list rows, for display names and bot flags
200
+ });
201
+ ```
202
+
203
+ One channel is one conversation: a leading `meta` with a participants table,
204
+ then top-level posts in time order with thread replies nested once under their
205
+ root. Python exposes `normalize_conversation` from `trajectory.conversations`.
206
+ The format does not extend `NormalizedRecord`, `normalizeTranscript`, or
207
+ `normalizeToCanonical`; agent schemas and canonical schema version remain
208
+ unchanged.
209
+
210
+ See [CONVERSATIONS.md](CONVERSATIONS.md) for the contract and
211
+ [Slack](src/adapters/slack/) for thread assembly and supported content. Slack is
212
+ the only conversation adapter in V0. Speaker names, reaction counts, and file
213
+ placeholders are supported; recipients, file contents, and other message metadata
214
+ remain deferred.
215
+
180
216
  ## Adding a source
181
217
 
182
218
  Each native format is implemented as a focused adapter that decodes source
@@ -30,6 +30,12 @@ The Python wrapper is published as
30
30
  pip install agent-trajectory
31
31
  ```
32
32
 
33
+ If publishing fails after the version tag was pushed, run **Publish existing
34
+ release tag** on that tag (`gh workflow run publish-existing-tag.yml --ref vX.Y.Z`).
35
+ It rebuilds and publishes missing npm/PyPI distributions without another bump
36
+ or tag. The retry requires a tag containing this workflow, so it cannot recover
37
+ older tags. Do not rerun the bump workflow after a tag was pushed.
38
+
33
39
  ## Quick start
34
40
 
35
41
  ```ts
@@ -155,6 +161,36 @@ The public function is:
155
161
  normalizeTranscript(input: NormalizeInput): NormalizeResult
156
162
  ```
157
163
 
164
+ ## Imported conversations (separate API)
165
+
166
+ Slack channels are multi-party conversations, not agent execution traces. They use
167
+ an independent [conversation schema](schema/conversation-v1.schema.json) and API:
168
+
169
+ ```ts
170
+ import { normalizeConversation } from "@letta-ai/trajectory/conversations";
171
+
172
+ const { records, diagnostics } = normalizeConversation({
173
+ source: "slack",
174
+ transcript: rawJsonl, // one channel's messages, as fetched
175
+ channel: "C0AB…",
176
+ channelName: "eng-deploys", // optional
177
+ users, // optional users.list rows, for display names and bot flags
178
+ });
179
+ ```
180
+
181
+ One channel is one conversation: a leading `meta` with a participants table,
182
+ then top-level posts in time order with thread replies nested once under their
183
+ root. Python exposes `normalize_conversation` from `trajectory.conversations`.
184
+ The format does not extend `NormalizedRecord`, `normalizeTranscript`, or
185
+ `normalizeToCanonical`; agent schemas and canonical schema version remain
186
+ unchanged.
187
+
188
+ See [CONVERSATIONS.md](CONVERSATIONS.md) for the contract and
189
+ [Slack](src/adapters/slack/) for thread assembly and supported content. Slack is
190
+ the only conversation adapter in V0. Speaker names, reaction counts, and file
191
+ placeholders are supported; recipients, file contents, and other message metadata
192
+ remain deferred.
193
+
158
194
  ## Adding a source
159
195
 
160
196
  Each native format is implemented as a focused adapter that decodes source
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "agent-trajectory"
7
- version = "0.3.0"
7
+ version = "0.4.4"
8
8
  description = "Normalize native agent transcripts into a shared trajectory format."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: agent-trajectory
3
- Version: 0.3.0
3
+ Version: 0.4.4
4
4
  Summary: Normalize native agent transcripts into a shared trajectory format.
5
5
  Author: Letta
6
6
  License-Expression: Apache-2.0
@@ -52,6 +52,12 @@ The Python wrapper is published as
52
52
  pip install agent-trajectory
53
53
  ```
54
54
 
55
+ If publishing fails after the version tag was pushed, run **Publish existing
56
+ release tag** on that tag (`gh workflow run publish-existing-tag.yml --ref vX.Y.Z`).
57
+ It rebuilds and publishes missing npm/PyPI distributions without another bump
58
+ or tag. The retry requires a tag containing this workflow, so it cannot recover
59
+ older tags. Do not rerun the bump workflow after a tag was pushed.
60
+
55
61
  ## Quick start
56
62
 
57
63
  ```ts
@@ -177,6 +183,36 @@ The public function is:
177
183
  normalizeTranscript(input: NormalizeInput): NormalizeResult
178
184
  ```
179
185
 
186
+ ## Imported conversations (separate API)
187
+
188
+ Slack channels are multi-party conversations, not agent execution traces. They use
189
+ an independent [conversation schema](schema/conversation-v1.schema.json) and API:
190
+
191
+ ```ts
192
+ import { normalizeConversation } from "@letta-ai/trajectory/conversations";
193
+
194
+ const { records, diagnostics } = normalizeConversation({
195
+ source: "slack",
196
+ transcript: rawJsonl, // one channel's messages, as fetched
197
+ channel: "C0AB…",
198
+ channelName: "eng-deploys", // optional
199
+ users, // optional users.list rows, for display names and bot flags
200
+ });
201
+ ```
202
+
203
+ One channel is one conversation: a leading `meta` with a participants table,
204
+ then top-level posts in time order with thread replies nested once under their
205
+ root. Python exposes `normalize_conversation` from `trajectory.conversations`.
206
+ The format does not extend `NormalizedRecord`, `normalizeTranscript`, or
207
+ `normalizeToCanonical`; agent schemas and canonical schema version remain
208
+ unchanged.
209
+
210
+ See [CONVERSATIONS.md](CONVERSATIONS.md) for the contract and
211
+ [Slack](src/adapters/slack/) for thread assembly and supported content. Slack is
212
+ the only conversation adapter in V0. Speaker names, reaction counts, and file
213
+ placeholders are supported; recipients, file contents, and other message metadata
214
+ remain deferred.
215
+
180
216
  ## Adding a source
181
217
 
182
218
  Each native format is implemented as a focused adapter that decodes source
@@ -10,6 +10,7 @@ python/src/trajectory/__init__.py
10
10
  python/src/trajectory/_client.py
11
11
  python/src/trajectory/_errors.py
12
12
  python/src/trajectory/_types.py
13
+ python/src/trajectory/conversations.py
13
14
  python/src/trajectory/py.typed
14
15
  python/src/trajectory/_vendor/deepagents_checkpoint.py
15
16
  python/src/trajectory/_vendor/trajectory-cli.mjs
@@ -82,4 +82,4 @@ __all__ = [
82
82
  "normalize_transcript",
83
83
  ]
84
84
 
85
- __version__ = "0.3.0"
85
+ __version__ = "0.4.4"
@@ -71,6 +71,17 @@ function parseTimestamp(value) {
71
71
  const date = new Date(withZone);
72
72
  return Number.isNaN(date.getTime()) ? undefined : date;
73
73
  }
74
+ var TEXT_BLOCK_TYPES = new Set(["text", "input_text", "output_text"]);
75
+ var IMAGE_BLOCK_TYPES = new Set(["image", "input_image", "image_url"]);
76
+ function contentBlockKind(value) {
77
+ if (!isObject(value) || typeof value.type !== "string")
78
+ return;
79
+ if (TEXT_BLOCK_TYPES.has(value.type))
80
+ return "text";
81
+ if (IMAGE_BLOCK_TYPES.has(value.type))
82
+ return "image";
83
+ return;
84
+ }
74
85
  function blocksText(content) {
75
86
  if (typeof content === "string")
76
87
  return content;
@@ -80,13 +91,15 @@ function blocksText(content) {
80
91
  for (const item of content) {
81
92
  if (!isObject(item))
82
93
  continue;
83
- const type = item.type;
84
- if (type === "text" || type === "input_text" || type === "output_text" || type === undefined && "text" in item) {
94
+ const kind = contentBlockKind(item);
95
+ if (kind === "text" || item.type === undefined && "text" in item) {
85
96
  if (typeof item.text === "string" && item.text.length > 0) {
86
97
  parts.push(item.text);
87
98
  }
88
- } else if (type === "image") {
99
+ } else if (kind === "image") {
89
100
  parts.push("[image]");
101
+ } else if (item.type === "tool_reference" && typeof item.tool_name === "string" && item.tool_name) {
102
+ parts.push(`tool_reference: ${item.tool_name}`);
90
103
  }
91
104
  }
92
105
  return parts.join(`
@@ -97,6 +110,164 @@ function jsonString(value) {
97
110
  return serialized === undefined ? "{}" : serialized;
98
111
  }
99
112
 
113
+ // src/adapters/serialized-blocks.ts
114
+ var IMAGE_BLOCK_MARKER = /(["'])type\1\s*:\s*(["'])(?:image|input_image|image_url)\2/;
115
+ var DATA_IMAGE_URL = /data:image\/[\w.+-]+;base64,[A-Za-z0-9+/]+=*/g;
116
+ function serializedBlocksText(text) {
117
+ const head = text.trimStart()[0];
118
+ if (head !== "[" && head !== "{" || !IMAGE_BLOCK_MARKER.test(text)) {
119
+ return text;
120
+ }
121
+ const parsed = parseJson(text) ?? parsePythonLiteral(text);
122
+ const blocks = Array.isArray(parsed) ? parsed : [parsed];
123
+ if (parsed != null && blocks.length > 0 && blocks.every((block) => contentBlockKind(block) !== undefined) && blocks.some((block) => contentBlockKind(block) === "image")) {
124
+ return blocksText(blocks);
125
+ }
126
+ return text.replace(DATA_IMAGE_URL, "[image]");
127
+ }
128
+ function parseJson(text) {
129
+ try {
130
+ return JSON.parse(text);
131
+ } catch {
132
+ return;
133
+ }
134
+ }
135
+ var PYTHON_ESCAPES = new Map([
136
+ ["\\", "\\"],
137
+ ["'", "'"],
138
+ ['"', '"'],
139
+ ["n", `
140
+ `],
141
+ ["r", "\r"],
142
+ ["t", "\t"]
143
+ ]);
144
+ var HEX_ESCAPE_LENGTHS = new Map([
145
+ ["x", 2],
146
+ ["u", 4],
147
+ ["U", 8]
148
+ ]);
149
+ var PYTHON_SCALAR = /True|False|None|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/y;
150
+ var HEX = /^[0-9a-fA-F]+$/;
151
+ function parsePythonLiteral(text) {
152
+ let index = 0;
153
+ const fail = () => {
154
+ throw new SyntaxError("not a Python literal");
155
+ };
156
+ const skipSpace = () => {
157
+ while (index < text.length && /\s/.test(text[index] ?? ""))
158
+ index += 1;
159
+ };
160
+ const parseString = () => {
161
+ const quote = text[index];
162
+ index += 1;
163
+ let out = "";
164
+ let start = index;
165
+ for (;; ) {
166
+ const char = text[index];
167
+ if (char === undefined || char === `
168
+ `)
169
+ fail();
170
+ if (char === quote) {
171
+ out += text.slice(start, index);
172
+ index += 1;
173
+ return out;
174
+ }
175
+ if (char !== "\\") {
176
+ index += 1;
177
+ continue;
178
+ }
179
+ out += text.slice(start, index);
180
+ const escape = text[index + 1] ?? fail();
181
+ index += 2;
182
+ const simple = PYTHON_ESCAPES.get(escape);
183
+ const hexLength = HEX_ESCAPE_LENGTHS.get(escape);
184
+ if (simple !== undefined) {
185
+ out += simple;
186
+ } else if (hexLength !== undefined) {
187
+ const hex = text.slice(index, index + hexLength);
188
+ if (hex.length !== hexLength || !HEX.test(hex))
189
+ fail();
190
+ out += String.fromCodePoint(Number.parseInt(hex, 16));
191
+ index += hexLength;
192
+ } else {
193
+ out += `\\${escape}`;
194
+ }
195
+ start = index;
196
+ }
197
+ };
198
+ const parseItems = (close, parseItem) => {
199
+ index += 1;
200
+ skipSpace();
201
+ if (text[index] === close) {
202
+ index += 1;
203
+ return;
204
+ }
205
+ for (;; ) {
206
+ parseItem();
207
+ skipSpace();
208
+ const char = text[index];
209
+ index += 1;
210
+ if (char === close)
211
+ return;
212
+ if (char !== ",")
213
+ fail();
214
+ skipSpace();
215
+ if (text[index] === close) {
216
+ index += 1;
217
+ return;
218
+ }
219
+ }
220
+ };
221
+ const parseValue = () => {
222
+ skipSpace();
223
+ const char = text[index];
224
+ if (char === "{") {
225
+ const out = {};
226
+ parseItems("}", () => {
227
+ skipSpace();
228
+ if (text[index] !== "'" && text[index] !== '"')
229
+ fail();
230
+ const key = parseString();
231
+ skipSpace();
232
+ if (text[index] !== ":")
233
+ fail();
234
+ index += 1;
235
+ Object.defineProperty(out, key, {
236
+ value: parseValue(),
237
+ enumerable: true,
238
+ writable: true,
239
+ configurable: true
240
+ });
241
+ });
242
+ return out;
243
+ }
244
+ if (char === "[" || char === "(") {
245
+ const out = [];
246
+ parseItems(char === "[" ? "]" : ")", () => out.push(parseValue()));
247
+ return out;
248
+ }
249
+ if (char === "'" || char === '"')
250
+ return parseString();
251
+ PYTHON_SCALAR.lastIndex = index;
252
+ const word = PYTHON_SCALAR.exec(text)?.[0] ?? fail();
253
+ index += word.length;
254
+ if (word === "True")
255
+ return true;
256
+ if (word === "False")
257
+ return false;
258
+ if (word === "None")
259
+ return null;
260
+ return Number(word);
261
+ };
262
+ try {
263
+ const value = parseValue();
264
+ skipSpace();
265
+ return index === text.length ? value : undefined;
266
+ } catch {
267
+ return;
268
+ }
269
+ }
270
+
100
271
  // src/adapters/atif/index.ts
101
272
  var SUPPORTED_SCHEMA_VERSIONS = new Set(Array.from({ length: 8 }, (_, index) => `ATIF-v1.${index}`));
102
273
  var atifAdapter = {
@@ -239,6 +410,8 @@ function atifContent(value) {
239
410
  return jsonString(value);
240
411
  }
241
412
  function observationContent(result) {
413
+ if (typeof result.content === "string")
414
+ return serializedBlocksText(result.content);
242
415
  if (result.content != null)
243
416
  return atifContent(result.content);
244
417
  if (Array.isArray(result.subagent_trajectory_ref)) {
@@ -461,17 +634,20 @@ var codexAdapter = {
461
634
  let model;
462
635
  let createdAt;
463
636
  let sessionId;
464
- for (const { value: record, line, byteOffset } of parseJsonLines(transcript, diagnostics)) {
637
+ const lines = parseJsonLines(transcript, diagnostics);
638
+ const agentReasoning = agentReasoningTexts(lines);
639
+ for (const { value: record, line, byteOffset } of lines) {
465
640
  const recordType = record.type;
466
641
  const payload = isObject(record.payload) ? record.payload : {};
467
642
  const timestamp = parseTimestamp(record.timestamp);
468
643
  const payloadType = payload.type;
644
+ let componentIndex = 0;
469
645
  const emit = (event) => {
470
646
  events.push({
471
647
  ...event,
472
648
  sourceOffset: byteOffset,
473
649
  sourceAnchorKind: "byte",
474
- componentIndex: 0
650
+ componentIndex: componentIndex++
475
651
  });
476
652
  };
477
653
  if (recordType === "session_meta") {
@@ -507,6 +683,23 @@ var codexAdapter = {
507
683
  }
508
684
  if (recordType !== "response_item")
509
685
  continue;
686
+ if (payloadType === "reasoning") {
687
+ for (const text of reasoningSummaries(payload.summary)) {
688
+ const key = text.trim();
689
+ const repeats = agentReasoning.get(key) ?? 0;
690
+ if (repeats > 0) {
691
+ agentReasoning.set(key, repeats - 1);
692
+ continue;
693
+ }
694
+ emit({
695
+ type: "reasoning",
696
+ content: text,
697
+ inputLine: line,
698
+ ...timestamp ? { timestamp } : {}
699
+ });
700
+ }
701
+ continue;
702
+ }
510
703
  if (payloadType === "message") {
511
704
  const role = payload.role;
512
705
  const content = blocksText(payload.content);
@@ -620,6 +813,25 @@ var codexAdapter = {
620
813
  };
621
814
  }
622
815
  };
816
+ function agentReasoningTexts(lines) {
817
+ const counts = new Map;
818
+ for (const { value } of lines) {
819
+ if (value.type !== "event_msg" || !isObject(value.payload))
820
+ continue;
821
+ const { type, text } = value.payload;
822
+ if (type !== "agent_reasoning" || typeof text !== "string")
823
+ continue;
824
+ const key = text.trim();
825
+ if (key)
826
+ counts.set(key, (counts.get(key) ?? 0) + 1);
827
+ }
828
+ return counts;
829
+ }
830
+ function reasoningSummaries(summary) {
831
+ if (!Array.isArray(summary))
832
+ return [];
833
+ return summary.flatMap((part) => isObject(part) && typeof part.text === "string" && part.text.trim() ? [part.text] : []);
834
+ }
623
835
  function outputText(output) {
624
836
  if (typeof output === "string")
625
837
  return output;
@@ -3636,6 +3848,460 @@ function isPartialTranscript(input) {
3636
3848
  return (input.sourceContext?.partial ?? false) || (input.sourceContext?.baseByteOffset ?? 0) > 0;
3637
3849
  }
3638
3850
 
3851
+ // src/adapters/slack/timestamp.ts
3852
+ function parseSlackTimestamp(value) {
3853
+ if (typeof value !== "string" || !/^\d{1,12}\.\d{6}$/.test(value))
3854
+ return;
3855
+ const [seconds, micros] = value.split(".");
3856
+ if (!seconds || !micros)
3857
+ return;
3858
+ if (seconds.length > 1 && seconds.startsWith("0"))
3859
+ return;
3860
+ const date = new Date(Number(seconds) * 1000 + Number(micros.slice(0, 3)));
3861
+ if (Number.isNaN(date.getTime()) || !/^\d{4}-/.test(date.toISOString()))
3862
+ return;
3863
+ return { ts: value, date, sequence: Number(micros) };
3864
+ }
3865
+ function compareSlackTimestamps(left, right) {
3866
+ const a = BigInt(left.replace(".", ""));
3867
+ const b = BigInt(right.replace(".", ""));
3868
+ return a < b ? -1 : a > b ? 1 : 0;
3869
+ }
3870
+
3871
+ // src/adapters/slack/metadata.ts
3872
+ var MENTION = /<@([UW][A-Z0-9]+)(?:\|([^>]*))?>/g;
3873
+ function buildDirectory(value) {
3874
+ const directory = new Map;
3875
+ if (value === undefined)
3876
+ return directory;
3877
+ if (!Array.isArray(value))
3878
+ throw invalid("Slack users must be an array.");
3879
+ for (const user of value) {
3880
+ if (!isObject(user) || typeof user.id !== "string" || !user.id.trim()) {
3881
+ throw invalid("Slack users require a source user id.");
3882
+ }
3883
+ const name = profileName(user.profile) ?? label(user.real_name) ?? label(user.name);
3884
+ const existing = directory.get(user.id);
3885
+ if (existing?.name !== undefined && name !== undefined && existing.name !== name) {
3886
+ throw invalid("Conflicting names for a Slack user; supply one authoritative user snapshot.");
3887
+ }
3888
+ const merged = name ?? existing?.name;
3889
+ directory.set(user.id, {
3890
+ ...merged === undefined ? {} : { name: merged },
3891
+ bot: user.is_bot === true || existing?.bot === true
3892
+ });
3893
+ }
3894
+ return directory;
3895
+ }
3896
+ function inlineName(raw) {
3897
+ const name = profileName(raw.user_profile);
3898
+ if (name || typeof raw.bot_id !== "string" || !raw.bot_id.trim())
3899
+ return name;
3900
+ return (isObject(raw.bot_profile) ? label(raw.bot_profile.name) : undefined) ?? label(raw.username);
3901
+ }
3902
+ function isBotMessage(raw) {
3903
+ return raw.subtype === "bot_message" || isObject(raw.bot_profile) || nonemptyText(raw.user) === undefined;
3904
+ }
3905
+ function readReactions(value) {
3906
+ if (!Array.isArray(value))
3907
+ throw invalid("Slack reactions must be an array.");
3908
+ const counts = new Map;
3909
+ for (const reaction of value) {
3910
+ if (!isObject(reaction))
3911
+ throw invalid("Slack reactions must be objects.");
3912
+ const name = nonemptyText(reaction.name);
3913
+ if (!name || counts.has(name))
3914
+ throw invalid("Slack reaction names must be non-empty and unique per message.");
3915
+ const count = reaction.count;
3916
+ if (typeof count !== "number" || !Number.isSafeInteger(count) || count < 0) {
3917
+ throw invalid("Slack reaction count must be a non-negative safe integer.");
3918
+ }
3919
+ counts.set(name, count);
3920
+ }
3921
+ return Object.fromEntries([...counts].sort(([a], [b]) => compareText(a, b)));
3922
+ }
3923
+ function filePlaceholders(value) {
3924
+ if (!Array.isArray(value))
3925
+ return [];
3926
+ return value.map((file) => {
3927
+ const name = isObject(file) ? label(file.name) ?? label(file.title) : undefined;
3928
+ return name ? `[file: ${name}]` : "[file]";
3929
+ });
3930
+ }
3931
+ function mentionedUsers(text) {
3932
+ return [...text.matchAll(MENTION)].map((match) => [match[1], label(match[2])]);
3933
+ }
3934
+ function resolveMentions(text, labels) {
3935
+ return text.replace(MENTION, (mention, id) => {
3936
+ const name = labels.get(id);
3937
+ return name === undefined ? mention : `@${name}`;
3938
+ });
3939
+ }
3940
+ function assignLabels(names, bots) {
3941
+ const labels = new Map;
3942
+ const used = new Set;
3943
+ for (const id of [...names.keys()].sort(compareText)) {
3944
+ const base = names.get(id) ?? id;
3945
+ let candidate = base;
3946
+ for (let n = 2;used.has(candidate); n++)
3947
+ candidate = `${base} (${n})`;
3948
+ used.add(candidate);
3949
+ labels.set(id, candidate);
3950
+ }
3951
+ const participants = Object.fromEntries([...labels].sort(([, a], [, b]) => compareText(a, b)).map(([id, name]) => [name, bots.has(id) ? { id, bot: true } : { id }]));
3952
+ return { labels, participants };
3953
+ }
3954
+ function compareText(left, right) {
3955
+ return left < right ? -1 : left > right ? 1 : 0;
3956
+ }
3957
+ function profileName(value) {
3958
+ if (!isObject(value))
3959
+ return;
3960
+ return label(value.display_name) ?? label(value.real_name);
3961
+ }
3962
+ function label(value) {
3963
+ if (typeof value !== "string")
3964
+ return;
3965
+ return value.trim() || undefined;
3966
+ }
3967
+ function nonemptyText(value) {
3968
+ return typeof value === "string" && value.trim() ? value : undefined;
3969
+ }
3970
+ function invalid(message) {
3971
+ return new NormalizationError("invalid_input", message);
3972
+ }
3973
+
3974
+ // src/adapters/slack/index.ts
3975
+ var MESSAGE_SUBTYPES = new Set([
3976
+ "bot_message",
3977
+ "thread_broadcast",
3978
+ "file_share",
3979
+ "me_message"
3980
+ ]);
3981
+ function normalizeSlackChannel(input) {
3982
+ const channel = nonemptyString(input.channel);
3983
+ if (!channel)
3984
+ throw invalid2("Slack input requires the channel the messages were fetched from.");
3985
+ if (input.channelName !== undefined && !input.channelName.trim()) {
3986
+ throw invalid2("Slack channel name must be non-empty when supplied.");
3987
+ }
3988
+ const directory = buildDirectory(input.users);
3989
+ const diagnostics = [];
3990
+ const copies = new Map;
3991
+ const inlineNames = new Map;
3992
+ const bots = new Set([...directory].filter(([, entry]) => entry.bot).map(([id]) => id));
3993
+ for (const raw of parseSlackMessages(input.transcript)) {
3994
+ if (!isObject(raw))
3995
+ throw invalid2("Slack messages must be objects.");
3996
+ if (raw.type !== "message" || raw.subtype !== undefined && !MESSAGE_SUBTYPES.has(String(raw.subtype))) {
3997
+ diagnostics.push({
3998
+ code: "slack_message_dropped",
3999
+ message: "Skipped an unsupported Slack event or message subtype."
4000
+ });
4001
+ continue;
4002
+ }
4003
+ const time = parseSlackTimestamp(raw.ts);
4004
+ const thread = parseSlackTimestamp(raw.thread_ts ?? raw.ts);
4005
+ if (!time || !thread)
4006
+ throw invalid2("Invalid Slack message timestamp.");
4007
+ if (compareSlackTimestamps(time.ts, thread.ts) < 0) {
4008
+ throw invalid2("Slack reply precedes its thread root.");
4009
+ }
4010
+ if (raw.channel !== undefined && raw.channel !== channel) {
4011
+ throw invalid2("Slack message channel disagrees with the supplied channel.");
4012
+ }
4013
+ const speaker = nonemptyString(raw.user) ?? nonemptyString(raw.bot_id);
4014
+ if (!speaker)
4015
+ throw invalid2("Slack message has no source speaker identity.");
4016
+ const name = inlineName(raw);
4017
+ const previous = inlineNames.get(speaker);
4018
+ if (name && (!previous || compareSlackTimestamps(time.ts, previous.ts) > 0 || time.ts === previous.ts && compareText(name, previous.name) > 0)) {
4019
+ inlineNames.set(speaker, { ts: time.ts, name });
4020
+ }
4021
+ if (isBotMessage(raw))
4022
+ bots.add(speaker);
4023
+ const text = typeof raw.text === "string" && raw.text.trim() ? raw.text : undefined;
4024
+ const content = [...text ? [text] : [], ...filePlaceholders(raw.files)].join(`
4025
+ `);
4026
+ if (!content) {
4027
+ diagnostics.push({
4028
+ code: "slack_message_dropped",
4029
+ message: "Skipped a Slack post without text or files (blocks are not extracted)."
4030
+ });
4031
+ continue;
4032
+ }
4033
+ const edited = isObject(raw.edited) ? parseSlackTimestamp(raw.edited.ts)?.ts : undefined;
4034
+ const snapshot = {
4035
+ ts: time.ts,
4036
+ thread: thread.ts,
4037
+ speaker,
4038
+ content,
4039
+ timestamp: time.date.toISOString().replace(/\.\d{3}Z$/, "Z"),
4040
+ ..."reactions" in raw ? { reactions: readReactions(raw.reactions) } : {},
4041
+ ...edited ? { edited } : {}
4042
+ };
4043
+ const existing = copies.get(time.ts);
4044
+ if (existing)
4045
+ existing.push(snapshot);
4046
+ else
4047
+ copies.set(time.ts, [snapshot]);
4048
+ }
4049
+ const messages = new Map;
4050
+ for (const ts of [...copies.keys()].sort(compareSlackTimestamps)) {
4051
+ messages.set(ts, reconcile(copies.get(ts) ?? [], diagnostics));
4052
+ }
4053
+ const names = new Map;
4054
+ for (const message of messages.values()) {
4055
+ names.set(message.speaker, directory.get(message.speaker)?.name ?? inlineNames.get(message.speaker)?.name);
4056
+ }
4057
+ for (const message of messages.values()) {
4058
+ for (const [id, inline] of mentionedUsers(message.content)) {
4059
+ if (names.get(id) === undefined)
4060
+ names.set(id, directory.get(id)?.name ?? inlineNames.get(id)?.name ?? inline);
4061
+ }
4062
+ }
4063
+ const { labels, participants } = assignLabels(names, bots);
4064
+ const meta = {
4065
+ role: "meta",
4066
+ source: "slack",
4067
+ channel,
4068
+ ...input.channelName === undefined ? {} : { channel_name: input.channelName },
4069
+ participants
4070
+ };
4071
+ const toMessage = (snapshot) => ({
4072
+ id: snapshot.ts,
4073
+ speaker: labels.get(snapshot.speaker) ?? snapshot.speaker,
4074
+ content: resolveMentions(snapshot.content, labels),
4075
+ timestamp: snapshot.timestamp,
4076
+ ...snapshot.reactions === undefined ? {} : { reactions: snapshot.reactions }
4077
+ });
4078
+ const repliesOf = new Map;
4079
+ for (const message of messages.values()) {
4080
+ if (message.ts === message.thread)
4081
+ continue;
4082
+ const replies = repliesOf.get(message.thread);
4083
+ if (replies)
4084
+ replies.push(toMessage(message));
4085
+ else
4086
+ repliesOf.set(message.thread, [toMessage(message)]);
4087
+ }
4088
+ const roots = new Set([...messages.values()].filter((m) => m.ts === m.thread).map((m) => m.ts));
4089
+ const posts = [];
4090
+ for (const thread of new Set([...roots, ...repliesOf.keys()])) {
4091
+ const replies = repliesOf.get(thread) ?? [];
4092
+ const root = roots.has(thread) ? messages.get(thread) : undefined;
4093
+ if (root) {
4094
+ posts.push(replies.length > 0 ? { ...toMessage(root), replies } : toMessage(root));
4095
+ } else {
4096
+ diagnostics.push({
4097
+ code: "slack_missing_root",
4098
+ message: "Thread replies were present without their root; the root was not fabricated."
4099
+ });
4100
+ posts.push({ id: thread, missing_root: true, replies });
4101
+ }
4102
+ }
4103
+ posts.sort((a, b) => compareSlackTimestamps(a.id, b.id));
4104
+ return { records: [meta, ...posts], diagnostics };
4105
+ }
4106
+ function reconcile(snapshots, diagnostics) {
4107
+ const [first, ...rest] = snapshots;
4108
+ if (!first)
4109
+ throw invalid2("Slack message has no snapshot.");
4110
+ if (rest.some((s) => s.thread !== first.thread || s.speaker !== first.speaker)) {
4111
+ throw invalid2("Conflicting thread or speaker for one Slack message; supply one authoritative snapshot.");
4112
+ }
4113
+ const kept = snapshots.reduce((best, candidate) => rank(candidate, best) > 0 ? candidate : best);
4114
+ for (const snapshot of snapshots) {
4115
+ if (snapshot === kept)
4116
+ continue;
4117
+ if (key(snapshot) === key(kept)) {
4118
+ diagnostics.push({ code: "slack_duplicate_message", message: "Removed a duplicate Slack message." });
4119
+ } else {
4120
+ diagnostics.push({
4121
+ code: "slack_conflicting_message",
4122
+ message: "Kept one of conflicting Slack message snapshots (latest edit, then most reactions)."
4123
+ });
4124
+ }
4125
+ }
4126
+ return kept;
4127
+ }
4128
+ function rank(a, b) {
4129
+ const edited = compareSlackTimestamps(a.edited ?? "0.000000", b.edited ?? "0.000000");
4130
+ if (edited !== 0)
4131
+ return edited;
4132
+ const reactions = reactionTotal(a) - reactionTotal(b);
4133
+ if (reactions !== 0)
4134
+ return reactions;
4135
+ return compareText(key(a), key(b));
4136
+ }
4137
+ function reactionTotal(snapshot) {
4138
+ if (snapshot.reactions === undefined)
4139
+ return -1;
4140
+ return Object.values(snapshot.reactions).reduce((sum, count) => sum + count, 0);
4141
+ }
4142
+ function key(snapshot) {
4143
+ return JSON.stringify([snapshot.content, snapshot.reactions ?? null]);
4144
+ }
4145
+ function parseSlackMessages(transcript) {
4146
+ if (!transcript.trim())
4147
+ return [];
4148
+ let parsed;
4149
+ try {
4150
+ parsed = JSON.parse(transcript);
4151
+ } catch {
4152
+ const lines = transcript.split(/\r?\n/).filter((line) => line.trim());
4153
+ return lines.map((line, index) => {
4154
+ try {
4155
+ return JSON.parse(line);
4156
+ } catch {
4157
+ throw invalid2(`Slack transcript line ${index + 1} is not valid JSON.`);
4158
+ }
4159
+ });
4160
+ }
4161
+ if (Array.isArray(parsed))
4162
+ return parsed;
4163
+ if (isObject(parsed) && Array.isArray(parsed.messages))
4164
+ return parsed.messages;
4165
+ throw invalid2("Slack transcript must be JSONL rows, a JSON array, or a conversations.history response.");
4166
+ }
4167
+ function invalid2(message) {
4168
+ return new NormalizationError("invalid_input", message);
4169
+ }
4170
+
4171
+ // src/conversations/validate.ts
4172
+ var META_KEYS2 = new Set(["role", "source", "channel", "channel_name", "participants"]);
4173
+ var MESSAGE_KEYS = new Set(["id", "speaker", "content", "timestamp", "reactions"]);
4174
+ var POST_KEYS = new Set([...MESSAGE_KEYS, "replies"]);
4175
+ var FRAGMENT_KEYS = new Set(["id", "missing_root", "replies"]);
4176
+ var PARTICIPANT_KEYS = new Set(["id", "bot"]);
4177
+ var ISO_TIMESTAMP = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:?\d{2})$/;
4178
+ function validateConversation(value) {
4179
+ if (!Array.isArray(value) || value.length < 1) {
4180
+ fail2("Conversation requires leading metadata.");
4181
+ }
4182
+ const meta = value[0];
4183
+ if (!isObject(meta) || meta.role !== "meta" || !nonempty(meta.source) || !nonempty(meta.channel)) {
4184
+ fail2("Conversation requires leading meta with source and channel.");
4185
+ }
4186
+ exactKeys2(meta, META_KEYS2);
4187
+ if ("channel_name" in meta && !nonempty(meta.channel_name)) {
4188
+ fail2("Channel name must be non-empty when present.");
4189
+ }
4190
+ const labels = validateParticipants(meta.participants);
4191
+ const ids = new Set;
4192
+ for (const record of value.slice(1)) {
4193
+ if (!isObject(record) || "role" in record) {
4194
+ fail2("Conversation body must contain only posts, not agent roles.");
4195
+ }
4196
+ if ("missing_root" in record) {
4197
+ exactKeys2(record, FRAGMENT_KEYS);
4198
+ if (record.missing_root !== true)
4199
+ fail2("Thread fragments must set missing_root to true.");
4200
+ if (!("replies" in record))
4201
+ fail2("Thread fragments require replies.");
4202
+ claimId(record.id, ids);
4203
+ } else {
4204
+ exactKeys2(record, POST_KEYS);
4205
+ validateMessage(record, ids, labels);
4206
+ }
4207
+ if ("replies" in record) {
4208
+ if (!Array.isArray(record.replies) || record.replies.length === 0) {
4209
+ fail2("Replies must be a non-empty array; omit the field for posts without replies.");
4210
+ }
4211
+ for (const reply of record.replies) {
4212
+ if (!isObject(reply))
4213
+ fail2("Replies must be objects.");
4214
+ exactKeys2(reply, MESSAGE_KEYS);
4215
+ validateMessage(reply, ids, labels);
4216
+ }
4217
+ }
4218
+ }
4219
+ }
4220
+ function validateParticipants(value) {
4221
+ if (!isObject(value))
4222
+ fail2("Conversation meta requires a participants object.");
4223
+ const ids = new Set;
4224
+ for (const [label2, participant] of Object.entries(value)) {
4225
+ if (!nonempty(label2))
4226
+ fail2("Participant labels must be non-empty.");
4227
+ if (!isObject(participant) || !nonempty(participant.id))
4228
+ fail2("Participants must contain a non-empty id.");
4229
+ exactKeys2(participant, PARTICIPANT_KEYS);
4230
+ if ("bot" in participant && participant.bot !== true)
4231
+ fail2("Participant bot flag must be true when present.");
4232
+ if (ids.has(participant.id))
4233
+ fail2("Participant IDs must be unique.");
4234
+ ids.add(participant.id);
4235
+ }
4236
+ return new Set(Object.keys(value));
4237
+ }
4238
+ function validateMessage(record, ids, labels) {
4239
+ claimId(record.id, ids);
4240
+ if (typeof record.speaker !== "string" || !labels.has(record.speaker)) {
4241
+ fail2("Message speaker must be a participant label.");
4242
+ }
4243
+ if ("reactions" in record)
4244
+ validateReactions(record.reactions);
4245
+ if (!nonempty(record.content))
4246
+ fail2("Message content must be non-empty text.");
4247
+ if (typeof record.timestamp !== "string" || !ISO_TIMESTAMP.test(record.timestamp) || Number.isNaN(Date.parse(record.timestamp))) {
4248
+ fail2("Message timestamp must be a valid ISO timestamp.");
4249
+ }
4250
+ }
4251
+ function claimId(id, ids) {
4252
+ if (!nonempty(id) || ids.has(id)) {
4253
+ fail2("Message IDs must be non-empty and unique within the conversation.");
4254
+ }
4255
+ ids.add(id);
4256
+ }
4257
+ function validateReactions(value) {
4258
+ if (!isObject(value))
4259
+ fail2("Reactions must map reaction names to counts.");
4260
+ for (const [name, count] of Object.entries(value)) {
4261
+ if (!nonempty(name))
4262
+ fail2("Reaction names must be non-empty.");
4263
+ if (typeof count !== "number" || !Number.isSafeInteger(count) || count < 0) {
4264
+ fail2("Reaction count must be a non-negative safe integer.");
4265
+ }
4266
+ }
4267
+ }
4268
+ function nonempty(value) {
4269
+ return typeof value === "string" && value.trim().length > 0;
4270
+ }
4271
+ function exactKeys2(record, allowed) {
4272
+ for (const key2 of Object.keys(record)) {
4273
+ if (!allowed.has(key2))
4274
+ fail2(`Unexpected conversation field: ${key2}`);
4275
+ }
4276
+ }
4277
+ function fail2(message) {
4278
+ throw new NormalizationError("invalid_input", message);
4279
+ }
4280
+
4281
+ // src/conversations/index.ts
4282
+ function normalizeConversation(input) {
4283
+ if (!input || typeof input !== "object" || typeof input.transcript !== "string") {
4284
+ throw new NormalizationError("invalid_input", "Expected a conversation source, transcript string, and channel.");
4285
+ }
4286
+ if (input.source !== "slack") {
4287
+ throw new NormalizationError("unknown_source", `Unsupported conversation source: ${input.source}`);
4288
+ }
4289
+ if (input.users !== undefined && !Array.isArray(input.users)) {
4290
+ throw new NormalizationError("invalid_input", "Slack users must be an array.");
4291
+ }
4292
+ if (input.channelName !== undefined && typeof input.channelName !== "string") {
4293
+ throw new NormalizationError("invalid_input", "Channel name must be a string.");
4294
+ }
4295
+ const result = normalizeSlackChannel({
4296
+ transcript: input.transcript,
4297
+ channel: input.channel,
4298
+ ...input.channelName === undefined ? {} : { channelName: input.channelName },
4299
+ ...input.users === undefined ? {} : { users: input.users }
4300
+ });
4301
+ validateConversation(result.records);
4302
+ return result;
4303
+ }
4304
+
3639
4305
  // src/python-cli.ts
3640
4306
  var PROTOCOL_VERSION = 1;
3641
4307
  async function main() {
@@ -3643,6 +4309,29 @@ async function main() {
3643
4309
  const results = [];
3644
4310
  for (const input of request.requests) {
3645
4311
  try {
4312
+ if (isObject(input) && "conversation" in input) {
4313
+ const request2 = input.conversation;
4314
+ if (!isObject(request2) || typeof request2.transcript !== "string" || typeof request2.channel !== "string") {
4315
+ throw new NormalizationError("invalid_input", "Expected a conversation source, transcript, and channel.");
4316
+ }
4317
+ if (request2.source !== "slack") {
4318
+ throw new NormalizationError("unknown_source", "Unsupported conversation source.");
4319
+ }
4320
+ if (request2.users !== undefined && !Array.isArray(request2.users)) {
4321
+ throw new NormalizationError("invalid_input", "Slack users must be an array.");
4322
+ }
4323
+ if (request2.channelName !== undefined && typeof request2.channelName !== "string") {
4324
+ throw new NormalizationError("invalid_input", "Channel name must be a string.");
4325
+ }
4326
+ results.push({ ok: true, result: normalizeConversation({
4327
+ source: request2.source,
4328
+ transcript: request2.transcript,
4329
+ channel: request2.channel,
4330
+ ...request2.channelName === undefined ? {} : { channelName: request2.channelName },
4331
+ ...request2.users === undefined ? {} : { users: request2.users }
4332
+ }) });
4333
+ continue;
4334
+ }
3646
4335
  const result = input !== null && typeof input === "object" && "list" in input ? await listTrajectories(input.list) : input !== null && typeof input === "object" && ("source" in input) && input.source === "deepagents" ? await normalizeCheckpoint(input) : normalizeTranscript(input);
3647
4336
  results.push({
3648
4337
  ok: true,
@@ -0,0 +1,117 @@
1
+ """Imported conversations, separate from agent trajectory records and APIs."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from typing import Literal, TypedDict, Union
7
+
8
+ from ._client import _run_bridge
9
+ from ._errors import NormalizationError, TrajectoryRuntimeError
10
+
11
+ ConversationSource = Literal["slack"]
12
+
13
+
14
+ class _ConversationParticipantOptional(TypedDict, total=False):
15
+ bot: Literal[True]
16
+
17
+
18
+ class ConversationParticipant(_ConversationParticipantOptional):
19
+ id: str
20
+
21
+
22
+ class _ConversationMetaOptional(TypedDict, total=False):
23
+ channel_name: str
24
+
25
+
26
+ class ConversationMetaRecord(_ConversationMetaOptional):
27
+ role: Literal["meta"]
28
+ source: str
29
+ channel: str
30
+ participants: dict[str, ConversationParticipant]
31
+
32
+
33
+ class _ConversationMessageOptional(TypedDict, total=False):
34
+ reactions: dict[str, int]
35
+
36
+
37
+ class ConversationMessage(_ConversationMessageOptional):
38
+ id: str
39
+ speaker: str
40
+ content: str
41
+ timestamp: str
42
+
43
+
44
+ class ConversationPost(ConversationMessage, total=False):
45
+ """A top-level post; ``replies`` is present only when the thread has replies."""
46
+
47
+ replies: list[ConversationMessage]
48
+
49
+
50
+ class ConversationThreadFragment(TypedDict):
51
+ """Replies whose root was not in the input; ``id`` is the root's source ID."""
52
+
53
+ id: str
54
+ missing_root: Literal[True]
55
+ replies: list[ConversationMessage]
56
+
57
+
58
+ ConversationRecord = Union[ConversationMetaRecord, ConversationPost, ConversationThreadFragment]
59
+
60
+
61
+ class ConversationDiagnostic(TypedDict):
62
+ code: Literal[
63
+ "slack_message_dropped", "slack_duplicate_message", "slack_conflicting_message", "slack_missing_root"
64
+ ]
65
+ message: str
66
+
67
+
68
+ class NormalizeConversationResult(TypedDict):
69
+ records: list[ConversationRecord]
70
+ diagnostics: list[ConversationDiagnostic]
71
+
72
+
73
+ def normalize_conversation(
74
+ *,
75
+ source: ConversationSource,
76
+ transcript: str,
77
+ channel: str,
78
+ channel_name: str | None = None,
79
+ users: list[object] | None = None,
80
+ ) -> NormalizeConversationResult:
81
+ """Normalize one channel's raw messages (JSONL, JSON array, or history response) into one conversation.
82
+
83
+ Slack requires ``channel`` to fetch messages and does not echo it back, so the
84
+ caller supplies it, along with an optional readable ``channel_name``. ``users``
85
+ are raw ``users.list`` rows used only for display names and bot flags.
86
+ """
87
+ request: dict[str, object] = {"source": source, "transcript": transcript, "channel": channel}
88
+ if channel_name is not None:
89
+ request["channelName"] = channel_name
90
+ if users is not None:
91
+ request["users"] = users
92
+ return _bridge_request({"conversation": request})
93
+
94
+
95
+ def _bridge_request(request: dict[str, object]) -> dict:
96
+ payload = json.dumps({"version": 1, "requests": [request]}, ensure_ascii=False)
97
+ completed = _run_bridge(payload)
98
+ try:
99
+ response = json.loads(completed.stdout)
100
+ except json.JSONDecodeError as error:
101
+ raise TrajectoryRuntimeError("Conversation runtime returned invalid JSON.") from error
102
+ if not isinstance(response, dict) or response.get("version") != 1:
103
+ raise TrajectoryRuntimeError("Conversation runtime protocol version did not match.")
104
+ results = response.get("results")
105
+ if not isinstance(results, list) or len(results) != 1 or not isinstance(results[0], dict):
106
+ raise TrajectoryRuntimeError("Conversation runtime returned an invalid result count.")
107
+ item = results[0]
108
+ if item.get("ok") is True and isinstance(item.get("result"), dict):
109
+ return item["result"]
110
+ error = item.get("error")
111
+ if item.get("ok") is False and isinstance(error, dict) and isinstance(error.get("message"), str):
112
+ if error.get("code") == "invalid_input":
113
+ raise NormalizationError("invalid_input", error["message"], input_index=0)
114
+ if error.get("code") == "unknown_source":
115
+ raise NormalizationError("unknown_source", error["message"], input_index=0)
116
+ raise TrajectoryRuntimeError(error["message"])
117
+ raise TrajectoryRuntimeError("Conversation runtime returned an invalid result.")