Skip to content

Commit

Permalink
[sdk] Expect stream body when used in browser env (#9941)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdraier authored Jan 16, 2025
1 parent dd95502 commit bd2576c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions sdks/js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdks/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dust-tt/client",
"version": "1.0.23",
"version": "1.0.24",
"description": "Client for Dust API",
"repository": {
"type": "git",
Expand Down
42 changes: 30 additions & 12 deletions sdks/js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import type { AxiosRequestConfig } from "axios";
import axios from "axios";
import { createParser } from "eventsource-parser";
import http from "http";
import https from "https";
import { Readable } from "stream";
import { z } from "zod";

import type {
AgentActionSpecificEvent,
AgentActionSuccessEvent,
AgentConfigurationViewType,
AgentErrorEvent,
AgentMessagePublicType,
AgentMessageSuccessEvent,
APIError,
AppsCheckRequestType,
Expand Down Expand Up @@ -40,7 +47,6 @@ import {
DeleteFolderResponseSchema,
Err,
FileUploadRequestResponseSchema,
FileUploadUrlRequestSchema,
GetActiveMemberEmailsInWorkspaceResponseSchema,
GetAgentConfigurationsResponseSchema,
GetAppsResponseSchema,
Expand All @@ -62,21 +68,18 @@ import {

export * from "./types";

import type { AxiosRequestConfig } from "axios";
import axios from "axios";
import { createParser } from "eventsource-parser";
import http from "http";
import https from "https";
import { Readable } from "stream";

interface DustResponse {
status: number;
ok: boolean;
url: string;
body: Readable;
body: Readable | string;
}

const textFromResponse = async (response: DustResponse): Promise<string> => {
if (typeof response.body === "string") {
return response.body;
}

const stream = response.body;

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -631,16 +634,32 @@ export class DustAPI {
const m = versions[versions.length - 1];
return m;
})
.filter((m) => {
.filter((m): m is AgentMessagePublicType => {
return (
m && m.type === "agent_message" && m.parentMessageId === userMessageId
);
});
if (agentMessages.length === 0) {
return new Err(new Error("Failed to retrieve agent message"));
}

const agentMessage = agentMessages[0];
return this.streamAgentMessageEvents({
conversation,
agentMessage,
signal,
});
}

async streamAgentMessageEvents({
conversation,
agentMessage,
signal,
}: {
conversation: ConversationPublicType;
agentMessage: AgentMessagePublicType;
signal?: AbortSignal;
}) {
const res = await this.request({
method: "GET",
path: `assistant/conversations/${conversation.sId}/messages/${agentMessage.sId}/events`,
Expand Down Expand Up @@ -893,7 +912,6 @@ export class DustAPI {
useCaseMetadata,
fileObject,
}: FileUploadUrlRequestType & { fileObject: File }) {
FileUploadUrlRequestSchema;
const res = await this.request({
method: "POST",
path: "files",
Expand Down Expand Up @@ -1076,7 +1094,7 @@ export class DustAPI {
): Promise<Result<{ response: DustResponse; duration: number }, APIError>> {
const now = Date.now();
try {
const res = await axiosNoKeepAlive<Readable>(url, {
const res = await axiosNoKeepAlive<Readable | string>(url, {
validateStatus: () => true,
responseType: "stream",
...config,
Expand Down

0 comments on commit bd2576c

Please sign in to comment.