@ai-matrx/agents 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,198 @@
1
+ 'use strict';
2
+
3
+ // projection/request.ts
4
+ var asRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
5
+ var asString = (value) => typeof value === "string" ? value : null;
6
+ var asNumber = (value) => typeof value === "number" && Number.isFinite(value) ? value : null;
7
+ function createAgentRequestProjection(input) {
8
+ return {
9
+ requestId: input.requestId,
10
+ conversationId: input.conversationId ?? null,
11
+ status: "pending",
12
+ answer: "",
13
+ reasoning: "",
14
+ reasoningActive: false,
15
+ phase: null,
16
+ phaseHistory: [],
17
+ operations: {},
18
+ tools: {},
19
+ renderBlocks: {},
20
+ renderBlockOrder: [],
21
+ completion: null,
22
+ error: null,
23
+ lastTransportSeq: 0,
24
+ eventCount: 0
25
+ };
26
+ }
27
+ function toolStatus(event) {
28
+ switch (event) {
29
+ case "tool_started":
30
+ return "started";
31
+ case "tool_step":
32
+ return "step";
33
+ case "tool_result_preview":
34
+ return "preview";
35
+ case "tool_completed":
36
+ return "completed";
37
+ case "tool_error":
38
+ return "error";
39
+ case "tool_delegated":
40
+ return "delegated";
41
+ default:
42
+ return "progress";
43
+ }
44
+ }
45
+ function projectAgentEvent(current, event) {
46
+ const streamSeq = asNumber(event.stream_seq);
47
+ if (streamSeq !== null && streamSeq <= current.lastTransportSeq) return current;
48
+ const data = asRecord(event.data);
49
+ const next = {
50
+ ...current,
51
+ status: current.status === "pending" ? "streaming" : current.status,
52
+ lastTransportSeq: streamSeq ?? current.lastTransportSeq,
53
+ eventCount: current.eventCount + 1
54
+ };
55
+ switch (event.event) {
56
+ case "chunk": {
57
+ const text = asString(data.text);
58
+ return text === null ? next : { ...next, answer: current.answer + text };
59
+ }
60
+ case "reasoning_chunk": {
61
+ const text = asString(data.text);
62
+ return text === null ? next : {
63
+ ...next,
64
+ reasoning: current.reasoning + text,
65
+ reasoningActive: true
66
+ };
67
+ }
68
+ case "reasoning":
69
+ return {
70
+ ...next,
71
+ reasoningActive: data.state === "started"
72
+ };
73
+ case "phase": {
74
+ const phase = asString(data.phase);
75
+ if (phase === null) return next;
76
+ return {
77
+ ...next,
78
+ phase,
79
+ phaseHistory: [...current.phaseHistory, phase]
80
+ };
81
+ }
82
+ case "init": {
83
+ const operationId = asString(data.operation_id);
84
+ const operation = asString(data.operation);
85
+ if (operationId === null || operation === null) return next;
86
+ return {
87
+ ...next,
88
+ operations: {
89
+ ...current.operations,
90
+ [operationId]: {
91
+ operationId,
92
+ operation,
93
+ parentOperationId: asString(data.parent_operation_id),
94
+ status: "active",
95
+ metadata: Object.keys(asRecord(data.metadata)).length ? asRecord(data.metadata) : null,
96
+ result: null
97
+ }
98
+ }
99
+ };
100
+ }
101
+ case "completion": {
102
+ const operationId = asString(data.operation_id);
103
+ const operation = asString(data.operation);
104
+ const rawStatus = asString(data.status);
105
+ const status = rawStatus === "failed" || rawStatus === "cancelled" ? rawStatus : "success";
106
+ const result = asRecord(data.result);
107
+ const operations = operationId ? {
108
+ ...current.operations,
109
+ [operationId]: {
110
+ ...current.operations[operationId] ?? {
111
+ operationId,
112
+ operation: operation ?? "unknown",
113
+ parentOperationId: null,
114
+ metadata: null
115
+ },
116
+ status,
117
+ result
118
+ }
119
+ } : current.operations;
120
+ if (operation === "user_request") {
121
+ return {
122
+ ...next,
123
+ operations,
124
+ completion: data,
125
+ status: status === "success" ? "complete" : status === "cancelled" ? "cancelled" : "error"
126
+ };
127
+ }
128
+ return { ...next, operations };
129
+ }
130
+ case "tool_event": {
131
+ const callId = asString(data.call_id);
132
+ const toolName = asString(data.tool_name);
133
+ const lifecycle = asString(data.event);
134
+ if (callId === null || toolName === null || lifecycle === null) return next;
135
+ const status = toolStatus(lifecycle);
136
+ return {
137
+ ...next,
138
+ status: status === "started" || status === "delegated" ? "awaiting-tools" : status === "completed" || status === "error" ? "streaming" : next.status,
139
+ tools: {
140
+ ...current.tools,
141
+ [callId]: {
142
+ callId,
143
+ toolName,
144
+ status,
145
+ message: asString(data.message),
146
+ data: Object.keys(asRecord(data.data)).length ? asRecord(data.data) : null
147
+ }
148
+ }
149
+ };
150
+ }
151
+ case "render_block": {
152
+ const blockId = asString(data.blockId);
153
+ const blockIndex = asNumber(data.blockIndex);
154
+ const type = asString(data.type);
155
+ if (blockId === null || blockIndex === null || type === null) return next;
156
+ const alreadyKnown = Object.hasOwn(current.renderBlocks, blockId);
157
+ return {
158
+ ...next,
159
+ renderBlocks: {
160
+ ...current.renderBlocks,
161
+ [blockId]: {
162
+ blockId,
163
+ blockIndex,
164
+ type,
165
+ status: data.status === "complete" || data.status === "error" ? data.status : "streaming",
166
+ content: asString(data.content),
167
+ data: Object.keys(asRecord(data.data)).length ? asRecord(data.data) : null,
168
+ metadata: Object.keys(asRecord(data.metadata)).length ? asRecord(data.metadata) : null
169
+ }
170
+ },
171
+ renderBlockOrder: alreadyKnown ? current.renderBlockOrder : [...current.renderBlockOrder, blockId]
172
+ };
173
+ }
174
+ case "error":
175
+ return { ...next, status: "error", error: data };
176
+ case "end":
177
+ return {
178
+ ...next,
179
+ status: current.status === "error" || current.status === "cancelled" ? current.status : "complete",
180
+ reasoningActive: false
181
+ };
182
+ case "data": {
183
+ const conversationId = data.type === "conversation_id" ? asString(data.conversation_id) : null;
184
+ return conversationId === null ? next : { ...next, conversationId };
185
+ }
186
+ default:
187
+ return next;
188
+ }
189
+ }
190
+ function projectAgentEvents(initial, events) {
191
+ return events.reduce(projectAgentEvent, initial);
192
+ }
193
+
194
+ exports.createAgentRequestProjection = createAgentRequestProjection;
195
+ exports.projectAgentEvent = projectAgentEvent;
196
+ exports.projectAgentEvents = projectAgentEvents;
197
+ //# sourceMappingURL=request.cjs.map
198
+ //# sourceMappingURL=request.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../projection/request.ts"],"names":[],"mappings":";;;AAmEA,IAAM,QAAA,GAA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A,EAAM,MAAA,CAAO,IAAA,CAAK,QAAA,CAAS,IAAA,CAAK,IAAI,CAAC,CAAA,CAAE,MAAA,GAAS,QAAA,CAAS,IAAA,CAAK,IAAI,CAAA,GAAI,IAAA;AAAA,YACtE,QAAA,EAAU,MAAA,CAAO,IAAA,CAAK,QAAA,CAAS,IAAA,CAAK,QAAQ,CAAC,CAAA,CAAE,MAAA,GAC3C,QAAA,CAAS,IAAA,CAAK,QAAQ,CAAA,GACtB;AAAA;AACN,SACF;AAAA,QACA,gBAAA,EAAkB,eACd,OAAA,CAAQ,gBAAA,GACR,CAAC,GAAG,OAAA,CAAQ,kBAAkB,OAAO;AAAA,OAC3C;AAAA,IACF;AAAA,IACA,KAAK,OAAA;AACH,MAAA,OAAO,EAAE,GAAG,IAAA,EAAM,MAAA,EAAQ,OAAA,EAAS,OAAO,IAAA,EAAK;AAAA,IACjD,KAAK,KAAA;AACH,MAAA,OAAO;AAAA,QACL,GAAG,IAAA;AAAA,QACH,MAAA,EACE,QAAQ,MAAA,KAAW,OAAA,IAAW,QAAQ,MAAA,KAAW,WAAA,GAC7C,QAAQ,MAAA,GACR,UAAA;AAAA,QACN,eAAA,EAAiB;AAAA,OACnB;AAAA,IACF,KAAK,MAAA,EAAQ;AACX,MAAA,MAAM,iBACJ,IAAA,CAAK,IAAA,KAAS,oBAAoB,QAAA,CAAS,IAAA,CAAK,eAAe,CAAA,GAAI,IAAA;AACrE,MAAA,OAAO,mBAAmB,IAAA,GAAO,IAAA,GAAO,EAAE,GAAG,MAAM,cAAA,EAAe;AAAA,IACpE;AAAA,IACA;AACE,MAAA,OAAO,IAAA;AAAA;AAEb;AAEO,SAAS,kBAAA,CACd,SACA,MAAA,EACwB;AACxB,EAAA,OAAO,MAAA,CAAO,MAAA,CAAO,iBAAA,EAAmB,OAAO,CAAA;AACjD","file":"request.cjs","sourcesContent":["export type AgentProjectionStatus =\n | \"pending\"\n | \"streaming\"\n | \"awaiting-tools\"\n | \"complete\"\n | \"error\"\n | \"cancelled\";\n\nexport interface AgentProjectionOperation {\n operationId: string;\n operation: string;\n parentOperationId: string | null;\n status: \"active\" | \"success\" | \"failed\" | \"cancelled\";\n metadata: Record<string, unknown> | null;\n result: Record<string, unknown> | null;\n}\n\nexport interface AgentProjectionTool {\n callId: string;\n toolName: string;\n status:\n | \"started\"\n | \"progress\"\n | \"step\"\n | \"preview\"\n | \"completed\"\n | \"error\"\n | \"delegated\";\n message: string | null;\n data: Record<string, unknown> | null;\n}\n\nexport interface AgentProjectionRenderBlock {\n blockId: string;\n blockIndex: number;\n type: string;\n status: \"streaming\" | \"complete\" | \"error\";\n content: string | null;\n data: Record<string, unknown> | null;\n metadata: Record<string, unknown> | null;\n}\n\nexport interface AgentRequestProjection {\n requestId: string;\n conversationId: string | null;\n status: AgentProjectionStatus;\n answer: string;\n reasoning: string;\n reasoningActive: boolean;\n phase: string | null;\n phaseHistory: string[];\n operations: Record<string, AgentProjectionOperation>;\n tools: Record<string, AgentProjectionTool>;\n renderBlocks: Record<string, AgentProjectionRenderBlock>;\n renderBlockOrder: string[];\n completion: Record<string, unknown> | null;\n error: Record<string, unknown> | null;\n lastTransportSeq: number;\n eventCount: number;\n}\n\nexport interface AgentProjectionEvent {\n event: string;\n data?: unknown;\n stream_seq?: number;\n}\n\nconst asRecord = (value: unknown): Record<string, unknown> =>\n value !== null && typeof value === \"object\" && !Array.isArray(value)\n ? (value as Record<string, unknown>)\n : {};\n\nconst asString = (value: unknown): string | null =>\n typeof value === \"string\" ? value : null;\n\nconst asNumber = (value: unknown): number | null =>\n typeof value === \"number\" && Number.isFinite(value) ? value : null;\n\nexport function createAgentRequestProjection(input: {\n requestId: string;\n conversationId?: string | null;\n}): AgentRequestProjection {\n return {\n requestId: input.requestId,\n conversationId: input.conversationId ?? null,\n status: \"pending\",\n answer: \"\",\n reasoning: \"\",\n reasoningActive: false,\n phase: null,\n phaseHistory: [],\n operations: {},\n tools: {},\n renderBlocks: {},\n renderBlockOrder: [],\n completion: null,\n error: null,\n lastTransportSeq: 0,\n eventCount: 0,\n };\n}\n\nfunction toolStatus(event: string): AgentProjectionTool[\"status\"] {\n switch (event) {\n case \"tool_started\":\n return \"started\";\n case \"tool_step\":\n return \"step\";\n case \"tool_result_preview\":\n return \"preview\";\n case \"tool_completed\":\n return \"completed\";\n case \"tool_error\":\n return \"error\";\n case \"tool_delegated\":\n return \"delegated\";\n default:\n return \"progress\";\n }\n}\n\nexport function projectAgentEvent(\n current: AgentRequestProjection,\n event: AgentProjectionEvent,\n): AgentRequestProjection {\n const streamSeq = asNumber(event.stream_seq);\n if (streamSeq !== null && streamSeq <= current.lastTransportSeq) return current;\n\n const data = asRecord(event.data);\n const next: AgentRequestProjection = {\n ...current,\n status: current.status === \"pending\" ? \"streaming\" : current.status,\n lastTransportSeq: streamSeq ?? current.lastTransportSeq,\n eventCount: current.eventCount + 1,\n };\n\n switch (event.event) {\n case \"chunk\": {\n const text = asString(data.text);\n return text === null ? next : { ...next, answer: current.answer + text };\n }\n case \"reasoning_chunk\": {\n const text = asString(data.text);\n return text === null\n ? next\n : {\n ...next,\n reasoning: current.reasoning + text,\n reasoningActive: true,\n };\n }\n case \"reasoning\":\n return {\n ...next,\n reasoningActive: data.state === \"started\",\n };\n case \"phase\": {\n const phase = asString(data.phase);\n if (phase === null) return next;\n return {\n ...next,\n phase,\n phaseHistory: [...current.phaseHistory, phase],\n };\n }\n case \"init\": {\n const operationId = asString(data.operation_id);\n const operation = asString(data.operation);\n if (operationId === null || operation === null) return next;\n return {\n ...next,\n operations: {\n ...current.operations,\n [operationId]: {\n operationId,\n operation,\n parentOperationId: asString(data.parent_operation_id),\n status: \"active\",\n metadata: Object.keys(asRecord(data.metadata)).length\n ? asRecord(data.metadata)\n : null,\n result: null,\n },\n },\n };\n }\n case \"completion\": {\n const operationId = asString(data.operation_id);\n const operation = asString(data.operation);\n const rawStatus = asString(data.status);\n const status: AgentProjectionOperation[\"status\"] =\n rawStatus === \"failed\" || rawStatus === \"cancelled\"\n ? rawStatus\n : \"success\";\n const result = asRecord(data.result);\n const operations = operationId\n ? {\n ...current.operations,\n [operationId]: {\n ...(current.operations[operationId] ?? {\n operationId,\n operation: operation ?? \"unknown\",\n parentOperationId: null,\n metadata: null,\n }),\n status,\n result,\n },\n }\n : current.operations;\n if (operation === \"user_request\") {\n return {\n ...next,\n operations,\n completion: data,\n status:\n status === \"success\"\n ? \"complete\"\n : status === \"cancelled\"\n ? \"cancelled\"\n : \"error\",\n };\n }\n return { ...next, operations };\n }\n case \"tool_event\": {\n const callId = asString(data.call_id);\n const toolName = asString(data.tool_name);\n const lifecycle = asString(data.event);\n if (callId === null || toolName === null || lifecycle === null) return next;\n const status = toolStatus(lifecycle);\n return {\n ...next,\n status:\n status === \"started\" || status === \"delegated\"\n ? \"awaiting-tools\"\n : status === \"completed\" || status === \"error\"\n ? \"streaming\"\n : next.status,\n tools: {\n ...current.tools,\n [callId]: {\n callId,\n toolName,\n status,\n message: asString(data.message),\n data: Object.keys(asRecord(data.data)).length ? asRecord(data.data) : null,\n },\n },\n };\n }\n case \"render_block\": {\n const blockId = asString(data.blockId);\n const blockIndex = asNumber(data.blockIndex);\n const type = asString(data.type);\n if (blockId === null || blockIndex === null || type === null) return next;\n const alreadyKnown = Object.hasOwn(current.renderBlocks, blockId);\n return {\n ...next,\n renderBlocks: {\n ...current.renderBlocks,\n [blockId]: {\n blockId,\n blockIndex,\n type,\n status:\n data.status === \"complete\" || data.status === \"error\"\n ? data.status\n : \"streaming\",\n content: asString(data.content),\n data: Object.keys(asRecord(data.data)).length ? asRecord(data.data) : null,\n metadata: Object.keys(asRecord(data.metadata)).length\n ? asRecord(data.metadata)\n : null,\n },\n },\n renderBlockOrder: alreadyKnown\n ? current.renderBlockOrder\n : [...current.renderBlockOrder, blockId],\n };\n }\n case \"error\":\n return { ...next, status: \"error\", error: data };\n case \"end\":\n return {\n ...next,\n status:\n current.status === \"error\" || current.status === \"cancelled\"\n ? current.status\n : \"complete\",\n reasoningActive: false,\n };\n case \"data\": {\n const conversationId =\n data.type === \"conversation_id\" ? asString(data.conversation_id) : null;\n return conversationId === null ? next : { ...next, conversationId };\n }\n default:\n return next;\n }\n}\n\nexport function projectAgentEvents(\n initial: AgentRequestProjection,\n events: readonly AgentProjectionEvent[],\n): AgentRequestProjection {\n return events.reduce(projectAgentEvent, initial);\n}\n"]}
@@ -0,0 +1,56 @@
1
+ type AgentProjectionStatus = "pending" | "streaming" | "awaiting-tools" | "complete" | "error" | "cancelled";
2
+ interface AgentProjectionOperation {
3
+ operationId: string;
4
+ operation: string;
5
+ parentOperationId: string | null;
6
+ status: "active" | "success" | "failed" | "cancelled";
7
+ metadata: Record<string, unknown> | null;
8
+ result: Record<string, unknown> | null;
9
+ }
10
+ interface AgentProjectionTool {
11
+ callId: string;
12
+ toolName: string;
13
+ status: "started" | "progress" | "step" | "preview" | "completed" | "error" | "delegated";
14
+ message: string | null;
15
+ data: Record<string, unknown> | null;
16
+ }
17
+ interface AgentProjectionRenderBlock {
18
+ blockId: string;
19
+ blockIndex: number;
20
+ type: string;
21
+ status: "streaming" | "complete" | "error";
22
+ content: string | null;
23
+ data: Record<string, unknown> | null;
24
+ metadata: Record<string, unknown> | null;
25
+ }
26
+ interface AgentRequestProjection {
27
+ requestId: string;
28
+ conversationId: string | null;
29
+ status: AgentProjectionStatus;
30
+ answer: string;
31
+ reasoning: string;
32
+ reasoningActive: boolean;
33
+ phase: string | null;
34
+ phaseHistory: string[];
35
+ operations: Record<string, AgentProjectionOperation>;
36
+ tools: Record<string, AgentProjectionTool>;
37
+ renderBlocks: Record<string, AgentProjectionRenderBlock>;
38
+ renderBlockOrder: string[];
39
+ completion: Record<string, unknown> | null;
40
+ error: Record<string, unknown> | null;
41
+ lastTransportSeq: number;
42
+ eventCount: number;
43
+ }
44
+ interface AgentProjectionEvent {
45
+ event: string;
46
+ data?: unknown;
47
+ stream_seq?: number;
48
+ }
49
+ declare function createAgentRequestProjection(input: {
50
+ requestId: string;
51
+ conversationId?: string | null;
52
+ }): AgentRequestProjection;
53
+ declare function projectAgentEvent(current: AgentRequestProjection, event: AgentProjectionEvent): AgentRequestProjection;
54
+ declare function projectAgentEvents(initial: AgentRequestProjection, events: readonly AgentProjectionEvent[]): AgentRequestProjection;
55
+
56
+ export { type AgentProjectionEvent, type AgentProjectionOperation, type AgentProjectionRenderBlock, type AgentProjectionStatus, type AgentProjectionTool, type AgentRequestProjection, createAgentRequestProjection, projectAgentEvent, projectAgentEvents };
@@ -0,0 +1,56 @@
1
+ type AgentProjectionStatus = "pending" | "streaming" | "awaiting-tools" | "complete" | "error" | "cancelled";
2
+ interface AgentProjectionOperation {
3
+ operationId: string;
4
+ operation: string;
5
+ parentOperationId: string | null;
6
+ status: "active" | "success" | "failed" | "cancelled";
7
+ metadata: Record<string, unknown> | null;
8
+ result: Record<string, unknown> | null;
9
+ }
10
+ interface AgentProjectionTool {
11
+ callId: string;
12
+ toolName: string;
13
+ status: "started" | "progress" | "step" | "preview" | "completed" | "error" | "delegated";
14
+ message: string | null;
15
+ data: Record<string, unknown> | null;
16
+ }
17
+ interface AgentProjectionRenderBlock {
18
+ blockId: string;
19
+ blockIndex: number;
20
+ type: string;
21
+ status: "streaming" | "complete" | "error";
22
+ content: string | null;
23
+ data: Record<string, unknown> | null;
24
+ metadata: Record<string, unknown> | null;
25
+ }
26
+ interface AgentRequestProjection {
27
+ requestId: string;
28
+ conversationId: string | null;
29
+ status: AgentProjectionStatus;
30
+ answer: string;
31
+ reasoning: string;
32
+ reasoningActive: boolean;
33
+ phase: string | null;
34
+ phaseHistory: string[];
35
+ operations: Record<string, AgentProjectionOperation>;
36
+ tools: Record<string, AgentProjectionTool>;
37
+ renderBlocks: Record<string, AgentProjectionRenderBlock>;
38
+ renderBlockOrder: string[];
39
+ completion: Record<string, unknown> | null;
40
+ error: Record<string, unknown> | null;
41
+ lastTransportSeq: number;
42
+ eventCount: number;
43
+ }
44
+ interface AgentProjectionEvent {
45
+ event: string;
46
+ data?: unknown;
47
+ stream_seq?: number;
48
+ }
49
+ declare function createAgentRequestProjection(input: {
50
+ requestId: string;
51
+ conversationId?: string | null;
52
+ }): AgentRequestProjection;
53
+ declare function projectAgentEvent(current: AgentRequestProjection, event: AgentProjectionEvent): AgentRequestProjection;
54
+ declare function projectAgentEvents(initial: AgentRequestProjection, events: readonly AgentProjectionEvent[]): AgentRequestProjection;
55
+
56
+ export { type AgentProjectionEvent, type AgentProjectionOperation, type AgentProjectionRenderBlock, type AgentProjectionStatus, type AgentProjectionTool, type AgentRequestProjection, createAgentRequestProjection, projectAgentEvent, projectAgentEvents };
@@ -0,0 +1,194 @@
1
+ // projection/request.ts
2
+ var asRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
3
+ var asString = (value) => typeof value === "string" ? value : null;
4
+ var asNumber = (value) => typeof value === "number" && Number.isFinite(value) ? value : null;
5
+ function createAgentRequestProjection(input) {
6
+ return {
7
+ requestId: input.requestId,
8
+ conversationId: input.conversationId ?? null,
9
+ status: "pending",
10
+ answer: "",
11
+ reasoning: "",
12
+ reasoningActive: false,
13
+ phase: null,
14
+ phaseHistory: [],
15
+ operations: {},
16
+ tools: {},
17
+ renderBlocks: {},
18
+ renderBlockOrder: [],
19
+ completion: null,
20
+ error: null,
21
+ lastTransportSeq: 0,
22
+ eventCount: 0
23
+ };
24
+ }
25
+ function toolStatus(event) {
26
+ switch (event) {
27
+ case "tool_started":
28
+ return "started";
29
+ case "tool_step":
30
+ return "step";
31
+ case "tool_result_preview":
32
+ return "preview";
33
+ case "tool_completed":
34
+ return "completed";
35
+ case "tool_error":
36
+ return "error";
37
+ case "tool_delegated":
38
+ return "delegated";
39
+ default:
40
+ return "progress";
41
+ }
42
+ }
43
+ function projectAgentEvent(current, event) {
44
+ const streamSeq = asNumber(event.stream_seq);
45
+ if (streamSeq !== null && streamSeq <= current.lastTransportSeq) return current;
46
+ const data = asRecord(event.data);
47
+ const next = {
48
+ ...current,
49
+ status: current.status === "pending" ? "streaming" : current.status,
50
+ lastTransportSeq: streamSeq ?? current.lastTransportSeq,
51
+ eventCount: current.eventCount + 1
52
+ };
53
+ switch (event.event) {
54
+ case "chunk": {
55
+ const text = asString(data.text);
56
+ return text === null ? next : { ...next, answer: current.answer + text };
57
+ }
58
+ case "reasoning_chunk": {
59
+ const text = asString(data.text);
60
+ return text === null ? next : {
61
+ ...next,
62
+ reasoning: current.reasoning + text,
63
+ reasoningActive: true
64
+ };
65
+ }
66
+ case "reasoning":
67
+ return {
68
+ ...next,
69
+ reasoningActive: data.state === "started"
70
+ };
71
+ case "phase": {
72
+ const phase = asString(data.phase);
73
+ if (phase === null) return next;
74
+ return {
75
+ ...next,
76
+ phase,
77
+ phaseHistory: [...current.phaseHistory, phase]
78
+ };
79
+ }
80
+ case "init": {
81
+ const operationId = asString(data.operation_id);
82
+ const operation = asString(data.operation);
83
+ if (operationId === null || operation === null) return next;
84
+ return {
85
+ ...next,
86
+ operations: {
87
+ ...current.operations,
88
+ [operationId]: {
89
+ operationId,
90
+ operation,
91
+ parentOperationId: asString(data.parent_operation_id),
92
+ status: "active",
93
+ metadata: Object.keys(asRecord(data.metadata)).length ? asRecord(data.metadata) : null,
94
+ result: null
95
+ }
96
+ }
97
+ };
98
+ }
99
+ case "completion": {
100
+ const operationId = asString(data.operation_id);
101
+ const operation = asString(data.operation);
102
+ const rawStatus = asString(data.status);
103
+ const status = rawStatus === "failed" || rawStatus === "cancelled" ? rawStatus : "success";
104
+ const result = asRecord(data.result);
105
+ const operations = operationId ? {
106
+ ...current.operations,
107
+ [operationId]: {
108
+ ...current.operations[operationId] ?? {
109
+ operationId,
110
+ operation: operation ?? "unknown",
111
+ parentOperationId: null,
112
+ metadata: null
113
+ },
114
+ status,
115
+ result
116
+ }
117
+ } : current.operations;
118
+ if (operation === "user_request") {
119
+ return {
120
+ ...next,
121
+ operations,
122
+ completion: data,
123
+ status: status === "success" ? "complete" : status === "cancelled" ? "cancelled" : "error"
124
+ };
125
+ }
126
+ return { ...next, operations };
127
+ }
128
+ case "tool_event": {
129
+ const callId = asString(data.call_id);
130
+ const toolName = asString(data.tool_name);
131
+ const lifecycle = asString(data.event);
132
+ if (callId === null || toolName === null || lifecycle === null) return next;
133
+ const status = toolStatus(lifecycle);
134
+ return {
135
+ ...next,
136
+ status: status === "started" || status === "delegated" ? "awaiting-tools" : status === "completed" || status === "error" ? "streaming" : next.status,
137
+ tools: {
138
+ ...current.tools,
139
+ [callId]: {
140
+ callId,
141
+ toolName,
142
+ status,
143
+ message: asString(data.message),
144
+ data: Object.keys(asRecord(data.data)).length ? asRecord(data.data) : null
145
+ }
146
+ }
147
+ };
148
+ }
149
+ case "render_block": {
150
+ const blockId = asString(data.blockId);
151
+ const blockIndex = asNumber(data.blockIndex);
152
+ const type = asString(data.type);
153
+ if (blockId === null || blockIndex === null || type === null) return next;
154
+ const alreadyKnown = Object.hasOwn(current.renderBlocks, blockId);
155
+ return {
156
+ ...next,
157
+ renderBlocks: {
158
+ ...current.renderBlocks,
159
+ [blockId]: {
160
+ blockId,
161
+ blockIndex,
162
+ type,
163
+ status: data.status === "complete" || data.status === "error" ? data.status : "streaming",
164
+ content: asString(data.content),
165
+ data: Object.keys(asRecord(data.data)).length ? asRecord(data.data) : null,
166
+ metadata: Object.keys(asRecord(data.metadata)).length ? asRecord(data.metadata) : null
167
+ }
168
+ },
169
+ renderBlockOrder: alreadyKnown ? current.renderBlockOrder : [...current.renderBlockOrder, blockId]
170
+ };
171
+ }
172
+ case "error":
173
+ return { ...next, status: "error", error: data };
174
+ case "end":
175
+ return {
176
+ ...next,
177
+ status: current.status === "error" || current.status === "cancelled" ? current.status : "complete",
178
+ reasoningActive: false
179
+ };
180
+ case "data": {
181
+ const conversationId = data.type === "conversation_id" ? asString(data.conversation_id) : null;
182
+ return conversationId === null ? next : { ...next, conversationId };
183
+ }
184
+ default:
185
+ return next;
186
+ }
187
+ }
188
+ function projectAgentEvents(initial, events) {
189
+ return events.reduce(projectAgentEvent, initial);
190
+ }
191
+
192
+ export { createAgentRequestProjection, projectAgentEvent, projectAgentEvents };
193
+ //# sourceMappingURL=request.js.map
194
+ //# sourceMappingURL=request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../projection/request.ts"],"names":[],"mappings":";AAmEA,IAAM,QAAA,GAAW,CAAC,KAAA,KAChB,KAAA,KAAU,QAAQ,OAAO,KAAA,KAAU,QAAA,IAAY,CAAC,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,GAC9D,QACD,EAAC;AAEP,IAAM,WAAW,CAAC,KAAA,KAChB,OAAO,KAAA,KAAU,WAAW,KAAA,GAAQ,IAAA;AAEtC,IAAM,QAAA,GAAW,CAAC,KAAA,KAChB,OAAO,KAAA,KAAU,YAAY,MAAA,CAAO,QAAA,CAAS,KAAK,CAAA,GAAI,KAAA,GAAQ,IAAA;AAEzD,SAAS,6BAA6B,KAAA,EAGlB;AACzB,EAAA,OAAO;AAAA,IACL,WAAW,KAAA,CAAM,SAAA;AAAA,IACjB,cAAA,EAAgB,MAAM,cAAA,IAAkB,IAAA;AAAA,IACxC,MAAA,EAAQ,SAAA;AAAA,IACR,MAAA,EAAQ,EAAA;AAAA,IACR,SAAA,EAAW,EAAA;AAAA,IACX,eAAA,EAAiB,KAAA;AAAA,IACjB,KAAA,EAAO,IAAA;AAAA,IACP,cAAc,EAAC;AAAA,IACf,YAAY,EAAC;AAAA,IACb,OAAO,EAAC;AAAA,IACR,cAAc,EAAC;AAAA,IACf,kBAAkB,EAAC;AAAA,IACnB,UAAA,EAAY,IAAA;AAAA,IACZ,KAAA,EAAO,IAAA;AAAA,IACP,gBAAA,EAAkB,CAAA;AAAA,IAClB,UAAA,EAAY;AAAA,GACd;AACF;AAEA,SAAS,WAAW,KAAA,EAA8C;AAChE,EAAA,QAAQ,KAAA;AAAO,IACb,KAAK,cAAA;AACH,MAAA,OAAO,SAAA;AAAA,IACT,KAAK,WAAA;AACH,MAAA,OAAO,MAAA;AAAA,IACT,KAAK,qBAAA;AACH,MAAA,OAA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xB,EAAA,OAAO,MAAA,CAAO,MAAA,CAAO,iBAAA,EAAmB,OAAO,CAAA;AACjD","file":"request.js","sourcesContent":["export type AgentProjectionStatus =\n | \"pending\"\n | \"streaming\"\n | \"awaiting-tools\"\n | \"complete\"\n | \"error\"\n | \"cancelled\";\n\nexport interface AgentProjectionOperation {\n operationId: string;\n operation: string;\n parentOperationId: string | null;\n status: \"active\" | \"success\" | \"failed\" | \"cancelled\";\n metadata: Record<string, unknown> | null;\n result: Record<string, unknown> | null;\n}\n\nexport interface AgentProjectionTool {\n callId: string;\n toolName: string;\n status:\n | \"started\"\n | \"progress\"\n | \"step\"\n | \"preview\"\n | \"completed\"\n | \"error\"\n | \"delegated\";\n message: string | null;\n data: Record<string, unknown> | null;\n}\n\nexport interface AgentProjectionRenderBlock {\n blockId: string;\n blockIndex: number;\n type: string;\n status: \"streaming\" | \"complete\" | \"error\";\n content: string | null;\n data: Record<string, unknown> | null;\n metadata: Record<string, unknown> | null;\n}\n\nexport interface AgentRequestProjection {\n requestId: string;\n conversationId: string | null;\n status: AgentProjectionStatus;\n answer: string;\n reasoning: string;\n reasoningActive: boolean;\n phase: string | null;\n phaseHistory: string[];\n operations: Record<string, AgentProjectionOperation>;\n tools: Record<string, AgentProjectionTool>;\n renderBlocks: Record<string, AgentProjectionRenderBlock>;\n renderBlockOrder: string[];\n completion: Record<string, unknown> | null;\n error: Record<string, unknown> | null;\n lastTransportSeq: number;\n eventCount: number;\n}\n\nexport interface AgentProjectionEvent {\n event: string;\n data?: unknown;\n stream_seq?: number;\n}\n\nconst asRecord = (value: unknown): Record<string, unknown> =>\n value !== null && typeof value === \"object\" && !Array.isArray(value)\n ? (value as Record<string, unknown>)\n : {};\n\nconst asString = (value: unknown): string | null =>\n typeof value === \"string\" ? value : null;\n\nconst asNumber = (value: unknown): number | null =>\n typeof value === \"number\" && Number.isFinite(value) ? value : null;\n\nexport function createAgentRequestProjection(input: {\n requestId: string;\n conversationId?: string | null;\n}): AgentRequestProjection {\n return {\n requestId: input.requestId,\n conversationId: input.conversationId ?? null,\n status: \"pending\",\n answer: \"\",\n reasoning: \"\",\n reasoningActive: false,\n phase: null,\n phaseHistory: [],\n operations: {},\n tools: {},\n renderBlocks: {},\n renderBlockOrder: [],\n completion: null,\n error: null,\n lastTransportSeq: 0,\n eventCount: 0,\n };\n}\n\nfunction toolStatus(event: string): AgentProjectionTool[\"status\"] {\n switch (event) {\n case \"tool_started\":\n return \"started\";\n case \"tool_step\":\n return \"step\";\n case \"tool_result_preview\":\n return \"preview\";\n case \"tool_completed\":\n return \"completed\";\n case \"tool_error\":\n return \"error\";\n case \"tool_delegated\":\n return \"delegated\";\n default:\n return \"progress\";\n }\n}\n\nexport function projectAgentEvent(\n current: AgentRequestProjection,\n event: AgentProjectionEvent,\n): AgentRequestProjection {\n const streamSeq = asNumber(event.stream_seq);\n if (streamSeq !== null && streamSeq <= current.lastTransportSeq) return current;\n\n const data = asRecord(event.data);\n const next: AgentRequestProjection = {\n ...current,\n status: current.status === \"pending\" ? \"streaming\" : current.status,\n lastTransportSeq: streamSeq ?? current.lastTransportSeq,\n eventCount: current.eventCount + 1,\n };\n\n switch (event.event) {\n case \"chunk\": {\n const text = asString(data.text);\n return text === null ? next : { ...next, answer: current.answer + text };\n }\n case \"reasoning_chunk\": {\n const text = asString(data.text);\n return text === null\n ? next\n : {\n ...next,\n reasoning: current.reasoning + text,\n reasoningActive: true,\n };\n }\n case \"reasoning\":\n return {\n ...next,\n reasoningActive: data.state === \"started\",\n };\n case \"phase\": {\n const phase = asString(data.phase);\n if (phase === null) return next;\n return {\n ...next,\n phase,\n phaseHistory: [...current.phaseHistory, phase],\n };\n }\n case \"init\": {\n const operationId = asString(data.operation_id);\n const operation = asString(data.operation);\n if (operationId === null || operation === null) return next;\n return {\n ...next,\n operations: {\n ...current.operations,\n [operationId]: {\n operationId,\n operation,\n parentOperationId: asString(data.parent_operation_id),\n status: \"active\",\n metadata: Object.keys(asRecord(data.metadata)).length\n ? asRecord(data.metadata)\n : null,\n result: null,\n },\n },\n };\n }\n case \"completion\": {\n const operationId = asString(data.operation_id);\n const operation = asString(data.operation);\n const rawStatus = asString(data.status);\n const status: AgentProjectionOperation[\"status\"] =\n rawStatus === \"failed\" || rawStatus === \"cancelled\"\n ? rawStatus\n : \"success\";\n const result = asRecord(data.result);\n const operations = operationId\n ? {\n ...current.operations,\n [operationId]: {\n ...(current.operations[operationId] ?? {\n operationId,\n operation: operation ?? \"unknown\",\n parentOperationId: null,\n metadata: null,\n }),\n status,\n result,\n },\n }\n : current.operations;\n if (operation === \"user_request\") {\n return {\n ...next,\n operations,\n completion: data,\n status:\n status === \"success\"\n ? \"complete\"\n : status === \"cancelled\"\n ? \"cancelled\"\n : \"error\",\n };\n }\n return { ...next, operations };\n }\n case \"tool_event\": {\n const callId = asString(data.call_id);\n const toolName = asString(data.tool_name);\n const lifecycle = asString(data.event);\n if (callId === null || toolName === null || lifecycle === null) return next;\n const status = toolStatus(lifecycle);\n return {\n ...next,\n status:\n status === \"started\" || status === \"delegated\"\n ? \"awaiting-tools\"\n : status === \"completed\" || status === \"error\"\n ? \"streaming\"\n : next.status,\n tools: {\n ...current.tools,\n [callId]: {\n callId,\n toolName,\n status,\n message: asString(data.message),\n data: Object.keys(asRecord(data.data)).length ? asRecord(data.data) : null,\n },\n },\n };\n }\n case \"render_block\": {\n const blockId = asString(data.blockId);\n const blockIndex = asNumber(data.blockIndex);\n const type = asString(data.type);\n if (blockId === null || blockIndex === null || type === null) return next;\n const alreadyKnown = Object.hasOwn(current.renderBlocks, blockId);\n return {\n ...next,\n renderBlocks: {\n ...current.renderBlocks,\n [blockId]: {\n blockId,\n blockIndex,\n type,\n status:\n data.status === \"complete\" || data.status === \"error\"\n ? data.status\n : \"streaming\",\n content: asString(data.content),\n data: Object.keys(asRecord(data.data)).length ? asRecord(data.data) : null,\n metadata: Object.keys(asRecord(data.metadata)).length\n ? asRecord(data.metadata)\n : null,\n },\n },\n renderBlockOrder: alreadyKnown\n ? current.renderBlockOrder\n : [...current.renderBlockOrder, blockId],\n };\n }\n case \"error\":\n return { ...next, status: \"error\", error: data };\n case \"end\":\n return {\n ...next,\n status:\n current.status === \"error\" || current.status === \"cancelled\"\n ? current.status\n : \"complete\",\n reasoningActive: false,\n };\n case \"data\": {\n const conversationId =\n data.type === \"conversation_id\" ? asString(data.conversation_id) : null;\n return conversationId === null ? next : { ...next, conversationId };\n }\n default:\n return next;\n }\n}\n\nexport function projectAgentEvents(\n initial: AgentRequestProjection,\n events: readonly AgentProjectionEvent[],\n): AgentRequestProjection {\n return events.reduce(projectAgentEvent, initial);\n}\n"]}