@runtypelabs/persona 3.13.0 → 3.14.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.
@@ -4329,7 +4329,7 @@ var AgentWidgetClient = class {
4329
4329
  }
4330
4330
  }
4331
4331
  async streamResponse(body, onEvent, assistantMessageId) {
4332
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z, __, _$, _aa, _ba, _ca, _da, _ea, _fa, _ga, _ha, _ia, _ja, _ka, _la, _ma, _na, _oa, _pa, _qa, _ra, _sa, _ta, _ua, _va, _wa, _xa, _ya, _za, _Aa, _Ba, _Ca, _Da, _Ea, _Fa, _Ga, _Ha, _Ia, _Ja, _Ka, _La, _Ma, _Na, _Oa, _Pa, _Qa, _Ra, _Sa, _Ta, _Ua, _Va, _Wa, _Xa, _Ya, _Za, __a, _$a, _ab, _bb, _cb, _db, _eb, _fb, _gb, _hb, _ib, _jb, _kb, _lb;
4332
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z, __, _$, _aa, _ba, _ca, _da, _ea, _fa, _ga, _ha, _ia, _ja, _ka, _la, _ma, _na, _oa, _pa, _qa, _ra, _sa, _ta, _ua, _va, _wa, _xa, _ya, _za, _Aa, _Ba, _Ca, _Da, _Ea, _Fa, _Ga, _Ha, _Ia, _Ja, _Ka, _La, _Ma, _Na, _Oa, _Pa, _Qa, _Ra, _Sa, _Ta, _Ua, _Va, _Wa, _Xa, _Ya, _Za, __a, _$a, _ab, _bb, _cb, _db, _eb, _fb, _gb, _hb, _ib, _jb, _kb, _lb, _mb, _nb;
4333
4333
  const reader = body.getReader();
4334
4334
  const decoder = new TextDecoder();
4335
4335
  let buffer = "";
@@ -5620,11 +5620,34 @@ var AgentWidgetClient = class {
5620
5620
  streamParsers.delete(id);
5621
5621
  rawContentBuffers.delete(id);
5622
5622
  seqChunkBuffers.delete(id);
5623
- } else if (payloadType === "error" && payload.error) {
5624
- onEvent({
5625
- type: "error",
5626
- error: payload.error instanceof Error ? payload.error : new Error(String(payload.error))
5627
- });
5623
+ } else if (payloadType === "error" || payloadType === "step_error" || payloadType === "dispatch_error" || payloadType === "flow_error") {
5624
+ let resolvedError = null;
5625
+ if (payload.error instanceof Error) {
5626
+ resolvedError = payload.error;
5627
+ } else if (payloadType === "dispatch_error") {
5628
+ const msg = (_mb = payload.message) != null ? _mb : payload.error;
5629
+ if (msg != null && msg !== "") {
5630
+ resolvedError = new Error(String(msg));
5631
+ }
5632
+ } else if (payloadType === "step_error" || payloadType === "flow_error") {
5633
+ const e = payload.error;
5634
+ if (typeof e === "string" && e !== "") {
5635
+ resolvedError = new Error(e);
5636
+ } else if (e != null && typeof e === "object" && "message" in e) {
5637
+ resolvedError = new Error(String((_nb = e.message) != null ? _nb : e));
5638
+ }
5639
+ } else if (payloadType === "error" && payload.error != null && payload.error !== "") {
5640
+ resolvedError = new Error(String(payload.error));
5641
+ }
5642
+ if (resolvedError) {
5643
+ onEvent({ type: "error", error: resolvedError });
5644
+ const finalMsg = assistantMessage;
5645
+ if (finalMsg && finalMsg.streaming) {
5646
+ finalMsg.streaming = false;
5647
+ emitMessage(finalMsg);
5648
+ }
5649
+ onEvent({ type: "status", status: "idle" });
5650
+ }
5628
5651
  }
5629
5652
  }
5630
5653
  }
@@ -4220,7 +4220,7 @@ var AgentWidgetClient = class {
4220
4220
  }
4221
4221
  }
4222
4222
  async streamResponse(body, onEvent, assistantMessageId) {
4223
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z, __, _$, _aa, _ba, _ca, _da, _ea, _fa, _ga, _ha, _ia, _ja, _ka, _la, _ma, _na, _oa, _pa, _qa, _ra, _sa, _ta, _ua, _va, _wa, _xa, _ya, _za, _Aa, _Ba, _Ca, _Da, _Ea, _Fa, _Ga, _Ha, _Ia, _Ja, _Ka, _La, _Ma, _Na, _Oa, _Pa, _Qa, _Ra, _Sa, _Ta, _Ua, _Va, _Wa, _Xa, _Ya, _Za, __a, _$a, _ab, _bb, _cb, _db, _eb, _fb, _gb, _hb, _ib, _jb, _kb, _lb;
4223
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V, _W, _X, _Y, _Z, __, _$, _aa, _ba, _ca, _da, _ea, _fa, _ga, _ha, _ia, _ja, _ka, _la, _ma, _na, _oa, _pa, _qa, _ra, _sa, _ta, _ua, _va, _wa, _xa, _ya, _za, _Aa, _Ba, _Ca, _Da, _Ea, _Fa, _Ga, _Ha, _Ia, _Ja, _Ka, _La, _Ma, _Na, _Oa, _Pa, _Qa, _Ra, _Sa, _Ta, _Ua, _Va, _Wa, _Xa, _Ya, _Za, __a, _$a, _ab, _bb, _cb, _db, _eb, _fb, _gb, _hb, _ib, _jb, _kb, _lb, _mb, _nb;
4224
4224
  const reader = body.getReader();
4225
4225
  const decoder = new TextDecoder();
4226
4226
  let buffer = "";
@@ -5511,11 +5511,34 @@ var AgentWidgetClient = class {
5511
5511
  streamParsers.delete(id);
5512
5512
  rawContentBuffers.delete(id);
5513
5513
  seqChunkBuffers.delete(id);
5514
- } else if (payloadType === "error" && payload.error) {
5515
- onEvent({
5516
- type: "error",
5517
- error: payload.error instanceof Error ? payload.error : new Error(String(payload.error))
5518
- });
5514
+ } else if (payloadType === "error" || payloadType === "step_error" || payloadType === "dispatch_error" || payloadType === "flow_error") {
5515
+ let resolvedError = null;
5516
+ if (payload.error instanceof Error) {
5517
+ resolvedError = payload.error;
5518
+ } else if (payloadType === "dispatch_error") {
5519
+ const msg = (_mb = payload.message) != null ? _mb : payload.error;
5520
+ if (msg != null && msg !== "") {
5521
+ resolvedError = new Error(String(msg));
5522
+ }
5523
+ } else if (payloadType === "step_error" || payloadType === "flow_error") {
5524
+ const e = payload.error;
5525
+ if (typeof e === "string" && e !== "") {
5526
+ resolvedError = new Error(e);
5527
+ } else if (e != null && typeof e === "object" && "message" in e) {
5528
+ resolvedError = new Error(String((_nb = e.message) != null ? _nb : e));
5529
+ }
5530
+ } else if (payloadType === "error" && payload.error != null && payload.error !== "") {
5531
+ resolvedError = new Error(String(payload.error));
5532
+ }
5533
+ if (resolvedError) {
5534
+ onEvent({ type: "error", error: resolvedError });
5535
+ const finalMsg = assistantMessage;
5536
+ if (finalMsg && finalMsg.streaming) {
5537
+ finalMsg.streaming = false;
5538
+ emitMessage(finalMsg);
5539
+ }
5540
+ onEvent({ type: "status", status: "idle" });
5541
+ }
5519
5542
  }
5520
5543
  }
5521
5544
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runtypelabs/persona",
3
- "version": "3.13.0",
3
+ "version": "3.14.0",
4
4
  "description": "Themeable, pluggable streaming agent widget for websites, in plain JS with support for voice input and reasoning / tool output.",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -1121,6 +1121,70 @@ describe('AgentWidgetClient - Agent Event Streaming', () => {
1121
1121
  }
1122
1122
  });
1123
1123
 
1124
+ it('should emit error and finalize streaming on step_error', async () => {
1125
+ const events: AgentWidgetEvent[] = [];
1126
+
1127
+ global.fetch = createAgentStreamFetch([
1128
+ 'data: {"type":"flow_start","flowId":"f1","flowName":"Test","totalSteps":1}\n\n',
1129
+ 'data: {"type":"step_delta","id":"s1","name":"Prompt","executionType":"prompt","text":"partial"}\n\n',
1130
+ sseEvent('step_error', { error: 'step blew up', seq: 3 }),
1131
+ ]);
1132
+
1133
+ const client = new AgentWidgetClient({
1134
+ apiUrl: 'http://localhost:8000',
1135
+ agent: { name: 'Test', model: 'openai:gpt-4o-mini', systemPrompt: 'test' },
1136
+ });
1137
+
1138
+ await client.dispatch(
1139
+ { messages: [{ id: 'usr_1', role: 'user', content: 'Hi', createdAt: new Date().toISOString() }] },
1140
+ (event) => events.push(event)
1141
+ );
1142
+
1143
+ const errorEvents = events.filter(e => e.type === 'error');
1144
+ expect(errorEvents.length).toBe(1);
1145
+ if (errorEvents[0].type === 'error') {
1146
+ expect(errorEvents[0].error.message).toBe('step blew up');
1147
+ }
1148
+
1149
+ const statusIdle = events.filter(e => e.type === 'status' && e.status === 'idle');
1150
+ expect(statusIdle.length).toBeGreaterThanOrEqual(1);
1151
+
1152
+ const messageEvents = events.filter(e => e.type === 'message');
1153
+ const lastAssistant = [...messageEvents]
1154
+ .reverse()
1155
+ .find(e => e.type === 'message' && e.message.role === 'assistant' && !e.message.variant);
1156
+ expect(lastAssistant?.type === 'message' && lastAssistant.message.streaming).toBe(false);
1157
+ });
1158
+
1159
+ it('should emit error and finalize streaming on dispatch_error (message only)', async () => {
1160
+ const events: AgentWidgetEvent[] = [];
1161
+
1162
+ global.fetch = createAgentStreamFetch([
1163
+ 'data: {"type":"flow_start","flowId":"f1","flowName":"Test","totalSteps":1}\n\n',
1164
+ 'data: {"type":"step_delta","id":"s1","name":"Prompt","executionType":"prompt","text":"x"}\n\n',
1165
+ sseEvent('dispatch_error', { message: 'bad config', seq: 2 }),
1166
+ ]);
1167
+
1168
+ const client = new AgentWidgetClient({
1169
+ apiUrl: 'http://localhost:8000',
1170
+ agent: { name: 'Test', model: 'openai:gpt-4o-mini', systemPrompt: 'test' },
1171
+ });
1172
+
1173
+ await client.dispatch(
1174
+ { messages: [{ id: 'usr_1', role: 'user', content: 'Hi', createdAt: new Date().toISOString() }] },
1175
+ (event) => events.push(event)
1176
+ );
1177
+
1178
+ const errorEvents = events.filter(e => e.type === 'error');
1179
+ expect(errorEvents.length).toBe(1);
1180
+ if (errorEvents[0].type === 'error') {
1181
+ expect(errorEvents[0].error.message).toBe('bad config');
1182
+ }
1183
+
1184
+ const statusIdle = events.filter(e => e.type === 'status' && e.status === 'idle');
1185
+ expect(statusIdle.length).toBeGreaterThanOrEqual(1);
1186
+ });
1187
+
1124
1188
  it('should handle agent reflection events', async () => {
1125
1189
  const events: AgentWidgetEvent[] = [];
1126
1190
  const execId = 'exec_test_7';
package/src/client.ts CHANGED
@@ -2592,14 +2592,43 @@ export class AgentWidgetClient {
2592
2592
  streamParsers.delete(id);
2593
2593
  rawContentBuffers.delete(id);
2594
2594
  seqChunkBuffers.delete(id);
2595
- } else if (payloadType === "error" && payload.error) {
2596
- onEvent({
2597
- type: "error",
2598
- error:
2599
- payload.error instanceof Error
2600
- ? payload.error
2601
- : new Error(String(payload.error))
2602
- });
2595
+ } else if (
2596
+ payloadType === "error" ||
2597
+ payloadType === "step_error" ||
2598
+ payloadType === "dispatch_error" ||
2599
+ payloadType === "flow_error"
2600
+ ) {
2601
+ let resolvedError: Error | null = null;
2602
+ if (payload.error instanceof Error) {
2603
+ resolvedError = payload.error;
2604
+ } else if (payloadType === "dispatch_error") {
2605
+ const msg = payload.message ?? payload.error;
2606
+ if (msg != null && msg !== "") {
2607
+ resolvedError = new Error(String(msg));
2608
+ }
2609
+ } else if (
2610
+ payloadType === "step_error" ||
2611
+ payloadType === "flow_error"
2612
+ ) {
2613
+ const e = payload.error;
2614
+ if (typeof e === "string" && e !== "") {
2615
+ resolvedError = new Error(e);
2616
+ } else if (e != null && typeof e === "object" && "message" in e) {
2617
+ resolvedError = new Error(String((e as { message?: unknown }).message ?? e));
2618
+ }
2619
+ } else if (payloadType === "error" && payload.error != null && payload.error !== "") {
2620
+ resolvedError = new Error(String(payload.error));
2621
+ }
2622
+
2623
+ if (resolvedError) {
2624
+ onEvent({ type: "error", error: resolvedError });
2625
+ const finalMsg = assistantMessage as AgentWidgetMessage | null;
2626
+ if (finalMsg && finalMsg.streaming) {
2627
+ finalMsg.streaming = false;
2628
+ emitMessage(finalMsg);
2629
+ }
2630
+ onEvent({ type: "status", status: "idle" });
2631
+ }
2603
2632
  }
2604
2633
  }
2605
2634
  }