@artinet/sdk
Version:
TypeScript SDK for Agentic Communication
80 lines • 2.75 kB
JavaScript
import { getCurrentTimestamp } from "../../utils/common/utils.js";
import { TaskState, } from "../../types/extended-schema.js";
import { v4 as uuidv4 } from "uuid";
import { update } from "./update/base.js";
//todo: move to update.ts
export function updateByIndex(append, artifacts, index, artifactUpdate) {
if (append) {
const existingArtifact = artifacts[index];
existingArtifact.parts.push(...artifactUpdate.parts);
if (artifactUpdate.metadata) {
existingArtifact.metadata = {
...(existingArtifact.metadata || {}),
...artifactUpdate.metadata,
};
}
if (artifactUpdate.description) {
existingArtifact.description = artifactUpdate.description;
}
if (artifactUpdate.name) {
existingArtifact.name = artifactUpdate.name;
}
artifacts[index] = existingArtifact;
}
else {
artifacts[index] = { ...artifactUpdate };
}
return { artifacts, replaced: true };
}
//todo: move to update.ts
export function processArtifactUpdate(append, artifacts, artifactUpdate) {
const existingIndex = artifacts.findIndex((a) => a.artifactId === artifactUpdate.artifactId);
let replaced = false;
let newArtifacts = artifacts;
if (existingIndex !== -1) {
({ artifacts: newArtifacts, replaced } = updateByIndex(append, artifacts, existingIndex, artifactUpdate));
}
if (!replaced) {
newArtifacts.push({ ...artifactUpdate });
}
return newArtifacts;
}
export async function processUpdate(taskStore, updateProps) {
if (!(await update(updateProps))) {
throw new Error("processUpdate: Invalid update");
}
await taskStore.save(updateProps.current);
return updateProps.current;
}
/**
* Loads or creates a task and its history.
* @param taskId The task ID
* @param message The message to process
* @param contextId Optional context ID
* @param metadata Optional metadata
* @returns The task and history
*/
export async function loadState(taskStore, message, metadata, taskId, contextId) {
if (taskId) {
const existingData = await taskStore.load(taskId);
if (existingData) {
return existingData;
}
}
const timestamp = getCurrentTimestamp();
const newTask = {
id: taskId ?? uuidv4(),
contextId: contextId ?? uuidv4(),
kind: "task",
status: {
state: TaskState.Submitted,
timestamp,
},
metadata: metadata,
};
message.taskId = newTask.id;
message.contextId = newTask.contextId;
const newHistory = [message];
return { task: newTask, history: newHistory };
}
//# sourceMappingURL=state.js.map