@anvia/studio 0.7.28 → 0.7.29
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.js +36 -41
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -951,20 +951,17 @@ function serializeError(error) {
|
|
|
951
951
|
}
|
|
952
952
|
|
|
953
953
|
// src/runtime/type-guards.ts
|
|
954
|
+
import {
|
|
955
|
+
isJsonValue as isCoreJsonValue
|
|
956
|
+
} from "@anvia/core/completion";
|
|
954
957
|
function isObject(value) {
|
|
955
958
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
956
959
|
}
|
|
957
960
|
function isJsonObject(value) {
|
|
958
|
-
return isObject(value) &&
|
|
961
|
+
return isObject(value) && isCoreJsonValue(value);
|
|
959
962
|
}
|
|
960
963
|
function isJsonValue(value) {
|
|
961
|
-
|
|
962
|
-
return true;
|
|
963
|
-
}
|
|
964
|
-
if (Array.isArray(value)) {
|
|
965
|
-
return value.every(isJsonValue);
|
|
966
|
-
}
|
|
967
|
-
return isJsonObject(value);
|
|
964
|
+
return isCoreJsonValue(value);
|
|
968
965
|
}
|
|
969
966
|
function isMessageInput(value) {
|
|
970
967
|
return typeof value === "string" || isMessage(value);
|
|
@@ -4662,7 +4659,7 @@ async function parsePipelineRunRequest(c) {
|
|
|
4662
4659
|
if (!isObject(body)) {
|
|
4663
4660
|
return { error: errorResponse(c, 400, "bad_request", "Request body must be an object") };
|
|
4664
4661
|
}
|
|
4665
|
-
if (!("input" in body) || !
|
|
4662
|
+
if (!("input" in body) || !isJsonValue(body.input)) {
|
|
4666
4663
|
return { error: errorResponse(c, 400, "bad_request", "input must be JSON-compatible") };
|
|
4667
4664
|
}
|
|
4668
4665
|
const request = {
|
|
@@ -4734,18 +4731,6 @@ function parsePipelineLogAfter(value) {
|
|
|
4734
4731
|
}
|
|
4735
4732
|
return after;
|
|
4736
4733
|
}
|
|
4737
|
-
function isJsonValue2(value) {
|
|
4738
|
-
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
4739
|
-
return Number.isFinite(value) || typeof value !== "number";
|
|
4740
|
-
}
|
|
4741
|
-
if (Array.isArray(value)) {
|
|
4742
|
-
return value.every(isJsonValue2);
|
|
4743
|
-
}
|
|
4744
|
-
if (isObject(value)) {
|
|
4745
|
-
return Object.values(value).every((item) => item === void 0 || isJsonValue2(item));
|
|
4746
|
-
}
|
|
4747
|
-
return false;
|
|
4748
|
-
}
|
|
4749
4734
|
|
|
4750
4735
|
// src/runtime/questions.ts
|
|
4751
4736
|
import { createHook as createHook2 } from "@anvia/core/hooks";
|
|
@@ -5362,7 +5347,7 @@ function traceSummary2(trace) {
|
|
|
5362
5347
|
}
|
|
5363
5348
|
function traceAgentId(trace) {
|
|
5364
5349
|
const nestedMetadata = trace.metadata?.metadata;
|
|
5365
|
-
return
|
|
5350
|
+
return isJsonObject(nestedMetadata) && typeof nestedMetadata.agentId === "string" ? nestedMetadata.agentId : void 0;
|
|
5366
5351
|
}
|
|
5367
5352
|
function studioRunId(context) {
|
|
5368
5353
|
const value = context.metadata?.studioRunId;
|
|
@@ -5375,19 +5360,7 @@ function serializeJsonError(error) {
|
|
|
5375
5360
|
message: error.message
|
|
5376
5361
|
};
|
|
5377
5362
|
}
|
|
5378
|
-
return
|
|
5379
|
-
}
|
|
5380
|
-
function isJsonObject2(value) {
|
|
5381
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5382
|
-
}
|
|
5383
|
-
function isJsonValue3(value) {
|
|
5384
|
-
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
5385
|
-
return true;
|
|
5386
|
-
}
|
|
5387
|
-
if (Array.isArray(value)) {
|
|
5388
|
-
return value.every(isJsonValue3);
|
|
5389
|
-
}
|
|
5390
|
-
return isJsonObject2(value) && Object.values(value).every(isJsonValue3);
|
|
5363
|
+
return isJsonValue(error) ? error : String(error);
|
|
5391
5364
|
}
|
|
5392
5365
|
|
|
5393
5366
|
// src/runtime/shared.ts
|
|
@@ -5951,6 +5924,7 @@ function hasStudioTraceObserver(agent) {
|
|
|
5951
5924
|
import { mkdirSync } from "fs";
|
|
5952
5925
|
import { createRequire } from "module";
|
|
5953
5926
|
import { dirname, resolve } from "path";
|
|
5927
|
+
import { isJsonValue as isJsonValue2 } from "@anvia/core/completion";
|
|
5954
5928
|
var DatabaseSync;
|
|
5955
5929
|
function createSqliteSessionStore(options = {}) {
|
|
5956
5930
|
return new SqliteSessionStore(options.path ?? ":memory:");
|
|
@@ -6607,6 +6581,7 @@ var SqliteSessionStore = class {
|
|
|
6607
6581
|
message_index INTEGER NOT NULL,
|
|
6608
6582
|
role TEXT NOT NULL,
|
|
6609
6583
|
message_id TEXT,
|
|
6584
|
+
metadata_json TEXT,
|
|
6610
6585
|
created_at TEXT NOT NULL,
|
|
6611
6586
|
PRIMARY KEY(session_id, message_index),
|
|
6612
6587
|
FOREIGN KEY(session_id) REFERENCES anvia_studio_sessions(id) ON DELETE CASCADE
|
|
@@ -6701,6 +6676,7 @@ var SqliteSessionStore = class {
|
|
|
6701
6676
|
CREATE INDEX IF NOT EXISTS anvia_studio_traces_session_started_idx
|
|
6702
6677
|
ON anvia_studio_traces(session_id, started_at DESC);
|
|
6703
6678
|
`);
|
|
6679
|
+
ensureMessageMetadataColumn(db);
|
|
6704
6680
|
this.db = db;
|
|
6705
6681
|
return db;
|
|
6706
6682
|
}
|
|
@@ -6745,7 +6721,7 @@ var SqliteSessionStore = class {
|
|
|
6745
6721
|
listSessionMessages(sessionId) {
|
|
6746
6722
|
const db = this.database();
|
|
6747
6723
|
const messageRows = db.prepare(
|
|
6748
|
-
`SELECT session_id, message_index, role, message_id, created_at
|
|
6724
|
+
`SELECT session_id, message_index, role, message_id, metadata_json, created_at
|
|
6749
6725
|
FROM anvia_studio_session_messages
|
|
6750
6726
|
WHERE session_id = $sessionId
|
|
6751
6727
|
ORDER BY message_index ASC`
|
|
@@ -6785,8 +6761,9 @@ var SqliteSessionStore = class {
|
|
|
6785
6761
|
message_index,
|
|
6786
6762
|
role,
|
|
6787
6763
|
message_id,
|
|
6764
|
+
metadata_json,
|
|
6788
6765
|
created_at
|
|
6789
|
-
) VALUES ($sessionId, $messageIndex, $role, $messageId, $createdAt)`
|
|
6766
|
+
) VALUES ($sessionId, $messageIndex, $role, $messageId, $metadata, $createdAt)`
|
|
6790
6767
|
);
|
|
6791
6768
|
const insertPart = db.prepare(
|
|
6792
6769
|
`INSERT INTO anvia_studio_session_message_parts (
|
|
@@ -6799,11 +6776,15 @@ var SqliteSessionStore = class {
|
|
|
6799
6776
|
);
|
|
6800
6777
|
messages.forEach((message, messageOffset) => {
|
|
6801
6778
|
const messageIndex = startIndex + messageOffset;
|
|
6779
|
+
if (message.metadata !== void 0 && !isJsonValue2(message.metadata)) {
|
|
6780
|
+
throw new TypeError("Studio message metadata must be a strict JSON value.");
|
|
6781
|
+
}
|
|
6802
6782
|
insertMessage.run({
|
|
6803
6783
|
$sessionId: sessionId,
|
|
6804
6784
|
$messageIndex: messageIndex,
|
|
6805
6785
|
$role: message.role,
|
|
6806
6786
|
$messageId: message.role === "assistant" ? message.id ?? null : null,
|
|
6787
|
+
$metadata: message.metadata === void 0 ? null : JSON.stringify(message.metadata),
|
|
6807
6788
|
$createdAt: createdAt
|
|
6808
6789
|
});
|
|
6809
6790
|
messageParts(message).forEach((part, partIndex) => {
|
|
@@ -6899,26 +6880,34 @@ function messageParts(message) {
|
|
|
6899
6880
|
}
|
|
6900
6881
|
function messageFromRows(row, partRows) {
|
|
6901
6882
|
const parts = partRows.map((partRow) => JSON.parse(partRow.part_json));
|
|
6883
|
+
const metadata = parseJsonValue(row.metadata_json);
|
|
6884
|
+
if (metadata !== void 0 && !isJsonValue2(metadata)) {
|
|
6885
|
+
throw new TypeError("Stored Studio message metadata is not a strict JSON value.");
|
|
6886
|
+
}
|
|
6887
|
+
const metadataProperties = compact({ metadata });
|
|
6902
6888
|
if (row.role === "system") {
|
|
6903
|
-
return { role: "system", content: systemContentFromParts(parts) };
|
|
6889
|
+
return { role: "system", content: systemContentFromParts(parts), ...metadataProperties };
|
|
6904
6890
|
}
|
|
6905
6891
|
if (row.role === "user") {
|
|
6906
6892
|
return {
|
|
6907
6893
|
role: "user",
|
|
6908
|
-
content: parts
|
|
6894
|
+
content: parts,
|
|
6895
|
+
...metadataProperties
|
|
6909
6896
|
};
|
|
6910
6897
|
}
|
|
6911
6898
|
if (row.role === "assistant") {
|
|
6912
6899
|
return {
|
|
6913
6900
|
role: "assistant",
|
|
6914
6901
|
...compact({ id: row.message_id ?? void 0 }),
|
|
6915
|
-
content: parts
|
|
6902
|
+
content: parts,
|
|
6903
|
+
...metadataProperties
|
|
6916
6904
|
};
|
|
6917
6905
|
}
|
|
6918
6906
|
if (row.role === "tool") {
|
|
6919
6907
|
return {
|
|
6920
6908
|
role: "tool",
|
|
6921
|
-
content: parts
|
|
6909
|
+
content: parts,
|
|
6910
|
+
...metadataProperties
|
|
6922
6911
|
};
|
|
6923
6912
|
}
|
|
6924
6913
|
throw new Error(`Unsupported stored message role: ${row.role}`);
|
|
@@ -6938,6 +6927,12 @@ function guardAgainstLegacySessionSchema(db) {
|
|
|
6938
6927
|
);
|
|
6939
6928
|
}
|
|
6940
6929
|
}
|
|
6930
|
+
function ensureMessageMetadataColumn(db) {
|
|
6931
|
+
const columns = db.prepare("PRAGMA table_info('anvia_studio_session_messages')").all();
|
|
6932
|
+
if (!columns.some((column) => column.name === "metadata_json")) {
|
|
6933
|
+
db.exec("ALTER TABLE anvia_studio_session_messages ADD COLUMN metadata_json TEXT");
|
|
6934
|
+
}
|
|
6935
|
+
}
|
|
6941
6936
|
function loadDatabaseSync() {
|
|
6942
6937
|
if (DatabaseSync !== void 0) {
|
|
6943
6938
|
return DatabaseSync;
|