mitra-interactions-sdk 1.0.60-beta.30 → 1.0.60-beta.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -297,7 +297,7 @@ await existing.loadHistory({ limit: 50 });
297
297
  | `task` | `AgentChat \| null` | Metadados do chat |
298
298
  | `isNew` | `boolean` | Se foi aberto via `{ create: true }` |
299
299
  | `status` | `AgentTaskStatus` | `opening` · `idle` · `streaming` · `cancelled` · `error` · `closed` |
300
- | `history` | `AgentMessage[]` | Histórico carregado (`{ id, sender, type, content, createdAt }`) |
300
+ | `history` | `AgentTimelineItem[]` | Linha do tempo canônica — `{ id, kind: 'user' \| 'agent', text, at }` ou `{ id, kind: 'tool', tool: AgentToolEvent, at }` (tool já desempacotada, mesmo shape do evento do streaming) |
301
301
  | `content` | `string` | Conteúdo acumulado do turno atual |
302
302
  | `queue` | `QueuedItem[]` | Mensagens enfileiradas (enviadas enquanto streamava) |
303
303
 
@@ -306,7 +306,7 @@ await existing.loadHistory({ limit: 50 });
306
306
  - `send(prompt)` → `void` — dispara um turno (WS `message`). Se já está streamando, **enfileira** (FIFO, máx 10).
307
307
  - `cancel()` → `Promise<void>` — interrompe o turno atual (WS `interrupt`, safety net de 10s).
308
308
  - `respondApproval(approved)` → `void` — responde um pedido de aprovação do agente (WS `approval_response`).
309
- - `loadHistory({ limit? })` → `Promise<AgentMessage[]>` — histórico persistido via REST.
309
+ - `loadHistory({ limit? })` → `Promise<AgentTimelineItem[]>` — histórico persistido via REST, já no formato canônico da timeline (renderizou o streaming, renderizou o histórico).
310
310
  - `editQueueItem(id, text)` / `removeQueueItem(id)` / `clearQueue()` — manipulam a fila.
311
311
  - `on(event, handler)` → função de unsubscribe — assina eventos da session.
312
312
  - `close()` — encerra a session, fecha o WS e libera os listeners.
package/dist/index.d.mts CHANGED
@@ -211,7 +211,7 @@ interface AgentChat {
211
211
  createdAt: string;
212
212
  updatedAt: string;
213
213
  }
214
- /** Mensagem persistida (MessageResponse do copilot). */
214
+ /** Mensagem persistida (MessageResponse do copilot), como chega do BFF. */
215
215
  interface AgentMessage {
216
216
  id: string;
217
217
  sender: 'USER' | 'AGENT' | (string & {});
@@ -219,6 +219,23 @@ interface AgentMessage {
219
219
  content: string;
220
220
  createdAt: string;
221
221
  }
222
+ /**
223
+ * Item da linha do tempo do chat — o formato canônico, o MESMO que o streaming produz:
224
+ * um item de ferramenta carrega exatamente o payload do evento `tool`. O histórico sai
225
+ * da SDK já neste formato, então a UI desenha os dois com o mesmo código e não existe
226
+ * tradução para o app errar. Renderizou o streaming, renderizou o histórico.
227
+ */
228
+ type AgentTimelineItem = {
229
+ id: string;
230
+ kind: 'user' | 'agent';
231
+ text: string;
232
+ at: string;
233
+ } | {
234
+ id: string;
235
+ kind: 'tool';
236
+ tool: AgentToolEvent;
237
+ at: string;
238
+ };
222
239
  type ChatManageAction = 'list' | 'rename' | 'delete';
223
240
  interface ManageAgentChatListOptions {
224
241
  action: 'list';
@@ -345,7 +362,7 @@ interface AgentRawEvent {
345
362
  interface AgentTaskEventMap {
346
363
  statusChange: AgentStatusChangeEvent;
347
364
  historyLoaded: {
348
- history: AgentMessage[];
365
+ history: AgentTimelineItem[];
349
366
  };
350
367
  taskCreated: AgentTaskCreatedEvent;
351
368
  turnStart: Record<string, never>;
@@ -363,7 +380,7 @@ interface AgentTaskSession {
363
380
  readonly task: AgentChat | null;
364
381
  readonly isNew: boolean;
365
382
  readonly status: AgentTaskStatus;
366
- readonly history: AgentMessage[];
383
+ readonly history: AgentTimelineItem[];
367
384
  readonly content: string;
368
385
  readonly queue: QueuedItem[];
369
386
  /** Dispara um turno (fire-and-forget; resultado via eventos). Se streaming, enfileira. */
@@ -375,7 +392,7 @@ interface AgentTaskSession {
375
392
  /** Carrega o histórico persistido (REST GET messages). */
376
393
  loadHistory(options?: {
377
394
  limit?: number;
378
- }): Promise<AgentMessage[]>;
395
+ }): Promise<AgentTimelineItem[]>;
379
396
  editQueueItem(id: string, text: string): void;
380
397
  removeQueueItem(id: string): void;
381
398
  clearQueue(): void;
@@ -719,4 +736,4 @@ declare function patchRecordMitra(options: PatchRecordOptions): Promise<Record<s
719
736
  declare function deleteRecordMitra(options: DeleteRecordOptions): Promise<void>;
720
737
  declare function createRecordsBatchMitra(options: CreateRecordsBatchOptions): Promise<Record<string, any>[]>;
721
738
 
722
- export { type AgentChat, type AgentCredentialStatus, type AgentDeltaEvent, type AgentErrorEvent, type AgentMessage, type AgentModel, type AgentProvider, type AgentQueueChangeEvent, type AgentRawEvent, type AgentStatusChangeEvent, type AgentTaskCreatedEvent, type AgentTaskEventMap, type AgentTaskEventName, type AgentTaskSession, type AgentTaskStatus, type AgentTaskTransport, type AgentToolEvent, type AgentTurnEndEvent, type AgentType, type AuthAgentCredentialResult, type CallIntegrationOptions, type CallIntegrationResponse, type ChatManageAction, type ConnectAgentCredentialResult, type CreateRecordOptions, type CreateRecordsBatchOptions, type CredentialAction, type DeleteAgentChatResult, type DeleteRecordOptions, type DeviceAuthAgentCredentialResult, type ExecutePublicServerFunctionAsyncResponse, type ExecutePublicServerFunctionOptions, type ExecutePublicServerFunctionResponse, type ExecuteServerFunctionAsyncOptions, type ExecuteServerFunctionAsyncResponse, type ExecuteServerFunctionOptions, type ExecuteServerFunctionResponse, type GetAgentTaskCreateOptions, type GetAgentTaskOpenOptions, type GetAgentTaskOptions, type GetPublicServerFunctionExecutionOptions, type GetPublicServerFunctionExecutionResponse, type GetRecordOptions, type IntegrationResponse, type ListAgentModelsResult, type ListAgentProvidersResult, type ListIntegrationsOptions, type ListRecordsOptions, type ListRecordsResponse, type LoginOptions, type LoginResponse, type ManageAgentChatDeleteOptions, type ManageAgentChatListOptions, type ManageAgentChatOptions, type ManageAgentChatRenameOptions, type ManageAgentCredentialOptions, type MitraConfig, type MitraInstance, type PatchRecordOptions, type QueuedItem, type RenameAgentChatResult, type SendOptions, type StopServerFunctionExecutionOptions, type StopServerFunctionExecutionResponse, type UpdateRecordOptions, callIntegrationMitra, configureSdkMitra, createMitraInstance, createRecordMitra, createRecordsBatchMitra, deleteRecordMitra, exchangeSsoCodeMitra, executePublicServerFunctionAsyncMitra, executePublicServerFunctionMitra, executeServerFunctionAsyncMitra, executeServerFunctionMitra, getAgentTaskMitra, getConfig, getPublicServerFunctionExecutionMitra, getRecordMitra, listIntegrationsMitra, listRecordsMitra, loginMitra, loginWithGoogleMitra, loginWithMicrosoftMitra, manageAgentChatMitra, manageAgentCredentialMitra, patchRecordMitra, refreshTokenSilently, resolveProjectId, stopServerFunctionExecutionMitra, updateRecordMitra };
739
+ export { type AgentChat, type AgentCredentialStatus, type AgentDeltaEvent, type AgentErrorEvent, type AgentMessage, type AgentModel, type AgentProvider, type AgentQueueChangeEvent, type AgentRawEvent, type AgentStatusChangeEvent, type AgentTaskCreatedEvent, type AgentTaskEventMap, type AgentTaskEventName, type AgentTaskSession, type AgentTaskStatus, type AgentTaskTransport, type AgentTimelineItem, type AgentToolEvent, type AgentTurnEndEvent, type AgentType, type AuthAgentCredentialResult, type CallIntegrationOptions, type CallIntegrationResponse, type ChatManageAction, type ConnectAgentCredentialResult, type CreateRecordOptions, type CreateRecordsBatchOptions, type CredentialAction, type DeleteAgentChatResult, type DeleteRecordOptions, type DeviceAuthAgentCredentialResult, type ExecutePublicServerFunctionAsyncResponse, type ExecutePublicServerFunctionOptions, type ExecutePublicServerFunctionResponse, type ExecuteServerFunctionAsyncOptions, type ExecuteServerFunctionAsyncResponse, type ExecuteServerFunctionOptions, type ExecuteServerFunctionResponse, type GetAgentTaskCreateOptions, type GetAgentTaskOpenOptions, type GetAgentTaskOptions, type GetPublicServerFunctionExecutionOptions, type GetPublicServerFunctionExecutionResponse, type GetRecordOptions, type IntegrationResponse, type ListAgentModelsResult, type ListAgentProvidersResult, type ListIntegrationsOptions, type ListRecordsOptions, type ListRecordsResponse, type LoginOptions, type LoginResponse, type ManageAgentChatDeleteOptions, type ManageAgentChatListOptions, type ManageAgentChatOptions, type ManageAgentChatRenameOptions, type ManageAgentCredentialOptions, type MitraConfig, type MitraInstance, type PatchRecordOptions, type QueuedItem, type RenameAgentChatResult, type SendOptions, type StopServerFunctionExecutionOptions, type StopServerFunctionExecutionResponse, type UpdateRecordOptions, callIntegrationMitra, configureSdkMitra, createMitraInstance, createRecordMitra, createRecordsBatchMitra, deleteRecordMitra, exchangeSsoCodeMitra, executePublicServerFunctionAsyncMitra, executePublicServerFunctionMitra, executeServerFunctionAsyncMitra, executeServerFunctionMitra, getAgentTaskMitra, getConfig, getPublicServerFunctionExecutionMitra, getRecordMitra, listIntegrationsMitra, listRecordsMitra, loginMitra, loginWithGoogleMitra, loginWithMicrosoftMitra, manageAgentChatMitra, manageAgentCredentialMitra, patchRecordMitra, refreshTokenSilently, resolveProjectId, stopServerFunctionExecutionMitra, updateRecordMitra };
package/dist/index.d.ts CHANGED
@@ -211,7 +211,7 @@ interface AgentChat {
211
211
  createdAt: string;
212
212
  updatedAt: string;
213
213
  }
214
- /** Mensagem persistida (MessageResponse do copilot). */
214
+ /** Mensagem persistida (MessageResponse do copilot), como chega do BFF. */
215
215
  interface AgentMessage {
216
216
  id: string;
217
217
  sender: 'USER' | 'AGENT' | (string & {});
@@ -219,6 +219,23 @@ interface AgentMessage {
219
219
  content: string;
220
220
  createdAt: string;
221
221
  }
222
+ /**
223
+ * Item da linha do tempo do chat — o formato canônico, o MESMO que o streaming produz:
224
+ * um item de ferramenta carrega exatamente o payload do evento `tool`. O histórico sai
225
+ * da SDK já neste formato, então a UI desenha os dois com o mesmo código e não existe
226
+ * tradução para o app errar. Renderizou o streaming, renderizou o histórico.
227
+ */
228
+ type AgentTimelineItem = {
229
+ id: string;
230
+ kind: 'user' | 'agent';
231
+ text: string;
232
+ at: string;
233
+ } | {
234
+ id: string;
235
+ kind: 'tool';
236
+ tool: AgentToolEvent;
237
+ at: string;
238
+ };
222
239
  type ChatManageAction = 'list' | 'rename' | 'delete';
223
240
  interface ManageAgentChatListOptions {
224
241
  action: 'list';
@@ -345,7 +362,7 @@ interface AgentRawEvent {
345
362
  interface AgentTaskEventMap {
346
363
  statusChange: AgentStatusChangeEvent;
347
364
  historyLoaded: {
348
- history: AgentMessage[];
365
+ history: AgentTimelineItem[];
349
366
  };
350
367
  taskCreated: AgentTaskCreatedEvent;
351
368
  turnStart: Record<string, never>;
@@ -363,7 +380,7 @@ interface AgentTaskSession {
363
380
  readonly task: AgentChat | null;
364
381
  readonly isNew: boolean;
365
382
  readonly status: AgentTaskStatus;
366
- readonly history: AgentMessage[];
383
+ readonly history: AgentTimelineItem[];
367
384
  readonly content: string;
368
385
  readonly queue: QueuedItem[];
369
386
  /** Dispara um turno (fire-and-forget; resultado via eventos). Se streaming, enfileira. */
@@ -375,7 +392,7 @@ interface AgentTaskSession {
375
392
  /** Carrega o histórico persistido (REST GET messages). */
376
393
  loadHistory(options?: {
377
394
  limit?: number;
378
- }): Promise<AgentMessage[]>;
395
+ }): Promise<AgentTimelineItem[]>;
379
396
  editQueueItem(id: string, text: string): void;
380
397
  removeQueueItem(id: string): void;
381
398
  clearQueue(): void;
@@ -719,4 +736,4 @@ declare function patchRecordMitra(options: PatchRecordOptions): Promise<Record<s
719
736
  declare function deleteRecordMitra(options: DeleteRecordOptions): Promise<void>;
720
737
  declare function createRecordsBatchMitra(options: CreateRecordsBatchOptions): Promise<Record<string, any>[]>;
721
738
 
722
- export { type AgentChat, type AgentCredentialStatus, type AgentDeltaEvent, type AgentErrorEvent, type AgentMessage, type AgentModel, type AgentProvider, type AgentQueueChangeEvent, type AgentRawEvent, type AgentStatusChangeEvent, type AgentTaskCreatedEvent, type AgentTaskEventMap, type AgentTaskEventName, type AgentTaskSession, type AgentTaskStatus, type AgentTaskTransport, type AgentToolEvent, type AgentTurnEndEvent, type AgentType, type AuthAgentCredentialResult, type CallIntegrationOptions, type CallIntegrationResponse, type ChatManageAction, type ConnectAgentCredentialResult, type CreateRecordOptions, type CreateRecordsBatchOptions, type CredentialAction, type DeleteAgentChatResult, type DeleteRecordOptions, type DeviceAuthAgentCredentialResult, type ExecutePublicServerFunctionAsyncResponse, type ExecutePublicServerFunctionOptions, type ExecutePublicServerFunctionResponse, type ExecuteServerFunctionAsyncOptions, type ExecuteServerFunctionAsyncResponse, type ExecuteServerFunctionOptions, type ExecuteServerFunctionResponse, type GetAgentTaskCreateOptions, type GetAgentTaskOpenOptions, type GetAgentTaskOptions, type GetPublicServerFunctionExecutionOptions, type GetPublicServerFunctionExecutionResponse, type GetRecordOptions, type IntegrationResponse, type ListAgentModelsResult, type ListAgentProvidersResult, type ListIntegrationsOptions, type ListRecordsOptions, type ListRecordsResponse, type LoginOptions, type LoginResponse, type ManageAgentChatDeleteOptions, type ManageAgentChatListOptions, type ManageAgentChatOptions, type ManageAgentChatRenameOptions, type ManageAgentCredentialOptions, type MitraConfig, type MitraInstance, type PatchRecordOptions, type QueuedItem, type RenameAgentChatResult, type SendOptions, type StopServerFunctionExecutionOptions, type StopServerFunctionExecutionResponse, type UpdateRecordOptions, callIntegrationMitra, configureSdkMitra, createMitraInstance, createRecordMitra, createRecordsBatchMitra, deleteRecordMitra, exchangeSsoCodeMitra, executePublicServerFunctionAsyncMitra, executePublicServerFunctionMitra, executeServerFunctionAsyncMitra, executeServerFunctionMitra, getAgentTaskMitra, getConfig, getPublicServerFunctionExecutionMitra, getRecordMitra, listIntegrationsMitra, listRecordsMitra, loginMitra, loginWithGoogleMitra, loginWithMicrosoftMitra, manageAgentChatMitra, manageAgentCredentialMitra, patchRecordMitra, refreshTokenSilently, resolveProjectId, stopServerFunctionExecutionMitra, updateRecordMitra };
739
+ export { type AgentChat, type AgentCredentialStatus, type AgentDeltaEvent, type AgentErrorEvent, type AgentMessage, type AgentModel, type AgentProvider, type AgentQueueChangeEvent, type AgentRawEvent, type AgentStatusChangeEvent, type AgentTaskCreatedEvent, type AgentTaskEventMap, type AgentTaskEventName, type AgentTaskSession, type AgentTaskStatus, type AgentTaskTransport, type AgentTimelineItem, type AgentToolEvent, type AgentTurnEndEvent, type AgentType, type AuthAgentCredentialResult, type CallIntegrationOptions, type CallIntegrationResponse, type ChatManageAction, type ConnectAgentCredentialResult, type CreateRecordOptions, type CreateRecordsBatchOptions, type CredentialAction, type DeleteAgentChatResult, type DeleteRecordOptions, type DeviceAuthAgentCredentialResult, type ExecutePublicServerFunctionAsyncResponse, type ExecutePublicServerFunctionOptions, type ExecutePublicServerFunctionResponse, type ExecuteServerFunctionAsyncOptions, type ExecuteServerFunctionAsyncResponse, type ExecuteServerFunctionOptions, type ExecuteServerFunctionResponse, type GetAgentTaskCreateOptions, type GetAgentTaskOpenOptions, type GetAgentTaskOptions, type GetPublicServerFunctionExecutionOptions, type GetPublicServerFunctionExecutionResponse, type GetRecordOptions, type IntegrationResponse, type ListAgentModelsResult, type ListAgentProvidersResult, type ListIntegrationsOptions, type ListRecordsOptions, type ListRecordsResponse, type LoginOptions, type LoginResponse, type ManageAgentChatDeleteOptions, type ManageAgentChatListOptions, type ManageAgentChatOptions, type ManageAgentChatRenameOptions, type ManageAgentCredentialOptions, type MitraConfig, type MitraInstance, type PatchRecordOptions, type QueuedItem, type RenameAgentChatResult, type SendOptions, type StopServerFunctionExecutionOptions, type StopServerFunctionExecutionResponse, type UpdateRecordOptions, callIntegrationMitra, configureSdkMitra, createMitraInstance, createRecordMitra, createRecordsBatchMitra, deleteRecordMitra, exchangeSsoCodeMitra, executePublicServerFunctionAsyncMitra, executePublicServerFunctionMitra, executeServerFunctionAsyncMitra, executeServerFunctionMitra, getAgentTaskMitra, getConfig, getPublicServerFunctionExecutionMitra, getRecordMitra, listIntegrationsMitra, listRecordsMitra, loginMitra, loginWithGoogleMitra, loginWithMicrosoftMitra, manageAgentChatMitra, manageAgentCredentialMitra, patchRecordMitra, refreshTokenSilently, resolveProjectId, stopServerFunctionExecutionMitra, updateRecordMitra };
package/dist/index.js CHANGED
@@ -592,23 +592,10 @@ var AgentTaskSession = class {
592
592
  ws.send(JSON.stringify({ type: "approval_response", approved }));
593
593
  }
594
594
  async loadHistory(options) {
595
- var _a, _b;
596
595
  if (!this._taskId) return [];
597
- this._history = await listChatMessages(this._taskId, this._projectId, { size: options == null ? void 0 : options.limit });
596
+ const messages = await listChatMessages(this._taskId, this._projectId, { size: options == null ? void 0 : options.limit });
597
+ this._history = messages.map((message) => toTimelineItem(message));
598
598
  this._emit("historyLoaded", { history: this._history });
599
- for (const message of this._history) {
600
- if (message.type !== "TOOL_USE") continue;
601
- const payload = parseObject(message.content);
602
- if (!payload) continue;
603
- this._emit("tool", {
604
- tool: (_a = payload.name) != null ? _a : "",
605
- toolId: payload.toolId,
606
- input: payload.input,
607
- content: (_b = payload.content) != null ? _b : payload.output,
608
- phase: "output" in payload ? "result" : "call",
609
- timestamp: Date.parse(message.createdAt) || Date.now()
610
- });
611
- }
612
599
  return this._history;
613
600
  }
614
601
  close() {
@@ -838,13 +825,33 @@ function errMsg(err) {
838
825
  if (err && typeof err === "object" && "message" in err) return String(err.message);
839
826
  return String(err);
840
827
  }
841
- function parseObject(raw) {
828
+ function toTimelineItem(message) {
829
+ const tool = message.type === "TOOL_USE" ? toToolEvent(message) : null;
830
+ if (tool) return { id: message.id, kind: "tool", tool, at: message.createdAt };
831
+ return {
832
+ id: message.id,
833
+ kind: message.sender === "USER" ? "user" : "agent",
834
+ text: message.content,
835
+ at: message.createdAt
836
+ };
837
+ }
838
+ function toToolEvent(message) {
839
+ var _a, _b;
840
+ let payload;
842
841
  try {
843
- const parsed = JSON.parse(raw);
844
- return parsed && typeof parsed === "object" ? parsed : null;
842
+ payload = JSON.parse(message.content);
843
+ if (!payload || typeof payload !== "object") return null;
845
844
  } catch (e) {
846
845
  return null;
847
846
  }
847
+ return {
848
+ tool: (_a = payload.name) != null ? _a : "",
849
+ toolId: payload.toolId,
850
+ input: payload.input,
851
+ content: (_b = payload.content) != null ? _b : payload.output,
852
+ phase: "output" in payload ? "result" : "call",
853
+ timestamp: Date.parse(message.createdAt) || Date.now()
854
+ };
848
855
  }
849
856
 
850
857
  // src/agent-chat.ts