Skip to content

Commit

Permalink
Revert "Upg: ApiClient use undici, refactor streamAgentAnswer a bit (#…
Browse files Browse the repository at this point in the history
…8539)"

This reverts commit 1b4a665.
  • Loading branch information
tdraier committed Nov 8, 2024
1 parent eed15e8 commit b0fc878
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
AgentActionPublicType,
AgentMessagePublicType,
ConversationPublicType,
DustAPI,
} from "@dust-tt/client";
Expand Down Expand Up @@ -123,9 +124,26 @@ export async function streamConversationToSlack(
{ adhereToRateLimit: false }
);

const streamRes = await dustAPI.streamAgentAnswerEvents({
const agentMessages = conversation.content
.map((versions) => {
const m = versions[versions.length - 1];
return m;
})
.filter((m) => {
return (
m &&
m.type === "agent_message" &&
m.parentMessageId === userMessage?.sId
);
});
if (agentMessages.length === 0) {
return new Err(new Error("Failed to retrieve agent message"));
}
const agentMessage = agentMessages[0] as AgentMessagePublicType;

const streamRes = await dustAPI.streamAgentMessageEvents({
conversation,
userMessageId: userMessage.sId,
message: agentMessage,
});

if (streamRes.isErr()) {
Expand Down
4 changes: 1 addition & 3 deletions front/package-lock.json

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

14 changes: 2 additions & 12 deletions sdks/js/package-lock.json

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

15 changes: 2 additions & 13 deletions sdks/js/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
{
"name": "@dust-tt/client",
"version": "1.0.5",
"description": "Client for Dust API",
"repository": {
"type": "git",
"url": "git+https://github.com/dust/dust-tt.git"
},
"author": "Dust (Permutation Labs SAS)",
"license": "ISC",
"bugs": {
"url": "https://github.com/dust/dust-tt/issues"
},
"homepage": "https://github.com/dust/dust-tt#readme",
"version": "1.0.0",
"author": "Dust",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"module": "dist/types.esm.js",
Expand Down Expand Up @@ -42,7 +32,6 @@
"dependencies": {
"eventsource-parser": "^1.1.1",
"moment-timezone": "^0.5.46",
"undici": "^6.20.1",
"zod": "^3.23.8"
},
"browser": {
Expand Down
25 changes: 4 additions & 21 deletions sdks/js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { z } from "zod";
import { fetch, RequestInit, Response } from "undici";

import type {
AgentActionSpecificEvent,
Expand Down Expand Up @@ -527,32 +526,16 @@ export class DustAPI {
return new Ok(r.value.message);
}

async streamAgentAnswerEvents({
async streamAgentMessageEvents({
conversation,
userMessageId,
message,
}: {
conversation: ConversationPublicType;
userMessageId: string;
message: AgentMessagePublicType;
}) {
// find the agent message with the parentMessageId equal to the user message id
const agentMessages = conversation.content
.map((versions) => {
const m = versions[versions.length - 1];
return m;
})
.filter((m) => {
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];

const res = await this.request({
method: "GET",
path: `assistant/conversations/${conversation.sId}/messages/${agentMessage.sId}/events`,
path: `assistant/conversations/${conversation.sId}/messages/${message.sId}/events`,
});

if (res.isErr()) {
Expand Down
12 changes: 8 additions & 4 deletions x/spolu/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AgentMessageType } from "@dust-tt/types";
import { assertNever } from "@dust-tt/types";
import { Command } from "commander";
import * as readlinePromises from "readline/promises";
Expand Down Expand Up @@ -118,11 +119,14 @@ conversations
throw new Error(`Failed to create conversation: ${res.error.message}`);
}

const { conversation, message } = res.value;
const agentMessage = res.value.conversation.content.at(-1);
if (!agentMessage) {
throw new Error(`No agent message received`);
}

const streamRes = await state.dustAPI().streamAgentAnswerEvents({
conversation: conversation,
userMessageId: message.sId,
const streamRes = await state.dustAPI().streamAgentMessageEvents({
conversation: res.value.conversation,
message: agentMessage[0] as AgentMessageType,
});

if (streamRes.isErr()) {
Expand Down
53 changes: 2 additions & 51 deletions x/spolu/cli/package-lock.json

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

3 changes: 1 addition & 2 deletions x/spolu/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"format:check": "prettier --check ."
},
"dependencies": {
"@dust-tt/client": "^1.0.5",
"@dust-tt/types": "file:../../../types",
"commander": "^12.1.0",
"talisman": "^1.1.4"
Expand All @@ -29,4 +28,4 @@
"prettier": "^3.0",
"typescript": "^5.6.2"
}
}
}
5 changes: 3 additions & 2 deletions x/spolu/cli/src/state.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { DustAPICredentials } from "@dust-tt/client";
import { DustAPI } from "@dust-tt/client";
import type {
ConversationWithoutContentType,
DustAPICredentials,
LightAgentConfigurationType,
} from "@dust-tt/types";
import { DustAPI } from "@dust-tt/types";
import fs from "fs/promises";
import path from "path";

export class State {
agents: LightAgentConfigurationType[] = [];
private conversations: ConversationWithoutContentType[] = [];

private credentials: DustAPICredentials | null = null;
private url: string = "https://dust.tt";
Expand Down

0 comments on commit b0fc878

Please sign in to comment.