@tangle-network/agent-app 0.56.0 → 0.56.1
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.
|
@@ -8,7 +8,9 @@
|
|
|
8
8
|
* the same `DetachedTurnFinal` shape `runDetachedTurn` already consumes.
|
|
9
9
|
*/
|
|
10
10
|
import type { SandboxInstance, SessionMessage } from '@tangle-network/sandbox';
|
|
11
|
+
import { type JsonRecord } from '../stream/index';
|
|
11
12
|
import type { DetachedTurnFinal } from './detached-turn';
|
|
13
|
+
import type { ChatTurnModelAttribution, ChatTurnUsage } from './turn-routes';
|
|
12
14
|
/** The official Sandbox methods needed for completed-turn recovery. */
|
|
13
15
|
export type CompletedSandboxTurnSource = Pick<SandboxInstance, 'findCompletedTurn' | 'session'>;
|
|
14
16
|
/** Options for resolving one exact detached turn. */
|
|
@@ -17,6 +19,10 @@ export interface ReadCompletedSandboxTurnOptions {
|
|
|
17
19
|
sessionId: string;
|
|
18
20
|
log?: (message: string, meta?: Record<string, unknown>) => void;
|
|
19
21
|
}
|
|
22
|
+
/** @internal Shared terminal-result decoder; callers must establish exact turn identity. */
|
|
23
|
+
export declare function usageFromResult(result: JsonRecord | undefined): ChatTurnUsage;
|
|
24
|
+
/** @internal Attribution travels with the same validated result as content and usage. */
|
|
25
|
+
export declare function attributionFromResult(result: Record<string, unknown> | undefined): Pick<ChatTurnModelAttribution, 'servedModel' | 'servedProvider' | 'servedSource'>;
|
|
20
26
|
/**
|
|
21
27
|
* Recover the durable content a Sandbox recorder attached to one assistant
|
|
22
28
|
* message. The caller is responsible for proving that the message belongs to
|
|
@@ -28,5 +34,7 @@ export declare function recoverSandboxAssistantMessage(message: SessionMessage):
|
|
|
28
34
|
* Read the exact completed turn from its keyed cache and completed message.
|
|
29
35
|
* Never borrow a session-wide aggregate from a concurrently advancing session.
|
|
30
36
|
* Observation failure is retryable and distinct from successful absent reads.
|
|
37
|
+
* Model attribution comes from that same identity-checked cache record, never
|
|
38
|
+
* from a second unchecked lookup that could belong to a different turn.
|
|
31
39
|
*/
|
|
32
|
-
export declare function readCompletedSandboxTurn(box: CompletedSandboxTurnSource, options: ReadCompletedSandboxTurnOptions): Promise<DetachedTurnFinal | null>;
|
|
40
|
+
export declare function readCompletedSandboxTurn(box: CompletedSandboxTurnSource, options: ReadCompletedSandboxTurnOptions): Promise<(DetachedTurnFinal & Pick<ChatTurnModelAttribution, 'servedModel' | 'servedProvider' | 'servedSource'>) | null>;
|
|
@@ -17,7 +17,7 @@ export * from './protected-runtime-producer';
|
|
|
17
17
|
export * from './model-failover-stream';
|
|
18
18
|
export * from './draft-persistence';
|
|
19
19
|
export * from './detached-turn';
|
|
20
|
-
export
|
|
20
|
+
export { type CompletedSandboxTurnSource, type ReadCompletedSandboxTurnOptions, readCompletedSandboxTurn, recoverSandboxAssistantMessage, } from './completed-sandbox-turn';
|
|
21
21
|
export * from './native-completion';
|
|
22
22
|
export * from './plan-follow-up';
|
|
23
23
|
export * from './durable-projection';
|
|
@@ -1392,12 +1392,12 @@ async function runDetachedTurn(opts) {
|
|
|
1392
1392
|
const settleRow = async (base, cachedPersisted) => {
|
|
1393
1393
|
const persisted = cachedPersisted ?? (!producer ? await persistedRow() : void 0);
|
|
1394
1394
|
const info = producer?.modelFailover?.();
|
|
1395
|
-
const
|
|
1395
|
+
const attribution = producer?.modelAttribution?.();
|
|
1396
1396
|
const model = producer?.model ?? persisted?.model ?? opts.model;
|
|
1397
|
-
const requestedModel =
|
|
1398
|
-
const servedModel =
|
|
1399
|
-
const servedProvider =
|
|
1400
|
-
const servedSource =
|
|
1397
|
+
const requestedModel = attribution?.requestedModel ?? persisted?.requestedModel ?? opts.model;
|
|
1398
|
+
const servedModel = attribution?.servedModel ?? persisted?.servedModel;
|
|
1399
|
+
const servedProvider = attribution?.servedProvider ?? persisted?.servedProvider;
|
|
1400
|
+
const servedSource = attribution?.servedSource ?? (persisted?.servedSource === "request" || persisted?.servedSource === "environment" || persisted?.servedSource === "profile" ? persisted.servedSource : void 0);
|
|
1401
1401
|
const result = {
|
|
1402
1402
|
...base,
|
|
1403
1403
|
...model ? { model } : {},
|
|
@@ -1558,6 +1558,23 @@ function usageFromResult(result) {
|
|
|
1558
1558
|
...costUsd !== void 0 ? { costUsd } : {}
|
|
1559
1559
|
};
|
|
1560
1560
|
}
|
|
1561
|
+
function nonEmptyString(value) {
|
|
1562
|
+
return typeof value === "string" && value.trim() ? value : void 0;
|
|
1563
|
+
}
|
|
1564
|
+
function attributionFromResult(result) {
|
|
1565
|
+
const metadata = asRecord(result?.metadata);
|
|
1566
|
+
const modelAttribution = asRecord(result?.modelAttribution) ?? asRecord(metadata?.modelAttribution);
|
|
1567
|
+
const backend = asRecord(result?.effectiveBackend) ?? asRecord(metadata?.effectiveBackend);
|
|
1568
|
+
const source = nonEmptyString(result?.servedSource) ?? nonEmptyString(modelAttribution?.servedSource) ?? nonEmptyString(backend?.source);
|
|
1569
|
+
const servedSource = source === "request" || source === "environment" || source === "profile" ? source : void 0;
|
|
1570
|
+
const servedModel = nonEmptyString(result?.servedModel) ?? nonEmptyString(modelAttribution?.servedModel) ?? nonEmptyString(backend?.model);
|
|
1571
|
+
const servedProvider = nonEmptyString(result?.servedProvider) ?? nonEmptyString(modelAttribution?.servedProvider) ?? nonEmptyString(backend?.provider);
|
|
1572
|
+
return {
|
|
1573
|
+
...servedModel ? { servedModel } : {},
|
|
1574
|
+
...servedProvider ? { servedProvider } : {},
|
|
1575
|
+
...servedSource ? { servedSource } : {}
|
|
1576
|
+
};
|
|
1577
|
+
}
|
|
1561
1578
|
function textFromResult(result) {
|
|
1562
1579
|
return asString(result?.response) ?? asString(result?.finalText) ?? asString(result?.text) ?? asString(result?.output);
|
|
1563
1580
|
}
|
|
@@ -1666,7 +1683,8 @@ async function readCompletedSandboxTurn(box, options) {
|
|
|
1666
1683
|
return {
|
|
1667
1684
|
...text !== void 0 ? { text } : {},
|
|
1668
1685
|
...hasUsage2(usage) ? { usage } : {},
|
|
1669
|
-
...parts ? { parts } : {}
|
|
1686
|
+
...parts ? { parts } : {},
|
|
1687
|
+
...attributionFromResult(result)
|
|
1670
1688
|
};
|
|
1671
1689
|
}
|
|
1672
1690
|
|
|
@@ -1677,9 +1695,6 @@ function timestamp(value) {
|
|
|
1677
1695
|
if (value instanceof Date) return value.getTime();
|
|
1678
1696
|
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
1679
1697
|
}
|
|
1680
|
-
function nonEmptyString(value) {
|
|
1681
|
-
return typeof value === "string" && value.trim() ? value : void 0;
|
|
1682
|
-
}
|
|
1683
1698
|
function record(value) {
|
|
1684
1699
|
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
1685
1700
|
}
|
|
@@ -1687,39 +1702,11 @@ function isSandboxNotFoundError(value) {
|
|
|
1687
1702
|
const error = record(value);
|
|
1688
1703
|
return error?.name === "NotFoundError" || error?.code === "NOT_FOUND" || error?.status === 404;
|
|
1689
1704
|
}
|
|
1690
|
-
function attribution(result) {
|
|
1691
|
-
const metadata = record(result?.metadata);
|
|
1692
|
-
const modelAttribution = record(result?.modelAttribution) ?? record(metadata?.modelAttribution);
|
|
1693
|
-
const backend = record(result?.effectiveBackend) ?? record(metadata?.effectiveBackend);
|
|
1694
|
-
const source = nonEmptyString(result?.servedSource) ?? nonEmptyString(modelAttribution?.servedSource) ?? nonEmptyString(backend?.source);
|
|
1695
|
-
const servedSource = source === "request" || source === "environment" || source === "profile" ? source : void 0;
|
|
1696
|
-
const servedModel = nonEmptyString(result?.servedModel) ?? nonEmptyString(modelAttribution?.servedModel) ?? nonEmptyString(backend?.model);
|
|
1697
|
-
const servedProvider = nonEmptyString(result?.servedProvider) ?? nonEmptyString(modelAttribution?.servedProvider) ?? nonEmptyString(backend?.provider);
|
|
1698
|
-
return {
|
|
1699
|
-
...servedModel ? { servedModel } : {},
|
|
1700
|
-
...servedProvider ? { servedProvider } : {},
|
|
1701
|
-
...servedSource ? { servedSource } : {}
|
|
1702
|
-
};
|
|
1703
|
-
}
|
|
1704
|
-
function usageFromResult2(result) {
|
|
1705
|
-
const raw = record(result?.usage) ?? record(result?.tokenUsage);
|
|
1706
|
-
const number = (value) => typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
1707
|
-
return {
|
|
1708
|
-
...number(raw?.inputTokens) !== void 0 ? { inputTokens: number(raw?.inputTokens) } : {},
|
|
1709
|
-
...number(raw?.outputTokens) !== void 0 ? { outputTokens: number(raw?.outputTokens) } : {},
|
|
1710
|
-
...number(raw?.cacheReadTokens) !== void 0 ? { cacheReadTokens: number(raw?.cacheReadTokens) } : {},
|
|
1711
|
-
...number(raw?.cacheWriteTokens) !== void 0 ? { cacheWriteTokens: number(raw?.cacheWriteTokens) } : {},
|
|
1712
|
-
...number(result?.costUsd) !== void 0 ? { costUsd: number(result?.costUsd) } : {}
|
|
1713
|
-
};
|
|
1714
|
-
}
|
|
1715
1705
|
function interruptedMessages(messages, turnId) {
|
|
1716
1706
|
return messages.filter(
|
|
1717
1707
|
(message) => message.role === "assistant" && message.metadata?.turnId === turnId && (message.metadata.interrupted === true || message.metadata.status === "interrupted")
|
|
1718
1708
|
);
|
|
1719
1709
|
}
|
|
1720
|
-
function mergeUsage(messageUsage, resultUsage) {
|
|
1721
|
-
return { ...messageUsage, ...resultUsage };
|
|
1722
|
-
}
|
|
1723
1710
|
function consistent(values) {
|
|
1724
1711
|
if (values.length === 0 || values.some((value) => value === void 0)) return void 0;
|
|
1725
1712
|
const first = values[0];
|
|
@@ -1807,7 +1794,7 @@ async function observeNativeCompletion(options) {
|
|
|
1807
1794
|
return { state: "running" };
|
|
1808
1795
|
}
|
|
1809
1796
|
if (admittedTurnIds.some((turnId) => runsByTurnId.get(turnId)?.status === "active")) return { state: "running" };
|
|
1810
|
-
|
|
1797
|
+
let messages;
|
|
1811
1798
|
const recovered = [];
|
|
1812
1799
|
for (const turnId of admittedTurnIds) {
|
|
1813
1800
|
const run = runsByTurnId.get(turnId);
|
|
@@ -1816,19 +1803,18 @@ async function observeNativeCompletion(options) {
|
|
|
1816
1803
|
continue;
|
|
1817
1804
|
}
|
|
1818
1805
|
const completed = run.status === "completed" ? await readCompletedSandboxTurn(source, { turnId, sessionId: options.sessionId }) : null;
|
|
1819
|
-
const cached = run.status === "completed" ? await source.findCompletedTurn(turnId, { sessionId: options.sessionId }) : null;
|
|
1820
1806
|
if (run.status === "completed" && completed) {
|
|
1821
1807
|
recovered.push({
|
|
1808
|
+
...completed,
|
|
1822
1809
|
turnId,
|
|
1823
1810
|
state: "completed",
|
|
1824
1811
|
text: completed.text ?? "",
|
|
1825
1812
|
parts: completed.parts ?? [],
|
|
1826
|
-
usage: completed.usage ?? {}
|
|
1827
|
-
...attribution(cached?.result)
|
|
1813
|
+
usage: completed.usage ?? {}
|
|
1828
1814
|
});
|
|
1829
1815
|
continue;
|
|
1830
1816
|
}
|
|
1831
|
-
const interrupted = interruptedMessages(messages, turnId);
|
|
1817
|
+
const interrupted = run.status === "failed" || run.status === "cancelled" ? interruptedMessages(messages ??= await exactSession.messages({ limit: 1e3 }), turnId) : [];
|
|
1832
1818
|
if ((run.status === "failed" || run.status === "cancelled") && interrupted.length === 1) {
|
|
1833
1819
|
const message = recoverSandboxAssistantMessage(interrupted[0]);
|
|
1834
1820
|
const result = await exactSession.result({ executionId: turnId });
|
|
@@ -1837,11 +1823,8 @@ async function observeNativeCompletion(options) {
|
|
|
1837
1823
|
state: "failed",
|
|
1838
1824
|
text: message.text ?? result.response ?? "",
|
|
1839
1825
|
parts: message.parts ?? [],
|
|
1840
|
-
usage:
|
|
1841
|
-
|
|
1842
|
-
...result.costUsd !== void 0 ? { costUsd: result.costUsd } : {}
|
|
1843
|
-
}),
|
|
1844
|
-
...attribution(result),
|
|
1826
|
+
usage: { ...message.usage, ...usageFromResult(result) },
|
|
1827
|
+
...attributionFromResult(result),
|
|
1845
1828
|
error: result.error ?? interrupted[0].metadata?.interruptReason ?? status.failureReason?.message
|
|
1846
1829
|
});
|
|
1847
1830
|
continue;
|