Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ interface RawPayload {
payload: string;
}

interface SessionData {
session: { server_id: string };
}

class assistClient extends EventEmitterWebAuthnSender {
private readonly ws: WebSocket;
readonly proto: Protobuf = new Protobuf();
Expand All @@ -89,11 +93,32 @@ class assistClient extends EventEmitterWebAuthnSender {
this.ws = new WebSocket(url);
this.ws.binaryType = 'arraybuffer';

this.ws.onclose = () => {
setState(state => {
return state.map(n => ({
...n,
stdout: n.stdout || '',
status: RunActionStatus.Finished,
}));
});
};

this.ws.onmessage = event => {
const uintArray = new Uint8Array(event.data);
const msg = this.proto.decode(uintArray);

switch (msg.type) {
case MessageTypeEnum.SESSION_DATA:
const sessionData = JSON.parse(msg.payload) as SessionData;
setState(state => {
state.push({
nodeId: sessionData.session.server_id,
status: RunActionStatus.Connecting,
});
return state;
});
break;

case MessageTypeEnum.RAW:
const data = JSON.parse(msg.payload) as RawPayload;
const payload = atob(data.payload);
Expand Down Expand Up @@ -245,11 +270,14 @@ function NodeOutput(props: NodeOutputProps) {
</LoadingContainer>
)}

{props.state.stdout && (
<NodeContent>
<pre>{props.state.stdout}</pre>
</NodeContent>
)}
{props.state.stdout !== undefined &&
(props.state.stdout === '' ? (
Comment on lines +273 to +274
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if stdout is undefined we want to show <NodeContent>?

IMO it should be !props.state.stdout
It checks for undefined or empty value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this works well in practice. Unless I'm misreading the parentheses/ternaries, this is equivalent to:

if (props.state.stdout !== undefined) {
  if (props.state.stdout === '')
    return "Empty output";
  }
  return <NodeOutput>...</NodeOutput>
} else {
  // props.state.stdout === undefined, display nothing
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice the parentheses 🤦‍♂️
Sorry for the noise.

'Empty output.'
) : (
<NodeContent>
<pre>{props.state.stdout}</pre>
</NodeContent>
))}
</NodeContainer>
);
}
2 changes: 2 additions & 0 deletions web/packages/teleport/src/Assist/Chat/ChatItem/ChatItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ export function ChatItem(props: ChatItemProps) {
</MachineName>
{props.message.content.errorMsg ? (
<ErrorMessage>{props.message.content.errorMsg}</ErrorMessage>
) : props.message.content.payload === '' ? (
<p>Empty output.</p>
) : (
<Output>{props.message.content.payload}</Output>
)}
Expand Down
39 changes: 32 additions & 7 deletions web/packages/teleport/src/Assist/contexts/messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import cfg from 'teleport/config';

import { ApiError } from 'teleport/services/api/parseError';

import { EventType } from 'teleport/lib/term/enums';

import {
Author,
ExecuteRemoteCommandContent,
Expand Down Expand Up @@ -84,6 +86,12 @@ interface PartialMessagePayload {
idx: number;
}

interface ExecEvent {
event: EventType.EXEC;
exitError?: string;
}
type SessionEvent = ExecEvent | { event: string };

const convertToQuery = (cmd: ExecuteRemoteCommandPayload): string => {
let query = '';

Expand Down Expand Up @@ -161,6 +169,10 @@ export const remoteCommandToMessage = async (
}
};

function isExecEvent(e: SessionEvent): e is ExecEvent {
return e.event == EventType.EXEC;
}

async function convertServerMessage(
message: ServerMessage,
clusterId: string
Expand Down Expand Up @@ -221,16 +233,29 @@ async function convertServerMessage(
sid: payload.session_id,
});

// The offset here is set base on A/B test that was run between me, myself and I.
const resp = await api.fetch(sessionUrl + '/stream?offset=0&bytes=4096', {
Accept: 'text/plain',
'Content-Type': 'text/plain; charset=utf-8',
});
const eventsResp = await api.fetch(sessionUrl + '/events');
const sessionExists = eventsResp.status === 200;
const eventsData = (await eventsResp.json()) as {
events: SessionEvent[];
};
const execEvent = eventsData.events.find(isExecEvent);

let msg;
let errorMsg;
if (resp.status === 200) {
msg = await resp.text();
if (sessionExists) {
// The offset here is set base on A/B test that was run between me, myself and I.
const stream = await api.fetch(
sessionUrl + '/stream?offset=0&bytes=4096',
{
Accept: 'text/plain',
'Content-Type': 'text/plain; charset=utf-8',
}
);
if (stream.status === 200) {
msg = await stream.text();
} else {
msg = execEvent?.exitError || ''; // empty output handled in <Output>
}
} else {
errorMsg = 'No session recording. The command execution failed.';
}
Expand Down
1 change: 1 addition & 0 deletions web/packages/teleport/src/lib/term/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum EventType {
START = 'session.start',
JOIN = 'session.join',
END = 'session.end',
EXEC = 'exec',
PRINT = 'print',
RESIZE = 'resize',
FILE_TRANSFER_REQUEST = 'file_transfer_request',
Expand Down