This commit is contained in:
Harivansh Rathi 2026-03-12 02:28:11 -04:00
parent 9115d5647f
commit e19f575229
2 changed files with 114 additions and 66 deletions

View file

@ -62,6 +62,7 @@ export type { HistoryPart } from "./types.js";
let activeGatewayRuntime: GatewayRuntime | null = null;
type JsonRecord = Record<string, unknown>;
type AssistantAgentMessage = Extract<AgentMessage, { role: "assistant" }>;
type CompanionChannelsSettings = JsonRecord & {
adapters?: Record<string, JsonRecord>;
@ -659,14 +660,15 @@ export class GatewayRuntime {
private emitStructuredParts(
managedSession: ManagedGatewaySession,
message: AgentMessage,
message: AssistantAgentMessage,
): void {
const content = message.content;
if (!Array.isArray(content)) return;
for (const part of content) {
if (typeof part !== "object" || part === null) continue;
const p = part as Record<string, unknown>;
const rawPart: unknown = part;
if (!isRecord(rawPart)) continue;
const p = rawPart;
if (p.type === "teamActivity") {
const teamId = typeof p.teamId === "string" ? p.teamId : "";
@ -722,7 +724,6 @@ export class GatewayRuntime {
message: errorMessage,
},
});
continue;
}
}
}

View file

@ -9,12 +9,47 @@ export interface GatewayTransientToolResult {
timestamp: number;
}
type TeamActivityMember = Extract<
HistoryPart,
{ type: "teamActivity" }
>["members"][number];
function isSupportedHistoryRole(
role: AgentMessage["role"],
): role is "user" | "assistant" | "toolResult" {
return role === "user" || role === "assistant" || role === "toolResult";
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null;
}
function normalizeTeamActivityMembers(value: unknown): TeamActivityMember[] {
if (!Array.isArray(value)) {
return [];
}
return value
.filter(isRecord)
.map((member) => {
const id = typeof member.id === "string" ? member.id : "";
if (!id) {
return null;
}
return {
id,
name: typeof member.name === "string" ? member.name : "Teammate",
...(typeof member.role === "string" ? { role: member.role } : {}),
status: typeof member.status === "string" ? member.status : "running",
...(typeof member.message === "string"
? { message: member.message }
: {}),
};
})
.filter((member): member is TeamActivityMember => member !== null);
}
function historyMessageId(message: AgentMessage, index: number): string {
return `${message.timestamp}-${message.role}-${index}`;
}
@ -53,74 +88,86 @@ export function messageContentToHistoryParts(msg: AgentMessage): HistoryPart[] {
}
if (msg.role === "assistant") {
const content = msg.content;
const content: unknown = msg.content;
if (!Array.isArray(content)) return [];
const parts: HistoryPart[] = [];
for (const contentPart of content) {
if (typeof contentPart !== "object" || contentPart === null) {
if (!isRecord(contentPart) || typeof contentPart.type !== "string") {
continue;
}
if (contentPart.type === "text") {
switch (contentPart.type) {
case "text":
if (typeof contentPart.text === "string") {
parts.push({
type: "text",
text: (contentPart as { type: "text"; text: string }).text,
text: contentPart.text,
});
} else if (contentPart.type === "thinking") {
}
break;
case "thinking":
if (typeof contentPart.thinking === "string") {
parts.push({
type: "reasoning",
text: (contentPart as { type: "thinking"; thinking: string })
.thinking,
text: contentPart.thinking,
});
} else if (contentPart.type === "toolCall") {
const toolCall = contentPart as {
type: "toolCall";
id: string;
name: string;
arguments: unknown;
};
}
break;
case "toolCall":
if (
typeof contentPart.id === "string" &&
typeof contentPart.name === "string"
) {
parts.push({
type: "tool-invocation",
toolCallId: toolCall.id,
toolName: toolCall.name,
args: toolCall.arguments,
toolCallId: contentPart.id,
toolName: contentPart.name,
args: contentPart.arguments,
state: "call",
});
} else if (contentPart.type === "teamActivity") {
const activity = contentPart as {
type: "teamActivity";
teamId: string;
status: string;
members?: Array<{ id: string; name: string; role?: string; status: string; message?: string }>;
};
}
break;
case "teamActivity": {
const teamId =
typeof contentPart.teamId === "string" ? contentPart.teamId : "";
if (!teamId) {
break;
}
parts.push({
type: "teamActivity",
teamId: activity.teamId,
status: activity.status,
members: Array.isArray(activity.members) ? activity.members : [],
teamId,
status:
typeof contentPart.status === "string"
? contentPart.status
: "running",
members: normalizeTeamActivityMembers(contentPart.members),
});
} else if (contentPart.type === "image") {
const image = contentPart as {
type: "image";
url: string;
mimeType?: string;
};
break;
}
case "image":
if (typeof contentPart.url === "string") {
parts.push({
type: "media",
url: image.url,
mimeType: image.mimeType,
url: contentPart.url,
...(typeof contentPart.mimeType === "string"
? { mimeType: contentPart.mimeType }
: {}),
});
} else if (contentPart.type === "error") {
const error = contentPart as {
type: "error";
code?: string;
message: string;
};
}
break;
case "error":
if (typeof contentPart.message === "string") {
parts.push({
type: "error",
code: typeof error.code === "string" ? error.code : "unknown",
message: error.message,
code:
typeof contentPart.code === "string"
? contentPart.code
: "unknown",
message: contentPart.message,
});
}
break;
}
}
return parts;
}