@cognipeer/agent-sdk 0.8.2 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +217 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +47 -2
- package/dist/index.d.ts +47 -2
- package/dist/index.mjs +217 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -4573,6 +4573,7 @@ function resolveUserQuestionState(state, resolution) {
|
|
|
4573
4573
|
ctx
|
|
4574
4574
|
};
|
|
4575
4575
|
}
|
|
4576
|
+
var ASK_USER_TOOL_NAME = "ask_user_question";
|
|
4576
4577
|
var DEFAULT_DESCRIPTION_WITH_FREETEXT = "Pause the agent and surface a structured prompt to the human user when you genuinely cannot proceed without their input. Group up to 4 closely related questions into a single call. Prefer concrete options the user can pick over open-ended free-text \u2014 list `options` whenever you can anticipate the choices. The user may type a custom answer for any question. Do not use this tool for things you can deduce from the conversation, the system prompt, or another tool. After the user answers, their selections come back as the tool result.";
|
|
4577
4578
|
var DEFAULT_DESCRIPTION_NO_FREETEXT = "Pause the agent and surface a structured multi-choice prompt to the human user when you genuinely cannot proceed without their input. Group up to 4 closely related questions into a single call. Every question MUST include at least two `options` \u2014 free-text answers are disabled for this agent. Do not use this tool for things you can deduce from the conversation, the system prompt, or another tool. After the user picks, their selections come back as the tool result.";
|
|
4578
4579
|
function buildOptionSchema() {
|
|
@@ -4608,7 +4609,7 @@ function createAskUserQuestionTool(stateRef, config) {
|
|
|
4608
4609
|
const description = config.promptOverride ?? (allowFreeText ? DEFAULT_DESCRIPTION_WITH_FREETEXT : DEFAULT_DESCRIPTION_NO_FREETEXT);
|
|
4609
4610
|
const questionSchema = buildQuestionSchema(allowFreeText);
|
|
4610
4611
|
const askUser = createTool({
|
|
4611
|
-
name:
|
|
4612
|
+
name: ASK_USER_TOOL_NAME,
|
|
4612
4613
|
description,
|
|
4613
4614
|
schema: zod.z.object({
|
|
4614
4615
|
questions: zod.z.array(questionSchema).min(1).max(4).describe("1-4 related questions to ask in a single pause.")
|
|
@@ -4624,7 +4625,7 @@ function createAskUserQuestionTool(stateRef, config) {
|
|
|
4624
4625
|
const entry = {
|
|
4625
4626
|
id: nanoid.nanoid(),
|
|
4626
4627
|
toolCallId,
|
|
4627
|
-
toolName:
|
|
4628
|
+
toolName: ASK_USER_TOOL_NAME,
|
|
4628
4629
|
questions,
|
|
4629
4630
|
status: "pending",
|
|
4630
4631
|
requestedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
@@ -4980,14 +4981,15 @@ function createAgent(opts) {
|
|
|
4980
4981
|
})();
|
|
4981
4982
|
const askUserOnQuestion = typeof opts.humanInTheLoop?.askUser === "object" ? opts.humanInTheLoop?.askUser?.onQuestion : void 0;
|
|
4982
4983
|
const askUserStateRef = askUserConfig ? { pendingUserQuestions: void 0, ctx: void 0, __onEvent: void 0, __currentToolCallId: void 0 } : void 0;
|
|
4983
|
-
|
|
4984
|
+
const hasTool = (name) => toolsBase.some((tool) => tool?.name === name);
|
|
4985
|
+
if (askUserConfig && askUserStateRef && !hasTool(ASK_USER_TOOL_NAME)) {
|
|
4984
4986
|
toolsBase.push(createAskUserQuestionTool(askUserStateRef, askUserConfig));
|
|
4985
4987
|
}
|
|
4986
4988
|
const soManager = opts.outputSchema ? new StructuredOutputManager(opts.outputSchema, resolveStrategy(opts.model)) : void 0;
|
|
4987
4989
|
if (soManager) {
|
|
4988
4990
|
const modelCapabilities = getModelCapabilities(opts.model);
|
|
4989
4991
|
const responseTool = soManager.getResponseTool();
|
|
4990
|
-
if (responseTool && modelCapabilities.structuredOutput !== "native") {
|
|
4992
|
+
if (responseTool && modelCapabilities.structuredOutput !== "native" && !hasTool(responseTool.name)) {
|
|
4991
4993
|
toolsBase.push(responseTool);
|
|
4992
4994
|
}
|
|
4993
4995
|
}
|
|
@@ -8600,6 +8602,78 @@ function resolveBudget(reasoning, effortBudget) {
|
|
|
8600
8602
|
return void 0;
|
|
8601
8603
|
}
|
|
8602
8604
|
|
|
8605
|
+
// src/providers/utils/media.ts
|
|
8606
|
+
var EXTENSION_MIME = {
|
|
8607
|
+
jpg: "image/jpeg",
|
|
8608
|
+
jpeg: "image/jpeg",
|
|
8609
|
+
png: "image/png",
|
|
8610
|
+
gif: "image/gif",
|
|
8611
|
+
webp: "image/webp",
|
|
8612
|
+
svg: "image/svg+xml",
|
|
8613
|
+
bmp: "image/bmp",
|
|
8614
|
+
tif: "image/tiff",
|
|
8615
|
+
tiff: "image/tiff",
|
|
8616
|
+
heic: "image/heic",
|
|
8617
|
+
heif: "image/heif",
|
|
8618
|
+
pdf: "application/pdf",
|
|
8619
|
+
csv: "text/csv",
|
|
8620
|
+
txt: "text/plain",
|
|
8621
|
+
md: "text/markdown",
|
|
8622
|
+
html: "text/html",
|
|
8623
|
+
doc: "application/msword",
|
|
8624
|
+
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
8625
|
+
xls: "application/vnd.ms-excel",
|
|
8626
|
+
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
8627
|
+
ppt: "application/vnd.ms-powerpoint",
|
|
8628
|
+
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
8629
|
+
mp3: "audio/mpeg",
|
|
8630
|
+
mpga: "audio/mpeg",
|
|
8631
|
+
wav: "audio/wav",
|
|
8632
|
+
ogg: "audio/ogg",
|
|
8633
|
+
flac: "audio/flac",
|
|
8634
|
+
aac: "audio/aac",
|
|
8635
|
+
m4a: "audio/mp4",
|
|
8636
|
+
webm: "audio/webm",
|
|
8637
|
+
mp4: "video/mp4",
|
|
8638
|
+
mov: "video/quicktime",
|
|
8639
|
+
avi: "video/x-msvideo"
|
|
8640
|
+
};
|
|
8641
|
+
function inferMediaTypeFromUrl(urlOrName) {
|
|
8642
|
+
if (!urlOrName) return void 0;
|
|
8643
|
+
const cleaned = urlOrName.split("?")[0].split("#")[0];
|
|
8644
|
+
const ext = cleaned.split(".").pop()?.toLowerCase();
|
|
8645
|
+
if (!ext || ext === cleaned.toLowerCase()) return void 0;
|
|
8646
|
+
return EXTENSION_MIME[ext];
|
|
8647
|
+
}
|
|
8648
|
+
function audioMimeToOpenAIFormat(mediaType) {
|
|
8649
|
+
const m = (mediaType ?? "").toLowerCase();
|
|
8650
|
+
if (m.includes("wav")) return "wav";
|
|
8651
|
+
return "mp3";
|
|
8652
|
+
}
|
|
8653
|
+
var BEDROCK_DOC_FORMATS = {
|
|
8654
|
+
"application/pdf": "pdf",
|
|
8655
|
+
"text/csv": "csv",
|
|
8656
|
+
"application/msword": "doc",
|
|
8657
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
8658
|
+
"application/vnd.ms-excel": "xls",
|
|
8659
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
|
8660
|
+
"text/html": "html",
|
|
8661
|
+
"text/plain": "txt",
|
|
8662
|
+
"text/markdown": "md"
|
|
8663
|
+
};
|
|
8664
|
+
function documentMimeToBedrockFormat(mediaType, fileName) {
|
|
8665
|
+
if (mediaType && BEDROCK_DOC_FORMATS[mediaType.toLowerCase()]) {
|
|
8666
|
+
return BEDROCK_DOC_FORMATS[mediaType.toLowerCase()];
|
|
8667
|
+
}
|
|
8668
|
+
const inferred = inferMediaTypeFromUrl(fileName);
|
|
8669
|
+
if (inferred && BEDROCK_DOC_FORMATS[inferred]) return BEDROCK_DOC_FORMATS[inferred];
|
|
8670
|
+
return void 0;
|
|
8671
|
+
}
|
|
8672
|
+
function sanitizeBedrockDocName(name, fallback = "document") {
|
|
8673
|
+
const cleaned = (name ?? "").replace(/\.[^.]+$/, "").replace(/[^a-zA-Z0-9\s\-()[\]]/g, " ").replace(/\s+/g, " ").trim();
|
|
8674
|
+
return cleaned || fallback;
|
|
8675
|
+
}
|
|
8676
|
+
|
|
8603
8677
|
// src/providers/openai.ts
|
|
8604
8678
|
var OpenAIProvider = class extends BaseProvider {
|
|
8605
8679
|
constructor(config) {
|
|
@@ -8822,6 +8896,27 @@ var OpenAIProvider = class extends BaseProvider {
|
|
|
8822
8896
|
const url = src.type === "url" ? src.url : `data:${src.mediaType};base64,${src.data}`;
|
|
8823
8897
|
return { type: "input_image", image_url: url };
|
|
8824
8898
|
}
|
|
8899
|
+
if (part.type === "file") {
|
|
8900
|
+
const src = part.source;
|
|
8901
|
+
if (src.type === "base64") {
|
|
8902
|
+
return {
|
|
8903
|
+
type: "input_file",
|
|
8904
|
+
filename: part.fileName ?? "file",
|
|
8905
|
+
file_data: `data:${src.mediaType};base64,${src.data}`
|
|
8906
|
+
};
|
|
8907
|
+
}
|
|
8908
|
+
return { type: "input_file", file_url: src.url };
|
|
8909
|
+
}
|
|
8910
|
+
if (part.type === "audio") {
|
|
8911
|
+
const src = part.source;
|
|
8912
|
+
if (src.type === "base64") {
|
|
8913
|
+
return {
|
|
8914
|
+
type: "input_audio",
|
|
8915
|
+
input_audio: { data: src.data, format: audioMimeToOpenAIFormat(src.mediaType) }
|
|
8916
|
+
};
|
|
8917
|
+
}
|
|
8918
|
+
return { type: textType, text: `[audio: ${src.url}]` };
|
|
8919
|
+
}
|
|
8825
8920
|
return { type: textType, text: "" };
|
|
8826
8921
|
});
|
|
8827
8922
|
return { role, content };
|
|
@@ -8906,6 +9001,29 @@ var OpenAIProvider = class extends BaseProvider {
|
|
|
8906
9001
|
const url = src.type === "url" ? src.url : `data:${src.mediaType};base64,${src.data}`;
|
|
8907
9002
|
return { type: "image_url", image_url: { url } };
|
|
8908
9003
|
}
|
|
9004
|
+
if (part.type === "file") {
|
|
9005
|
+
const src = part.source;
|
|
9006
|
+
if (src.type === "base64") {
|
|
9007
|
+
return {
|
|
9008
|
+
type: "file",
|
|
9009
|
+
file: {
|
|
9010
|
+
filename: part.fileName ?? "file",
|
|
9011
|
+
file_data: `data:${src.mediaType};base64,${src.data}`
|
|
9012
|
+
}
|
|
9013
|
+
};
|
|
9014
|
+
}
|
|
9015
|
+
return { type: "text", text: `[file: ${src.url}]` };
|
|
9016
|
+
}
|
|
9017
|
+
if (part.type === "audio") {
|
|
9018
|
+
const src = part.source;
|
|
9019
|
+
if (src.type === "base64") {
|
|
9020
|
+
return {
|
|
9021
|
+
type: "input_audio",
|
|
9022
|
+
input_audio: { data: src.data, format: audioMimeToOpenAIFormat(src.mediaType) }
|
|
9023
|
+
};
|
|
9024
|
+
}
|
|
9025
|
+
return { type: "text", text: `[audio: ${src.url}]` };
|
|
9026
|
+
}
|
|
8909
9027
|
return part;
|
|
8910
9028
|
});
|
|
8911
9029
|
}
|
|
@@ -9303,6 +9421,22 @@ var AnthropicProvider = class extends BaseProvider {
|
|
|
9303
9421
|
source: { type: "url", url: src.url }
|
|
9304
9422
|
};
|
|
9305
9423
|
}
|
|
9424
|
+
if (part.type === "file") {
|
|
9425
|
+
const src = part.source;
|
|
9426
|
+
const title = part.fileName ? { title: part.fileName } : {};
|
|
9427
|
+
if (src.type === "base64") {
|
|
9428
|
+
return {
|
|
9429
|
+
type: "document",
|
|
9430
|
+
source: { type: "base64", media_type: src.mediaType, data: src.data },
|
|
9431
|
+
...title
|
|
9432
|
+
};
|
|
9433
|
+
}
|
|
9434
|
+
return { type: "document", source: { type: "url", url: src.url }, ...title };
|
|
9435
|
+
}
|
|
9436
|
+
if (part.type === "audio") {
|
|
9437
|
+
const src = part.source;
|
|
9438
|
+
return { type: "text", text: src.type === "url" ? `[audio: ${src.url}]` : "[audio attachment omitted: not supported by this model]" };
|
|
9439
|
+
}
|
|
9306
9440
|
return part;
|
|
9307
9441
|
});
|
|
9308
9442
|
}
|
|
@@ -9770,6 +9904,24 @@ var BedrockProvider = class extends BaseProvider {
|
|
|
9770
9904
|
}
|
|
9771
9905
|
});
|
|
9772
9906
|
}
|
|
9907
|
+
} else if (part.type === "file") {
|
|
9908
|
+
const src = part.source;
|
|
9909
|
+
const format = documentMimeToBedrockFormat(src.mediaType, part.fileName);
|
|
9910
|
+
if (src.type === "base64" && format) {
|
|
9911
|
+
content.push({
|
|
9912
|
+
document: {
|
|
9913
|
+
format,
|
|
9914
|
+
name: sanitizeBedrockDocName(part.fileName),
|
|
9915
|
+
source: { bytes: src.data }
|
|
9916
|
+
}
|
|
9917
|
+
});
|
|
9918
|
+
} else {
|
|
9919
|
+
const ref = src.type === "url" ? src.url : part.fileName ?? "attachment";
|
|
9920
|
+
content.push({ text: `[file: ${ref}]` });
|
|
9921
|
+
}
|
|
9922
|
+
} else if (part.type === "audio") {
|
|
9923
|
+
const src = part.source;
|
|
9924
|
+
content.push({ text: src.type === "url" ? `[audio: ${src.url}]` : "[audio attachment omitted: not supported by this model]" });
|
|
9773
9925
|
}
|
|
9774
9926
|
}
|
|
9775
9927
|
}
|
|
@@ -10113,7 +10265,25 @@ var VertexProvider = class extends BaseProvider {
|
|
|
10113
10265
|
});
|
|
10114
10266
|
} else {
|
|
10115
10267
|
parts.push({
|
|
10116
|
-
fileData: {
|
|
10268
|
+
fileData: {
|
|
10269
|
+
mimeType: src.mediaType ?? inferMediaTypeFromUrl(src.url) ?? "image/jpeg",
|
|
10270
|
+
fileUri: src.url
|
|
10271
|
+
}
|
|
10272
|
+
});
|
|
10273
|
+
}
|
|
10274
|
+
} else if (part.type === "file" || part.type === "audio") {
|
|
10275
|
+
const src = part.source;
|
|
10276
|
+
const fallbackMime = part.type === "audio" ? "audio/mpeg" : "application/pdf";
|
|
10277
|
+
if (src.type === "base64") {
|
|
10278
|
+
parts.push({
|
|
10279
|
+
inlineData: { mimeType: src.mediaType ?? fallbackMime, data: src.data }
|
|
10280
|
+
});
|
|
10281
|
+
} else {
|
|
10282
|
+
parts.push({
|
|
10283
|
+
fileData: {
|
|
10284
|
+
mimeType: src.mediaType ?? inferMediaTypeFromUrl(src.url) ?? inferMediaTypeFromUrl(part.fileName) ?? fallbackMime,
|
|
10285
|
+
fileUri: src.url
|
|
10286
|
+
}
|
|
10117
10287
|
});
|
|
10118
10288
|
}
|
|
10119
10289
|
}
|
|
@@ -10458,9 +10628,50 @@ function convertContent(content) {
|
|
|
10458
10628
|
}
|
|
10459
10629
|
}
|
|
10460
10630
|
}
|
|
10631
|
+
if (part.type === "file" || part.type === "document") {
|
|
10632
|
+
const source = extractBinarySource(part);
|
|
10633
|
+
if (source) {
|
|
10634
|
+
const fileName = part.metadata?.filename ?? part.metadata?.fileName ?? part.fileName ?? part.filename ?? part.name ?? part.file?.filename;
|
|
10635
|
+
return { type: "file", source, ...fileName ? { fileName } : {} };
|
|
10636
|
+
}
|
|
10637
|
+
}
|
|
10638
|
+
if (part.type === "audio" || part.type === "input_audio") {
|
|
10639
|
+
const ia = part.input_audio;
|
|
10640
|
+
if (ia?.data) {
|
|
10641
|
+
const mediaType = ia.format === "wav" ? "audio/wav" : "audio/mpeg";
|
|
10642
|
+
return { type: "audio", source: { type: "base64", mediaType, data: ia.data } };
|
|
10643
|
+
}
|
|
10644
|
+
const source = extractBinarySource(part);
|
|
10645
|
+
if (source) return { type: "audio", source };
|
|
10646
|
+
}
|
|
10461
10647
|
return { type: "text", text: part.text ?? part.content ?? JSON.stringify(part) };
|
|
10462
10648
|
});
|
|
10463
10649
|
}
|
|
10650
|
+
function extractBinarySource(part) {
|
|
10651
|
+
const src = part.source;
|
|
10652
|
+
if (src?.type === "base64" && src.data) {
|
|
10653
|
+
return { type: "base64", mediaType: src.mediaType ?? src.media_type ?? src.mime_type ?? "application/octet-stream", data: src.data };
|
|
10654
|
+
}
|
|
10655
|
+
if (src?.type === "url" && src.url) {
|
|
10656
|
+
const mediaType2 = src.mediaType ?? src.media_type ?? src.mime_type;
|
|
10657
|
+
return { type: "url", url: src.url, ...mediaType2 ? { mediaType: mediaType2 } : {} };
|
|
10658
|
+
}
|
|
10659
|
+
const mediaType = part.mime_type ?? part.mimeType ?? part.media_type ?? part.mediaType;
|
|
10660
|
+
if (part.source_type === "base64" && part.data) {
|
|
10661
|
+
return { type: "base64", mediaType: mediaType ?? "application/octet-stream", data: part.data };
|
|
10662
|
+
}
|
|
10663
|
+
if (part.source_type === "url" && part.url) {
|
|
10664
|
+
return { type: "url", url: part.url, ...mediaType ? { mediaType } : {} };
|
|
10665
|
+
}
|
|
10666
|
+
const value = part.data ?? part.url ?? part.file?.file_data;
|
|
10667
|
+
if (typeof value === "string" && value) {
|
|
10668
|
+
const match = value.match(/^data:([^;]+);base64,(.+)$/);
|
|
10669
|
+
if (match) return { type: "base64", mediaType: mediaType ?? match[1], data: match[2] };
|
|
10670
|
+
if (/^https?:\/\//.test(value)) return { type: "url", url: value, ...mediaType ? { mediaType } : {} };
|
|
10671
|
+
if (mediaType) return { type: "base64", mediaType, data: value };
|
|
10672
|
+
}
|
|
10673
|
+
return void 0;
|
|
10674
|
+
}
|
|
10464
10675
|
function isZodSchema(obj) {
|
|
10465
10676
|
return obj != null && typeof obj === "object" && typeof obj.safeParse === "function" && "_def" in obj;
|
|
10466
10677
|
}
|
|
@@ -10738,6 +10949,7 @@ function createProvider(config) {
|
|
|
10738
10949
|
}
|
|
10739
10950
|
}
|
|
10740
10951
|
|
|
10952
|
+
exports.ASK_USER_TOOL_NAME = ASK_USER_TOOL_NAME;
|
|
10741
10953
|
exports.AnthropicProvider = AnthropicProvider;
|
|
10742
10954
|
exports.AzureProvider = AzureProvider;
|
|
10743
10955
|
exports.BaseProvider = BaseProvider;
|