@artinet/sdk
Version:
A TypeScript SDK for building collaborative AI agents.
107 lines (106 loc) • 3.33 kB
JavaScript
/**
* Copyright 2025 The Artinet Project
* SPDX-License-Identifier: Apache-2.0
*/
import { describe } from "./index.js";
import { TaskStatus, } from "./task-builder.js";
export function hasCarry(ret) {
return (ret !== null && typeof ret === "object" && "reply" in ret && "args" in ret);
}
export function hasReply(ret) {
return ret !== null && typeof ret === "object" && "reply" in ret;
}
function messagePart(params) {
switch (params.kind) {
case "text": {
return describe.part.text(params.part);
}
case "file": {
return describe.part.file(params.part);
}
case "data": {
return describe.part.data(params.part);
}
default:
throw new Error("Invalid part detected", { cause: params });
}
}
export function Parts(kind) {
return async function* (payload, { taskId, contextId }) {
const parts = (Array.isArray(payload) ? payload : [payload]).map((part) => messagePart({ kind, part }));
yield describe.update.working({
taskId,
contextId,
message: describe.message({ role: "agent", parts }),
});
return;
};
}
export function Message() {
return async function* (payload, { taskId, contextId }) {
if (Array.isArray(payload)) {
throw new Error("Array of messages is not supported");
}
const message = typeof payload === "string" ? describe.message(payload) : payload;
yield describe.message({
...message,
taskId,
contextId,
});
return;
};
}
export function Artifact() {
return async function* (payload, { taskId, contextId }) {
const updates = Array.isArray(payload) ? payload : [payload];
for (const update of updates) {
const artifact = typeof update === "string" ? describe.artifact(update) : update;
yield describe.update.artifact({
...artifact,
taskId,
contextId,
});
}
return;
};
}
export function Status() {
return async function* (payload, { taskId, contextId }) {
const updates = Array.isArray(payload) ? payload : [payload];
for (const update of updates) {
const status = typeof update === "string" ? TaskStatus.create(update) : update;
yield describe.update.status({
...status,
taskId,
contextId,
});
}
return;
};
}
export function Task() {
return async function* (payload, { taskId, contextId }) {
if (Array.isArray(payload)) {
throw new Error("Array of tasks is not supported");
}
const task = typeof payload === "string" ? describe.task(payload) : payload;
yield describe.task({ ...task, taskId, contextId });
return;
};
}
export async function* Reply(ret, context, transform) {
if (Array.isArray(ret)) {
yield* transform(ret, context);
}
else if (hasCarry(ret)) {
yield* transform(ret.reply, context);
return ret.args;
}
else if (hasReply(ret)) {
yield* transform(ret.reply, context);
}
else {
yield* transform(ret, context);
}
return;
}