2026-03-03 21:32:01 +08:00
|
|
|
import type { BaseStream } from "@langchain/langgraph-sdk/react";
|
2026-01-19 19:41:46 +08:00
|
|
|
|
|
|
|
|
import type { AgentThreadState } from "../threads";
|
|
|
|
|
|
2026-01-17 15:09:44 +08:00
|
|
|
import { urlOfArtifact } from "./utils";
|
|
|
|
|
|
|
|
|
|
export async function loadArtifactContent({
|
|
|
|
|
filepath,
|
|
|
|
|
threadId,
|
2026-03-03 21:32:01 +08:00
|
|
|
isMock,
|
2026-01-17 15:09:44 +08:00
|
|
|
}: {
|
|
|
|
|
filepath: string;
|
|
|
|
|
threadId: string;
|
2026-03-03 21:32:01 +08:00
|
|
|
isMock?: boolean;
|
2026-01-17 15:09:44 +08:00
|
|
|
}) {
|
2026-01-31 11:08:27 +08:00
|
|
|
let enhancedFilepath = filepath;
|
|
|
|
|
if (filepath.endsWith(".skill")) {
|
2026-01-31 22:27:06 +08:00
|
|
|
enhancedFilepath = filepath + "/SKILL.md";
|
2026-01-31 11:08:27 +08:00
|
|
|
}
|
2026-03-03 21:32:01 +08:00
|
|
|
const url = urlOfArtifact({ filepath: enhancedFilepath, threadId, isMock });
|
2026-01-17 15:09:44 +08:00
|
|
|
const response = await fetch(url);
|
|
|
|
|
const text = await response.text();
|
|
|
|
|
return text;
|
|
|
|
|
}
|
2026-01-19 19:41:46 +08:00
|
|
|
|
|
|
|
|
export function loadArtifactContentFromToolCall({
|
|
|
|
|
url: urlString,
|
|
|
|
|
thread,
|
|
|
|
|
}: {
|
|
|
|
|
url: string;
|
2026-03-03 21:32:01 +08:00
|
|
|
thread: BaseStream<AgentThreadState>;
|
2026-01-19 19:41:46 +08:00
|
|
|
}) {
|
|
|
|
|
const url = new URL(urlString);
|
|
|
|
|
const toolCallId = url.searchParams.get("tool_call_id");
|
|
|
|
|
const messageId = url.searchParams.get("message_id");
|
|
|
|
|
if (messageId && toolCallId) {
|
|
|
|
|
const message = thread.messages.find((message) => message.id === messageId);
|
|
|
|
|
if (message?.type === "ai" && message.tool_calls) {
|
|
|
|
|
const toolCall = message.tool_calls.find(
|
|
|
|
|
(toolCall) => toolCall.id === toolCallId,
|
|
|
|
|
);
|
|
|
|
|
if (toolCall) {
|
|
|
|
|
return toolCall.args.content;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|