@fabric-harness/sdk 1.13.0 → 2.0.0
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/action.d.ts +69 -0
- package/dist/action.d.ts.map +1 -0
- package/dist/action.js +119 -0
- package/dist/action.js.map +1 -0
- package/dist/agent-definition.d.ts +6 -0
- package/dist/agent-definition.d.ts.map +1 -1
- package/dist/agent-definition.js.map +1 -1
- package/dist/agent.d.ts +2 -0
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +35 -1
- package/dist/agent.js.map +1 -1
- package/dist/attachment-store-contract.d.ts +34 -0
- package/dist/attachment-store-contract.d.ts.map +1 -0
- package/dist/attachment-store-contract.js +0 -0
- package/dist/attachment-store-contract.js.map +1 -0
- package/dist/attachment-store.d.ts +107 -0
- package/dist/attachment-store.d.ts.map +1 -0
- package/dist/attachment-store.js +164 -0
- package/dist/attachment-store.js.map +1 -0
- package/dist/channel.d.ts +84 -0
- package/dist/channel.d.ts.map +1 -0
- package/dist/channel.js +115 -0
- package/dist/channel.js.map +1 -0
- package/dist/context-budget.d.ts +16 -0
- package/dist/context-budget.d.ts.map +1 -1
- package/dist/context-budget.js +38 -3
- package/dist/context-budget.js.map +1 -1
- package/dist/conversation-projection.d.ts +52 -0
- package/dist/conversation-projection.d.ts.map +1 -0
- package/dist/conversation-projection.js +207 -0
- package/dist/conversation-projection.js.map +1 -0
- package/dist/conversation-stream-contract.d.ts +37 -0
- package/dist/conversation-stream-contract.d.ts.map +1 -0
- package/dist/conversation-stream-contract.js +453 -0
- package/dist/conversation-stream-contract.js.map +1 -0
- package/dist/conversation-stream.d.ts +141 -0
- package/dist/conversation-stream.d.ts.map +1 -0
- package/dist/conversation-stream.js +159 -0
- package/dist/conversation-stream.js.map +1 -0
- package/dist/cost-budget.d.ts +106 -1
- package/dist/cost-budget.d.ts.map +1 -1
- package/dist/cost-budget.js +48 -2
- package/dist/cost-budget.js.map +1 -1
- package/dist/delivered-message.d.ts +90 -0
- package/dist/delivered-message.d.ts.map +1 -0
- package/dist/delivered-message.js +154 -0
- package/dist/delivered-message.js.map +1 -0
- package/dist/dispatch.d.ts +108 -0
- package/dist/dispatch.d.ts.map +1 -0
- package/dist/dispatch.js +179 -0
- package/dist/dispatch.js.map +1 -0
- package/dist/error.d.ts +1 -1
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js.map +1 -1
- package/dist/history.d.ts +49 -2
- package/dist/history.d.ts.map +1 -1
- package/dist/history.js +163 -0
- package/dist/history.js.map +1 -1
- package/dist/in-memory-submission-store.d.ts +53 -0
- package/dist/in-memory-submission-store.d.ts.map +1 -0
- package/dist/in-memory-submission-store.js +340 -0
- package/dist/in-memory-submission-store.js.map +1 -0
- package/dist/index.d.ts +51 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +28 -7
- package/dist/index.js.map +1 -1
- package/dist/loop.d.ts +29 -3
- package/dist/loop.d.ts.map +1 -1
- package/dist/loop.js +167 -24
- package/dist/loop.js.map +1 -1
- package/dist/model.d.ts +37 -0
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +250 -79
- package/dist/model.js.map +1 -1
- package/dist/otel-observer.d.ts +44 -0
- package/dist/otel-observer.d.ts.map +1 -0
- package/dist/otel-observer.js +165 -0
- package/dist/otel-observer.js.map +1 -0
- package/dist/persistence.d.ts +70 -0
- package/dist/persistence.d.ts.map +1 -0
- package/dist/persistence.js +17 -0
- package/dist/persistence.js.map +1 -0
- package/dist/persistent-agent.d.ts +86 -0
- package/dist/persistent-agent.d.ts.map +1 -0
- package/dist/persistent-agent.js +75 -0
- package/dist/persistent-agent.js.map +1 -0
- package/dist/pi-loop-runtime.d.ts.map +1 -1
- package/dist/pi-loop-runtime.js +7 -1
- package/dist/pi-loop-runtime.js.map +1 -1
- package/dist/profile.d.ts +33 -0
- package/dist/profile.d.ts.map +1 -0
- package/dist/profile.js +10 -0
- package/dist/profile.js.map +1 -0
- package/dist/providers.d.ts +14 -0
- package/dist/providers.d.ts.map +1 -1
- package/dist/providers.js +18 -1
- package/dist/providers.js.map +1 -1
- package/dist/result.d.ts +12 -0
- package/dist/result.d.ts.map +1 -1
- package/dist/result.js +26 -0
- package/dist/result.js.map +1 -1
- package/dist/retrieval.d.ts +61 -0
- package/dist/retrieval.d.ts.map +1 -0
- package/dist/retrieval.js +26 -0
- package/dist/retrieval.js.map +1 -0
- package/dist/runtime-mode.d.ts +1 -1
- package/dist/runtime-mode.d.ts.map +1 -1
- package/dist/runtime-mode.js +2 -1
- package/dist/runtime-mode.js.map +1 -1
- package/dist/scope-key.d.ts +25 -0
- package/dist/scope-key.d.ts.map +1 -0
- package/dist/scope-key.js +34 -0
- package/dist/scope-key.js.map +1 -0
- package/dist/session.d.ts +47 -1
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +364 -35
- package/dist/session.js.map +1 -1
- package/dist/skills.d.ts +33 -0
- package/dist/skills.d.ts.map +1 -0
- package/dist/skills.js +65 -0
- package/dist/skills.js.map +1 -0
- package/dist/store.d.ts +36 -1
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +83 -0
- package/dist/store.js.map +1 -1
- package/dist/stream-chunks.d.ts +42 -0
- package/dist/stream-chunks.d.ts.map +1 -0
- package/dist/stream-chunks.js +138 -0
- package/dist/stream-chunks.js.map +1 -0
- package/dist/strict.d.ts +40 -11
- package/dist/strict.d.ts.map +1 -1
- package/dist/strict.js +22 -7
- package/dist/strict.js.map +1 -1
- package/dist/submission-context.d.ts +26 -0
- package/dist/submission-context.d.ts.map +1 -0
- package/dist/submission-context.js +20 -0
- package/dist/submission-context.js.map +1 -0
- package/dist/submission-runner.d.ts +150 -0
- package/dist/submission-runner.d.ts.map +1 -0
- package/dist/submission-runner.js +767 -0
- package/dist/submission-runner.js.map +1 -0
- package/dist/submission-state.d.ts +50 -0
- package/dist/submission-state.d.ts.map +1 -0
- package/dist/submission-state.js +82 -0
- package/dist/submission-state.js.map +1 -0
- package/dist/submission-store-contract.d.ts +31 -0
- package/dist/submission-store-contract.d.ts.map +1 -0
- package/dist/submission-store-contract.js +730 -0
- package/dist/submission-store-contract.js.map +1 -0
- package/dist/submission-store.d.ts +365 -0
- package/dist/submission-store.d.ts.map +1 -0
- package/dist/submission-store.js +191 -0
- package/dist/submission-store.js.map +1 -0
- package/dist/submission-telemetry.d.ts +56 -0
- package/dist/submission-telemetry.d.ts.map +1 -0
- package/dist/submission-telemetry.js +36 -0
- package/dist/submission-telemetry.js.map +1 -0
- package/dist/testing-contracts.d.ts +17 -0
- package/dist/testing-contracts.d.ts.map +1 -0
- package/dist/testing-contracts.js +14 -0
- package/dist/testing-contracts.js.map +1 -0
- package/dist/tools.d.ts +32 -4
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +132 -6
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +132 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +16 -8
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
export class ConversationStreamStoreError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
constructor(code, message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'ConversationStreamStoreError';
|
|
6
|
+
this.code = code;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export const CONVERSATION_STREAM_DEFAULT_READ_LIMIT = 100;
|
|
10
|
+
export const CONVERSATION_STREAM_MAX_READ_LIMIT = 1000;
|
|
11
|
+
export function clampReadLimit(limit) {
|
|
12
|
+
if (limit === undefined || !Number.isFinite(limit) || limit <= 0) {
|
|
13
|
+
return CONVERSATION_STREAM_DEFAULT_READ_LIMIT;
|
|
14
|
+
}
|
|
15
|
+
return Math.min(Math.floor(limit), CONVERSATION_STREAM_MAX_READ_LIMIT);
|
|
16
|
+
}
|
|
17
|
+
export function formatStreamOffset(offset) {
|
|
18
|
+
return String(offset);
|
|
19
|
+
}
|
|
20
|
+
export function parseStreamOffset(offset) {
|
|
21
|
+
if (offset === undefined || offset === '')
|
|
22
|
+
return 0;
|
|
23
|
+
const parsed = Number.parseInt(offset, 10);
|
|
24
|
+
if (!Number.isFinite(parsed) || parsed < 0)
|
|
25
|
+
return 0;
|
|
26
|
+
return parsed;
|
|
27
|
+
}
|
|
28
|
+
/** Stream path for a session's conversation projection. */
|
|
29
|
+
export function conversationStreamPath(storeSessionId) {
|
|
30
|
+
return `conversations/${storeSessionId}`;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Process-local listener registry shared by store implementations —
|
|
34
|
+
* registration, unsubscribe-and-prune, and error-swallowing notify.
|
|
35
|
+
*/
|
|
36
|
+
export class StreamListenerRegistry {
|
|
37
|
+
listeners = new Map();
|
|
38
|
+
subscribe(path, listener) {
|
|
39
|
+
let set = this.listeners.get(path);
|
|
40
|
+
if (!set) {
|
|
41
|
+
set = new Set();
|
|
42
|
+
this.listeners.set(path, set);
|
|
43
|
+
}
|
|
44
|
+
set.add(listener);
|
|
45
|
+
return () => {
|
|
46
|
+
set.delete(listener);
|
|
47
|
+
if (set.size === 0)
|
|
48
|
+
this.listeners.delete(path);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
notify(path) {
|
|
52
|
+
for (const listener of this.listeners.get(path) ?? []) {
|
|
53
|
+
try {
|
|
54
|
+
listener();
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// Listener failures never affect the writer.
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** In-memory conversation stream store (dev / `runtime: 'stateless'` / tests). */
|
|
63
|
+
export class InMemoryConversationStreamStore {
|
|
64
|
+
streams = new Map();
|
|
65
|
+
registry = new StreamListenerRegistry();
|
|
66
|
+
async createStream(path, identity) {
|
|
67
|
+
if (!this.streams.has(path)) {
|
|
68
|
+
this.streams.set(path, {
|
|
69
|
+
identity,
|
|
70
|
+
batches: [],
|
|
71
|
+
nextOffset: 0,
|
|
72
|
+
producerId: null,
|
|
73
|
+
producerEpoch: 0,
|
|
74
|
+
nextProducerSequence: 0,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async acquireProducer(path, producerId) {
|
|
79
|
+
const stream = this.requireStream(path);
|
|
80
|
+
stream.producerId = producerId;
|
|
81
|
+
stream.producerEpoch += 1;
|
|
82
|
+
stream.nextProducerSequence = 0;
|
|
83
|
+
return {
|
|
84
|
+
producerId,
|
|
85
|
+
producerEpoch: stream.producerEpoch,
|
|
86
|
+
nextProducerSequence: stream.nextProducerSequence,
|
|
87
|
+
offset: formatStreamOffset(stream.nextOffset),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
async append(input) {
|
|
91
|
+
const stream = this.requireStream(input.path);
|
|
92
|
+
if (input.producerId !== stream.producerId || input.producerEpoch !== stream.producerEpoch) {
|
|
93
|
+
throw new ConversationStreamStoreError('PRODUCER_FENCED', `Producer ${input.producerId}@${input.producerEpoch} is fenced; current is ${stream.producerId ?? 'none'}@${stream.producerEpoch}.`);
|
|
94
|
+
}
|
|
95
|
+
// Idempotent redelivery: identical coordinates return the committed offset.
|
|
96
|
+
const existing = stream.batches.find((batch) => batch.producerId === input.producerId &&
|
|
97
|
+
batch.producerEpoch === input.producerEpoch &&
|
|
98
|
+
batch.producerSequence === input.producerSequence);
|
|
99
|
+
if (existing)
|
|
100
|
+
return { offset: formatStreamOffset(existing.seq) };
|
|
101
|
+
if (input.producerSequence !== stream.nextProducerSequence) {
|
|
102
|
+
throw new ConversationStreamStoreError('SEQUENCE_CONFLICT', `Producer sequence ${input.producerSequence} does not match expected ${stream.nextProducerSequence}.`);
|
|
103
|
+
}
|
|
104
|
+
const seq = stream.nextOffset;
|
|
105
|
+
stream.batches.push({
|
|
106
|
+
seq,
|
|
107
|
+
producerId: input.producerId,
|
|
108
|
+
producerEpoch: input.producerEpoch,
|
|
109
|
+
producerSequence: input.producerSequence,
|
|
110
|
+
records: input.records.map((record) => structuredClone(record)),
|
|
111
|
+
});
|
|
112
|
+
stream.nextOffset = seq + 1;
|
|
113
|
+
stream.nextProducerSequence = input.producerSequence + 1;
|
|
114
|
+
this.registry.notify(input.path);
|
|
115
|
+
return { offset: formatStreamOffset(seq) };
|
|
116
|
+
}
|
|
117
|
+
async read(path, options) {
|
|
118
|
+
const stream = this.requireStream(path);
|
|
119
|
+
const from = parseStreamOffset(options?.offset);
|
|
120
|
+
const limit = clampReadLimit(options?.limit);
|
|
121
|
+
const selected = stream.batches.filter((batch) => batch.seq >= from).slice(0, limit);
|
|
122
|
+
const last = selected[selected.length - 1];
|
|
123
|
+
const nextOffset = last ? last.seq + 1 : from;
|
|
124
|
+
return {
|
|
125
|
+
batches: selected.map((batch) => ({
|
|
126
|
+
offset: formatStreamOffset(batch.seq),
|
|
127
|
+
records: batch.records.map((record) => structuredClone(record)),
|
|
128
|
+
})),
|
|
129
|
+
nextOffset: formatStreamOffset(nextOffset),
|
|
130
|
+
upToDate: nextOffset >= stream.nextOffset,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
async getMeta(path) {
|
|
134
|
+
const stream = this.streams.get(path);
|
|
135
|
+
if (!stream)
|
|
136
|
+
return null;
|
|
137
|
+
return {
|
|
138
|
+
identity: stream.identity,
|
|
139
|
+
nextOffset: formatStreamOffset(stream.nextOffset),
|
|
140
|
+
producerId: stream.producerId,
|
|
141
|
+
producerEpoch: stream.producerEpoch,
|
|
142
|
+
nextProducerSequence: stream.nextProducerSequence,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
async delete(path) {
|
|
146
|
+
this.streams.delete(path);
|
|
147
|
+
}
|
|
148
|
+
subscribe(path, listener) {
|
|
149
|
+
return this.registry.subscribe(path, listener);
|
|
150
|
+
}
|
|
151
|
+
requireStream(path) {
|
|
152
|
+
const stream = this.streams.get(path);
|
|
153
|
+
if (!stream) {
|
|
154
|
+
throw new ConversationStreamStoreError('STREAM_NOT_FOUND', `Conversation stream not found: ${path}`);
|
|
155
|
+
}
|
|
156
|
+
return stream;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=conversation-stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversation-stream.js","sourceRoot":"","sources":["../src/conversation-stream.ts"],"names":[],"mappings":"AAsGA,MAAM,OAAO,4BAA6B,SAAQ,KAAK;IAC5C,IAAI,CAGW;IAExB,YAAY,IAA0C,EAAE,OAAe;QACrE,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,8BAA8B,CAAC;QAC3C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAED,MAAM,CAAC,MAAM,sCAAsC,GAAG,GAAG,CAAC;AAC1D,MAAM,CAAC,MAAM,kCAAkC,GAAG,IAAI,CAAC;AAEvD,MAAM,UAAU,cAAc,CAAC,KAAyB;IACtD,IAAI,KAAK,KAAK,SAAS,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;QACjE,OAAO,sCAAsC,CAAC;IAChD,CAAC;IACD,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,kCAAkC,CAAC,CAAC;AACzE,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,MAAc;IAC/C,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC;AACxB,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,MAA0B;IAC1D,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,KAAK,EAAE;QAAE,OAAO,CAAC,CAAC;IACpD,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAC3C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC;QAAE,OAAO,CAAC,CAAC;IACrD,OAAO,MAAM,CAAC;AA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ClD,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,IAAY;QACvB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC;IAED,SAAS,CAAC,IAAY,EAAE,QAAoB;QAC1C,OAAO,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;IACjD,CAAC;IAEO,aAAa,CAAC,IAAY;QAChC,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACtC,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,4BAA4B,CAAC,kBAAkB,EAAE,kCAAkC,IAAI,EAAE,CAAC,CAAC;QACvG,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
|
package/dist/cost-budget.d.ts
CHANGED
|
@@ -94,6 +94,21 @@ export interface CostLimit {
|
|
|
94
94
|
* and pause until approved. Re-throw on denial.
|
|
95
95
|
*/
|
|
96
96
|
onExceed?: 'throw' | 'approve';
|
|
97
|
+
/**
|
|
98
|
+
* Whether the scope is tracked incrementally in-process or read from an
|
|
99
|
+
* external source. Defaults to `'incremental'`.
|
|
100
|
+
*/
|
|
101
|
+
scopeSource?: 'incremental' | 'external';
|
|
102
|
+
/**
|
|
103
|
+
* External source for actual (non-estimated) spend. When provided, the
|
|
104
|
+
* budget tracker can query real-time cost data from the provider.
|
|
105
|
+
*/
|
|
106
|
+
actualSource?: ActualCostSource;
|
|
107
|
+
/**
|
|
108
|
+
* TTL for caching actual cost lookups (milliseconds). When omitted, the
|
|
109
|
+
* actual source's default or per-query option is used.
|
|
110
|
+
*/
|
|
111
|
+
actualsCacheTtlMs?: number;
|
|
97
112
|
}
|
|
98
113
|
export interface CostLimitContext {
|
|
99
114
|
scope: 'call' | 'session' | 'cross-process';
|
|
@@ -101,6 +116,85 @@ export interface CostLimitContext {
|
|
|
101
116
|
limitUsd: number;
|
|
102
117
|
/** External scope key, populated for `scope: 'cross-process'`. */
|
|
103
118
|
scopeKey?: string;
|
|
119
|
+
/** ISO timestamp when actual cost data was last fetched from the provider. */
|
|
120
|
+
actualsFetchedAt?: string;
|
|
121
|
+
/** TTL (in ms) for the cached actual cost data used in this observation. */
|
|
122
|
+
cacheTtlMs?: number;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Attribution dimensions for a single cost observation. All fields are
|
|
126
|
+
* optional so callers can tag as much or as little metadata as they have.
|
|
127
|
+
*/
|
|
128
|
+
export interface CostAttribution {
|
|
129
|
+
agentId?: string;
|
|
130
|
+
userId?: string;
|
|
131
|
+
tenantId?: string;
|
|
132
|
+
model?: string;
|
|
133
|
+
provider?: string;
|
|
134
|
+
sessionId?: string;
|
|
135
|
+
turnId?: string;
|
|
136
|
+
[key: string]: string | undefined;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Query filters for retrieving aggregated cost attribution rows.
|
|
140
|
+
*/
|
|
141
|
+
export interface AttributionQuery {
|
|
142
|
+
agentId?: string;
|
|
143
|
+
userId?: string;
|
|
144
|
+
tenantId?: string;
|
|
145
|
+
model?: string;
|
|
146
|
+
provider?: string;
|
|
147
|
+
/** ISO date string (inclusive). */
|
|
148
|
+
startDate?: string;
|
|
149
|
+
/** ISO date string (inclusive). */
|
|
150
|
+
endDate?: string;
|
|
151
|
+
/** Dimensions to group by. Defaults to all if omitted. */
|
|
152
|
+
groupBy?: Array<keyof CostAttribution>;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* A single row of aggregated cost attribution data.
|
|
156
|
+
*/
|
|
157
|
+
export interface CostAttributionRow {
|
|
158
|
+
agentId?: string;
|
|
159
|
+
userId?: string;
|
|
160
|
+
tenantId?: string;
|
|
161
|
+
model?: string;
|
|
162
|
+
provider?: string;
|
|
163
|
+
/** Period identifier, e.g. `day:2026-06-23`. */
|
|
164
|
+
period: string;
|
|
165
|
+
/** Estimated cost from the static price table. */
|
|
166
|
+
estimatedUsd: number;
|
|
167
|
+
/** Actual cost from the provider's billing API. */
|
|
168
|
+
actualUsd: number;
|
|
169
|
+
/** Actual DBU (Databricks Unit) spend, if applicable. */
|
|
170
|
+
actualDbus: number;
|
|
171
|
+
/** Difference between estimated and actual cost. */
|
|
172
|
+
deltaUsd: number;
|
|
173
|
+
/** Number of calls in this aggregation. */
|
|
174
|
+
calls: number;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* External source for actual (non-estimated) spend. Implementations can
|
|
178
|
+
* query provider billing APIs, Databricks usage tables, or other real-time
|
|
179
|
+
* cost data.
|
|
180
|
+
*/
|
|
181
|
+
export interface ActualCostSource {
|
|
182
|
+
/**
|
|
183
|
+
* Fetch the actual USD spend for a scope, optionally filtered by
|
|
184
|
+
* attribution dimensions.
|
|
185
|
+
*/
|
|
186
|
+
getUsd(scopeKey: string, attribution?: CostAttribution, options?: {
|
|
187
|
+
cacheTtlMs?: number;
|
|
188
|
+
}): Promise<{
|
|
189
|
+
usd: number;
|
|
190
|
+
dbus?: number;
|
|
191
|
+
actualsFetchedAt?: string;
|
|
192
|
+
cacheTtlMs?: number;
|
|
193
|
+
}>;
|
|
194
|
+
/**
|
|
195
|
+
* Query aggregated cost attribution rows for reporting or debugging.
|
|
196
|
+
*/
|
|
197
|
+
queryAttribution(filters: AttributionQuery): Promise<CostAttributionRow[]>;
|
|
104
198
|
}
|
|
105
199
|
export declare class CostLimitExceededError extends FabricError {
|
|
106
200
|
readonly context: CostLimitContext;
|
|
@@ -112,7 +206,11 @@ export declare class CostLimitExceededError extends FabricError {
|
|
|
112
206
|
export declare class CostBudgetTracker {
|
|
113
207
|
private readonly limit;
|
|
114
208
|
private sessionTotalUsd;
|
|
209
|
+
private pendingScopeViolation;
|
|
210
|
+
private defaultStore;
|
|
115
211
|
constructor(limit: CostLimit | undefined);
|
|
212
|
+
/** How `perScope` is enforced: `'incremental'` (estimate counter) or `'external'` (actual-cost source). */
|
|
213
|
+
get scopeSource(): 'incremental' | 'external';
|
|
116
214
|
/** Has any budget configured. */
|
|
117
215
|
enabled(): boolean;
|
|
118
216
|
/**
|
|
@@ -125,8 +223,15 @@ export declare class CostBudgetTracker {
|
|
|
125
223
|
* Cross-process scope check. Returns a violation when the scope's running
|
|
126
224
|
* total exceeds `perScope` after incrementing by `callUsd`. No-op when
|
|
127
225
|
* `perScope`/`scopeKey` are not configured.
|
|
226
|
+
*
|
|
227
|
+
* For `scopeSource === 'incremental'` (default), increments the store and
|
|
228
|
+
* compares. For `scopeSource === 'external'`, queries `actualSource` instead.
|
|
128
229
|
*/
|
|
129
|
-
observeScopeAsync(callUsd: number): Promise<CostLimitContext | undefined>;
|
|
230
|
+
observeScopeAsync(callUsd: number, attribution?: CostAttribution): Promise<CostLimitContext | undefined>;
|
|
231
|
+
/** Store a pending scope violation for asynchronous reconciliation. */
|
|
232
|
+
setPendingScopeViolation(violation: CostLimitContext): void;
|
|
233
|
+
/** Take and clear the pending scope violation, if any. */
|
|
234
|
+
takePendingScopeViolation(): CostLimitContext | undefined;
|
|
130
235
|
/** Total spend so far this session. */
|
|
131
236
|
total(): number;
|
|
132
237
|
onExceed(): 'throw' | 'approve';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cost-budget.d.ts","sourceRoot":"","sources":["../src/cost-budget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC;;;;;;;;;;;;;;;;GAgBG;AACH;;;;;;;;GAQG;AACH,MAAM,WAAW,eAAe;IAC9B,uEAAuE;IACvE,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/D,2CAA2C;IAC3C,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACvC,yDAAyD;IACzD,KAAK,CAAC,CAAC,KAAK,EAAE,MAA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AAG,gBAAgB,GAAG,SAAS;IAYtD;;;;;;;OAOG;IACG,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC;IAoC9G,uEAAuE;IACvE,wBAAwB,CAAC,SAAS,EAAE,gBAAgB,GAAG,IAAI;IAI3D,0DAA0D;IAC1D,yBAAyB,IAAI,gBAAgB,GAAG,SAAS;IAMzD,uCAAuC;IACvC,KAAK,IAAI,MAAM;IAIf,QAAQ,IAAI,OAAO,GAAG,SAAS;CAGhC"}
|
package/dist/cost-budget.js
CHANGED
|
@@ -86,9 +86,15 @@ export class CostLimitExceededError extends FabricError {
|
|
|
86
86
|
export class CostBudgetTracker {
|
|
87
87
|
limit;
|
|
88
88
|
sessionTotalUsd = 0;
|
|
89
|
+
pendingScopeViolation;
|
|
90
|
+
defaultStore;
|
|
89
91
|
constructor(limit) {
|
|
90
92
|
this.limit = limit;
|
|
91
93
|
}
|
|
94
|
+
/** How `perScope` is enforced: `'incremental'` (estimate counter) or `'external'` (actual-cost source). */
|
|
95
|
+
get scopeSource() {
|
|
96
|
+
return this.limit?.scopeSource ?? 'incremental';
|
|
97
|
+
}
|
|
92
98
|
/** Has any budget configured. */
|
|
93
99
|
enabled() {
|
|
94
100
|
return Boolean(this.limit && (this.limit.perCall !== undefined || this.limit.perSession !== undefined || this.limit.perScope !== undefined));
|
|
@@ -114,17 +120,57 @@ export class CostBudgetTracker {
|
|
|
114
120
|
* Cross-process scope check. Returns a violation when the scope's running
|
|
115
121
|
* total exceeds `perScope` after incrementing by `callUsd`. No-op when
|
|
116
122
|
* `perScope`/`scopeKey` are not configured.
|
|
123
|
+
*
|
|
124
|
+
* For `scopeSource === 'incremental'` (default), increments the store and
|
|
125
|
+
* compares. For `scopeSource === 'external'`, queries `actualSource` instead.
|
|
117
126
|
*/
|
|
118
|
-
async observeScopeAsync(callUsd) {
|
|
127
|
+
async observeScopeAsync(callUsd, attribution) {
|
|
119
128
|
if (!this.limit?.perScope || !this.limit.scopeKey || callUsd <= 0)
|
|
120
129
|
return undefined;
|
|
121
|
-
const
|
|
130
|
+
const scopeSource = this.limit.scopeSource ?? 'incremental';
|
|
131
|
+
if (scopeSource === 'external') {
|
|
132
|
+
if (!this.limit.actualSource)
|
|
133
|
+
return undefined;
|
|
134
|
+
const opts = {};
|
|
135
|
+
if (this.limit.actualsCacheTtlMs !== undefined)
|
|
136
|
+
opts.cacheTtlMs = this.limit.actualsCacheTtlMs;
|
|
137
|
+
const result = await this.limit.actualSource.getUsd(this.limit.scopeKey, attribution, opts);
|
|
138
|
+
if (result.usd > this.limit.perScope) {
|
|
139
|
+
const ctx = {
|
|
140
|
+
scope: 'cross-process',
|
|
141
|
+
observedUsd: result.usd,
|
|
142
|
+
limitUsd: this.limit.perScope,
|
|
143
|
+
scopeKey: this.limit.scopeKey,
|
|
144
|
+
};
|
|
145
|
+
if (result.actualsFetchedAt)
|
|
146
|
+
ctx.actualsFetchedAt = result.actualsFetchedAt;
|
|
147
|
+
if (result.cacheTtlMs !== undefined)
|
|
148
|
+
ctx.cacheTtlMs = result.cacheTtlMs;
|
|
149
|
+
return ctx;
|
|
150
|
+
}
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
// incremental (default) — reuse a single default store instance per tracker
|
|
154
|
+
if (!this.defaultStore) {
|
|
155
|
+
this.defaultStore = this.limit.store ?? inMemoryCostBudgetStore();
|
|
156
|
+
}
|
|
157
|
+
const store = this.defaultStore;
|
|
122
158
|
const total = await store.incrementUsd(this.limit.scopeKey, callUsd);
|
|
123
159
|
if (total > this.limit.perScope) {
|
|
124
160
|
return { scope: 'cross-process', observedUsd: total, limitUsd: this.limit.perScope, scopeKey: this.limit.scopeKey };
|
|
125
161
|
}
|
|
126
162
|
return undefined;
|
|
127
163
|
}
|
|
164
|
+
/** Store a pending scope violation for asynchronous reconciliation. */
|
|
165
|
+
setPendingScopeViolation(violation) {
|
|
166
|
+
this.pendingScopeViolation = violation;
|
|
167
|
+
}
|
|
168
|
+
/** Take and clear the pending scope violation, if any. */
|
|
169
|
+
takePendingScopeViolation() {
|
|
170
|
+
const v = this.pendingScopeViolation;
|
|
171
|
+
this.pendingScopeViolation = undefined;
|
|
172
|
+
return v;
|
|
173
|
+
}
|
|
128
174
|
/** Total spend so far this session. */
|
|
129
175
|
total() {
|
|
130
176
|
return this.sessionTotalUsd;
|
package/dist/cost-budget.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cost-budget.js","sourceRoot":"","sources":["../src/cost-budget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAqCzC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,eAAe,CAC7B,QAAgB,EAChB,OAAwB;IAExB,IAAI,CAAC,QAAQ;QAAE,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IACzE,MAAM,UAAU,GAAwC,EAAE,CAAC;IAC3D,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,IAAI,IAAI,EAAE,CAAC;IACtC,MAAM,IAAI,GAAG,GAAG,CAAC,cAAc,EAAE,CAAC;IAClC,MAAM,EAAE,GAAG,MAAM,CAAC,GAAG,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAC1D,MAAM,EAAE,GAAG,MAAM,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IACrD,MAAM,EAAE,GAAG,MAAM,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IACtD,IAAI,OAAO,CAAC,UAAU,KAAK,SAAS;QAAE,UAAU,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,OAAO,CAA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AAE,CAAC;YACvB,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,IAAI,uBAAuB,EAAE,CAAC;QACpE,CAAC;QACD,MAAM,KAAK,GAAG,IAAI,CAAC,YAAY,CAAC;QAChC,MAAM,KAAK,GAAG,MAAM,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QACrE,IAAI,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;YAChC,OAAO,EAAE,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,KAAK,EAAE,QAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;QACtH,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,uEAAuE;IACvE,wBAAwB,CAAC,SAA2B;QAClD,IAAI,CAAC,qBAAqB,GAAG,SAAS,CAAC;IACzC,CAAC;IAED,0DAA0D;IAC1D,yBAAyB;QACvB,MAAM,CAAC,GAAG,IAAI,CAAC,qBAAqB,CAAC;QACrC,IAAI,CAAC,qBAAqB,GAAG,SAAS,CAAC;QACvC,OAAO,CAAC,CAAC;IACX,CAAC;IAED,uCAAuC;IACvC,KAAK;QACH,OAAO,IAAI,CAAC,eAAe,CAAC;IAC9B,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,KAAK,EAAE,QAAQ,IAAI,OAAO,CAAC;IACzC,CAAC;CACF"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { SignalEntryData } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* DeliveredMessage — the single unified input shape for everything that
|
|
4
|
+
* enters a persistent agent's session: direct HTTP prompts, dispatch,
|
|
5
|
+
* channels/webhooks, Databricks events, SDK clients, and tests.
|
|
6
|
+
*
|
|
7
|
+
* `kind: 'user'` is a direct user talking to the assistant (1:1 chat
|
|
8
|
+
* surface), optionally carrying attachments.
|
|
9
|
+
*
|
|
10
|
+
* `kind: 'signal'` models everything beyond that direct exchange — a Slack
|
|
11
|
+
* thread or a Lakeflow job event is activity the agent observes, not the
|
|
12
|
+
* assistant's own user speaking. Sender identity and structured metadata go
|
|
13
|
+
* in `attributes`; the message itself in `body`. Signals render into model
|
|
14
|
+
* context as an XML envelope (`<tagName ...attributes>body</tagName>`).
|
|
15
|
+
*
|
|
16
|
+
* `body` (not `text`/`content`) is named for headroom — a future revision may
|
|
17
|
+
* accept a real JSON value and stringify internally without a field rename.
|
|
18
|
+
*/
|
|
19
|
+
export type DeliveredMessage = {
|
|
20
|
+
kind: 'user';
|
|
21
|
+
body: string;
|
|
22
|
+
attachments?: DeliveredAttachment[];
|
|
23
|
+
} | {
|
|
24
|
+
kind: 'signal';
|
|
25
|
+
/** Caller-defined event/signal type, e.g. `'slack.message'` or `'databricks.job_run'`. */
|
|
26
|
+
type: string;
|
|
27
|
+
body: string;
|
|
28
|
+
attributes?: Record<string, string>;
|
|
29
|
+
tagName?: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* One attachment on a `kind: 'user'` message. Today the only supported
|
|
33
|
+
* attachment is an image, carried either inline (`data`, base64) or as a
|
|
34
|
+
* durable content-addressed reference (`ref`) once an attachment store is
|
|
35
|
+
* configured — admission materializes inline bytes into refs. An attachment
|
|
36
|
+
* must carry `data` or `ref` (or both, transiently during materialization).
|
|
37
|
+
*/
|
|
38
|
+
export interface DeliveredAttachment {
|
|
39
|
+
type: 'image';
|
|
40
|
+
/** Base64-encoded bytes (inline form). */
|
|
41
|
+
data?: string;
|
|
42
|
+
mimeType: string;
|
|
43
|
+
/** Uploader-provided display name; not part of byte identity. */
|
|
44
|
+
filename?: string;
|
|
45
|
+
/** Durable content-addressed reference (materialized form). */
|
|
46
|
+
ref?: DeliveredAttachmentRef;
|
|
47
|
+
}
|
|
48
|
+
/** Durable reference to attachment bytes in an attachment store. */
|
|
49
|
+
export interface DeliveredAttachmentRef {
|
|
50
|
+
/** Attachment id assigned at materialization. */
|
|
51
|
+
id: string;
|
|
52
|
+
/** Lowercase hex SHA-256 of the bytes — the content address. */
|
|
53
|
+
digest: string;
|
|
54
|
+
sizeBytes: number;
|
|
55
|
+
}
|
|
56
|
+
/** Maximum accepted base64 length for a single inline attachment. */
|
|
57
|
+
export declare const MAX_ATTACHMENT_DATA_LENGTH: number;
|
|
58
|
+
/** Thrown by {@link parseDeliveredMessage} on malformed input. */
|
|
59
|
+
export declare class InvalidDeliveredMessageError extends Error {
|
|
60
|
+
readonly code = "INVALID_DELIVERED_MESSAGE";
|
|
61
|
+
constructor(reason: string);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Validate a raw value as a {@link DeliveredMessage}. Shared by dispatch
|
|
65
|
+
* admission and the direct HTTP route so every transport produces the same
|
|
66
|
+
* structured error on bad input.
|
|
67
|
+
*/
|
|
68
|
+
export declare function parseDeliveredMessage(value: unknown): DeliveredMessage;
|
|
69
|
+
/** True when a raw value already looks like a {@link DeliveredMessage} (has a valid `kind`). */
|
|
70
|
+
export declare function isDeliveredMessageShape(value: unknown): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Normalize legacy inputs into a {@link DeliveredMessage}:
|
|
73
|
+
* - a string → a user message with that body
|
|
74
|
+
* - a value with a `kind` discriminator → validated as a DeliveredMessage
|
|
75
|
+
* - any other JSON value → a user message with the JSON-stringified body
|
|
76
|
+
* (matching the historical dispatch rendering, so behavior is unchanged
|
|
77
|
+
* for pre-DeliveredMessage callers)
|
|
78
|
+
*/
|
|
79
|
+
export declare function normalizeDeliveredMessage(input: unknown): DeliveredMessage;
|
|
80
|
+
/**
|
|
81
|
+
* Render a delivered message to the prompt text form. User messages pass
|
|
82
|
+
* their body through verbatim; signals render as their XML envelope via the
|
|
83
|
+
* shared {@link renderSignalMessage} machinery.
|
|
84
|
+
*/
|
|
85
|
+
export declare function renderDeliveredMessage(message: DeliveredMessage): string;
|
|
86
|
+
/** Map a signal-kind message onto the persisted `signal` entry's data shape. */
|
|
87
|
+
export declare function deliveredSignalToEntryData(message: Extract<DeliveredMessage, {
|
|
88
|
+
kind: 'signal';
|
|
89
|
+
}>): SignalEntryData;
|
|
90
|
+
//# sourceMappingURL=delivered-message.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delivered-message.d.ts","sourceRoot":"","sources":["../src/delivered-message.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAGlD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,gBAAgB,GACxB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,mBAAmB,EAAE,CAAA;CAAE,GACnE;IACE,IAAI,EAAE,QAAQ,CAAC;IACf,0FAA0F;IAC1F,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEN;;;;;;GAMG;AACH,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,OAAO,CAAC;IACd,0CAA0C;IAC1C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,iEAAiE;IACjE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,+DAA+D;IAC/D,GAAG,CAAC,EAAE,sBAAsB,CAAC;CAC9B;AAED,oEAAoE;AACpE,MAAM,WAAW,sBAAsB;IACrC,iDAAiD;IACjD,EAAE,EAAE,MAAM,CAAC;IACX,gEAAgE;IAChE,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,qEAAqE;AACrE,eAAO,MAAM,0BAA0B,QAA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