@buoy-gg/agent-core 7.0.41 → 7.0.43
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/lib/commonjs/blocks/receipts.js +1 -761
- package/lib/commonjs/blocks/runId.js +1 -31
- package/lib/commonjs/blocks/types.js +1 -140
- package/lib/commonjs/blocks/uiTool.js +6 -1405
- package/lib/commonjs/catalog/catalog.g.js +5 -4556
- package/lib/commonjs/catalog/catalog.source.json +249 -0
- package/lib/commonjs/catalog/catalog.types.g.js +1 -46
- package/lib/commonjs/catalog/normalizeParams.js +2 -332
- package/lib/commonjs/catalog/signature.js +1 -68
- package/lib/commonjs/catalog/snapshotReads.js +1 -263
- package/lib/commonjs/catalog/toProviderTools.js +2 -145
- package/lib/commonjs/catalog/validateParams.js +1 -129
- package/lib/commonjs/context/buildContextPack.js +1 -489
- package/lib/commonjs/effects/digest.js +1 -34
- package/lib/commonjs/effects/ledger.js +1 -345
- package/lib/commonjs/engine/askGate.js +1 -287
- package/lib/commonjs/engine/effectFor.js +1 -223
- package/lib/commonjs/engine/evidence.js +3 -111
- package/lib/commonjs/engine/historyBudget.js +1 -363
- package/lib/commonjs/engine/retrieve.js +1 -214
- package/lib/commonjs/engine/runAgentTurn.js +8 -1836
- package/lib/commonjs/engine/systemPrompt.js +54 -163
- package/lib/commonjs/engine/textToolCalls.js +1 -276
- package/lib/commonjs/engine/tokenCalibration.js +1 -81
- package/lib/commonjs/engine/verify.js +4 -297
- package/lib/commonjs/index.js +1 -384
- package/lib/commonjs/policy/labels.js +1 -319
- package/lib/commonjs/policy/policy.js +1 -148
- package/lib/commonjs/policy/redact.js +1 -172
- package/lib/commonjs/providers/anthropic.js +3 -445
- package/lib/commonjs/providers/openai.js +4 -324
- package/lib/commonjs/providers/problem.js +1 -98
- package/lib/commonjs/providers/sse.js +1 -240
- package/lib/commonjs/providers/streamTimer.js +1 -123
- package/lib/commonjs/providers/transport.js +1 -123
- package/lib/commonjs/providers/types.js +1 -6
- package/lib/commonjs/providers/xhrStream.js +1 -212
- package/lib/commonjs/realNow.js +1 -0
- package/lib/commonjs/session.js +1 -393
- package/lib/commonjs/types.js +0 -1
- package/lib/module/blocks/receipts.js +1 -755
- package/lib/module/blocks/runId.js +1 -26
- package/lib/module/blocks/types.js +1 -134
- package/lib/module/blocks/uiTool.js +6 -1396
- package/lib/module/catalog/catalog.g.js +5 -4552
- package/lib/module/catalog/catalog.source.json +249 -0
- package/lib/module/catalog/catalog.types.g.js +1 -42
- package/lib/module/catalog/normalizeParams.js +2 -326
- package/lib/module/catalog/signature.js +1 -63
- package/lib/module/catalog/snapshotReads.js +1 -259
- package/lib/module/catalog/toProviderTools.js +2 -139
- package/lib/module/catalog/validateParams.js +1 -124
- package/lib/module/context/buildContextPack.js +1 -485
- package/lib/module/effects/digest.js +1 -29
- package/lib/module/effects/ledger.js +1 -339
- package/lib/module/engine/askGate.js +1 -280
- package/lib/module/engine/effectFor.js +1 -220
- package/lib/module/engine/evidence.js +3 -105
- package/lib/module/engine/historyBudget.js +1 -355
- package/lib/module/engine/retrieve.js +1 -210
- package/lib/module/engine/runAgentTurn.js +8 -1827
- package/lib/module/engine/systemPrompt.js +54 -157
- package/lib/module/engine/textToolCalls.js +1 -270
- package/lib/module/engine/tokenCalibration.js +1 -75
- package/lib/module/engine/verify.js +4 -289
- package/lib/module/index.js +1 -35
- package/lib/module/policy/labels.js +1 -312
- package/lib/module/policy/policy.js +1 -142
- package/lib/module/policy/redact.js +1 -165
- package/lib/module/providers/anthropic.js +3 -441
- package/lib/module/providers/openai.js +4 -320
- package/lib/module/providers/problem.js +1 -92
- package/lib/module/providers/sse.js +1 -231
- package/lib/module/providers/streamTimer.js +1 -118
- package/lib/module/providers/transport.js +1 -119
- package/lib/module/providers/types.js +1 -4
- package/lib/module/providers/xhrStream.js +1 -207
- package/lib/module/realNow.js +1 -0
- package/lib/module/session.js +1 -369
- package/lib/module/types.js +0 -1
- package/lib/typescript/catalog/catalog.g.d.ts +3 -3
- package/lib/typescript/catalog/catalog.types.g.d.ts +3 -2
- package/lib/typescript/providers/problem.d.ts +0 -20
- package/lib/typescript/providers/streamTimer.d.ts +0 -24
- package/lib/typescript/realNow.d.ts +18 -0
- package/lib/web/index.mjs +52 -53
- package/package.json +1 -1
- package/lib/commonjs/blocks/receipts.js.map +0 -1
- package/lib/commonjs/blocks/runId.js.map +0 -1
- package/lib/commonjs/blocks/types.js.map +0 -1
- package/lib/commonjs/blocks/uiTool.js.map +0 -1
- package/lib/commonjs/catalog/catalog.g.js.map +0 -1
- package/lib/commonjs/catalog/catalog.types.g.js.map +0 -1
- package/lib/commonjs/catalog/normalizeParams.js.map +0 -1
- package/lib/commonjs/catalog/signature.js.map +0 -1
- package/lib/commonjs/catalog/snapshotReads.js.map +0 -1
- package/lib/commonjs/catalog/toProviderTools.js.map +0 -1
- package/lib/commonjs/catalog/validateParams.js.map +0 -1
- package/lib/commonjs/context/buildContextPack.js.map +0 -1
- package/lib/commonjs/effects/digest.js.map +0 -1
- package/lib/commonjs/effects/ledger.js.map +0 -1
- package/lib/commonjs/engine/askGate.js.map +0 -1
- package/lib/commonjs/engine/effectFor.js.map +0 -1
- package/lib/commonjs/engine/evidence.js.map +0 -1
- package/lib/commonjs/engine/historyBudget.js.map +0 -1
- package/lib/commonjs/engine/retrieve.js.map +0 -1
- package/lib/commonjs/engine/runAgentTurn.js.map +0 -1
- package/lib/commonjs/engine/systemPrompt.js.map +0 -1
- package/lib/commonjs/engine/textToolCalls.js.map +0 -1
- package/lib/commonjs/engine/tokenCalibration.js.map +0 -1
- package/lib/commonjs/engine/verify.js.map +0 -1
- package/lib/commonjs/index.js.map +0 -1
- package/lib/commonjs/policy/labels.js.map +0 -1
- package/lib/commonjs/policy/policy.js.map +0 -1
- package/lib/commonjs/policy/redact.js.map +0 -1
- package/lib/commonjs/providers/anthropic.js.map +0 -1
- package/lib/commonjs/providers/openai.js.map +0 -1
- package/lib/commonjs/providers/problem.js.map +0 -1
- package/lib/commonjs/providers/sse.js.map +0 -1
- package/lib/commonjs/providers/streamTimer.js.map +0 -1
- package/lib/commonjs/providers/transport.js.map +0 -1
- package/lib/commonjs/providers/types.js.map +0 -1
- package/lib/commonjs/providers/xhrStream.js.map +0 -1
- package/lib/commonjs/session.js.map +0 -1
- package/lib/commonjs/types.js.map +0 -1
- package/lib/module/blocks/receipts.js.map +0 -1
- package/lib/module/blocks/runId.js.map +0 -1
- package/lib/module/blocks/types.js.map +0 -1
- package/lib/module/blocks/uiTool.js.map +0 -1
- package/lib/module/catalog/catalog.g.js.map +0 -1
- package/lib/module/catalog/catalog.types.g.js.map +0 -1
- package/lib/module/catalog/normalizeParams.js.map +0 -1
- package/lib/module/catalog/signature.js.map +0 -1
- package/lib/module/catalog/snapshotReads.js.map +0 -1
- package/lib/module/catalog/toProviderTools.js.map +0 -1
- package/lib/module/catalog/validateParams.js.map +0 -1
- package/lib/module/context/buildContextPack.js.map +0 -1
- package/lib/module/effects/digest.js.map +0 -1
- package/lib/module/effects/ledger.js.map +0 -1
- package/lib/module/engine/askGate.js.map +0 -1
- package/lib/module/engine/effectFor.js.map +0 -1
- package/lib/module/engine/evidence.js.map +0 -1
- package/lib/module/engine/historyBudget.js.map +0 -1
- package/lib/module/engine/retrieve.js.map +0 -1
- package/lib/module/engine/runAgentTurn.js.map +0 -1
- package/lib/module/engine/systemPrompt.js.map +0 -1
- package/lib/module/engine/textToolCalls.js.map +0 -1
- package/lib/module/engine/tokenCalibration.js.map +0 -1
- package/lib/module/engine/verify.js.map +0 -1
- package/lib/module/index.js.map +0 -1
- package/lib/module/policy/labels.js.map +0 -1
- package/lib/module/policy/policy.js.map +0 -1
- package/lib/module/policy/redact.js.map +0 -1
- package/lib/module/providers/anthropic.js.map +0 -1
- package/lib/module/providers/openai.js.map +0 -1
- package/lib/module/providers/problem.js.map +0 -1
- package/lib/module/providers/sse.js.map +0 -1
- package/lib/module/providers/streamTimer.js.map +0 -1
- package/lib/module/providers/transport.js.map +0 -1
- package/lib/module/providers/types.js.map +0 -1
- package/lib/module/providers/xhrStream.js.map +0 -1
- package/lib/module/session.js.map +0 -1
- package/lib/module/types.js.map +0 -1
- package/lib/typescript/blocks/receipts.d.ts.map +0 -1
- package/lib/typescript/blocks/runId.d.ts.map +0 -1
- package/lib/typescript/blocks/types.d.ts.map +0 -1
- package/lib/typescript/blocks/uiTool.d.ts.map +0 -1
- package/lib/typescript/catalog/catalog.g.d.ts.map +0 -1
- package/lib/typescript/catalog/catalog.types.g.d.ts.map +0 -1
- package/lib/typescript/catalog/normalizeParams.d.ts.map +0 -1
- package/lib/typescript/catalog/signature.d.ts.map +0 -1
- package/lib/typescript/catalog/snapshotReads.d.ts.map +0 -1
- package/lib/typescript/catalog/toProviderTools.d.ts.map +0 -1
- package/lib/typescript/catalog/validateParams.d.ts.map +0 -1
- package/lib/typescript/context/buildContextPack.d.ts.map +0 -1
- package/lib/typescript/effects/digest.d.ts.map +0 -1
- package/lib/typescript/effects/ledger.d.ts.map +0 -1
- package/lib/typescript/engine/askGate.d.ts.map +0 -1
- package/lib/typescript/engine/effectFor.d.ts.map +0 -1
- package/lib/typescript/engine/evidence.d.ts.map +0 -1
- package/lib/typescript/engine/historyBudget.d.ts.map +0 -1
- package/lib/typescript/engine/retrieve.d.ts.map +0 -1
- package/lib/typescript/engine/runAgentTurn.d.ts.map +0 -1
- package/lib/typescript/engine/systemPrompt.d.ts.map +0 -1
- package/lib/typescript/engine/textToolCalls.d.ts.map +0 -1
- package/lib/typescript/engine/tokenCalibration.d.ts.map +0 -1
- package/lib/typescript/engine/verify.d.ts.map +0 -1
- package/lib/typescript/index.d.ts.map +0 -1
- package/lib/typescript/policy/labels.d.ts.map +0 -1
- package/lib/typescript/policy/policy.d.ts.map +0 -1
- package/lib/typescript/policy/redact.d.ts.map +0 -1
- package/lib/typescript/providers/anthropic.d.ts.map +0 -1
- package/lib/typescript/providers/openai.d.ts.map +0 -1
- package/lib/typescript/providers/problem.d.ts.map +0 -1
- package/lib/typescript/providers/sse.d.ts.map +0 -1
- package/lib/typescript/providers/streamTimer.d.ts.map +0 -1
- package/lib/typescript/providers/transport.d.ts.map +0 -1
- package/lib/typescript/providers/types.d.ts.map +0 -1
- package/lib/typescript/providers/xhrStream.d.ts.map +0 -1
- package/lib/typescript/session.d.ts.map +0 -1
- package/lib/typescript/types.d.ts.map +0 -1
package/lib/commonjs/session.js
CHANGED
|
@@ -1,393 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
Object.defineProperty(exports, "compressConsumed", {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: function () {
|
|
9
|
-
return _historyBudget.compressConsumed;
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
exports.createAskBuoySession = createAskBuoySession;
|
|
13
|
-
exports.resumableHistory = resumableHistory;
|
|
14
|
-
Object.defineProperty(exports, "trimHistory", {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
get: function () {
|
|
17
|
-
return _historyBudget.trimHistory;
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
Object.defineProperty(exports, "trimHistoryReport", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
get: function () {
|
|
23
|
-
return _historyBudget.trimHistoryReport;
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
var _catalog = require("./catalog/catalog.g");
|
|
27
|
-
var _buildContextPack = require("./context/buildContextPack");
|
|
28
|
-
var _runAgentTurn = require("./engine/runAgentTurn");
|
|
29
|
-
var _systemPrompt = require("./engine/systemPrompt");
|
|
30
|
-
var _ledger = require("./effects/ledger");
|
|
31
|
-
var _evidence = require("./engine/evidence");
|
|
32
|
-
var _tokenCalibration = require("./engine/tokenCalibration");
|
|
33
|
-
var _historyBudget = require("./engine/historyBudget");
|
|
34
|
-
var _anthropic = require("./providers/anthropic");
|
|
35
|
-
var _openai = require("./providers/openai");
|
|
36
|
-
var _policy = require("./policy/policy");
|
|
37
|
-
var _redact = require("./policy/redact");
|
|
38
|
-
/**
|
|
39
|
-
* A conversation, with everything wired up.
|
|
40
|
-
*
|
|
41
|
-
* This is the one-call surface the UI packages use. It owns the pieces that
|
|
42
|
-
* have to agree with each other across a whole session — the provider, the
|
|
43
|
-
* catalog filtered to what this device can actually do, the system prompt
|
|
44
|
-
* (the stable part built once after grounding, the RIGHT NOW part re-read every
|
|
45
|
-
* turn), the effect ledger, and the message history —
|
|
46
|
-
* so a chat UI is a message list plus a text box rather than an orchestrator.
|
|
47
|
-
*/
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* A stored history, made safe to send as the start of a new request.
|
|
51
|
-
*
|
|
52
|
-
* Two things make a saved conversation unusable as-is.
|
|
53
|
-
*
|
|
54
|
-
* A history captured mid-turn ends with an assistant message whose `tool_use`
|
|
55
|
-
* blocks were never answered, or with the `tool_result`s and no reply after
|
|
56
|
-
* them. Every provider rejects both: the blocks must pair up. So this rewinds
|
|
57
|
-
* to the last COMPLETE round — an assistant message that made no tool calls,
|
|
58
|
-
* which is where a conversation is always safe to resume from.
|
|
59
|
-
*
|
|
60
|
-
* And thinking blocks are dropped. Anthropic needs them replayed only when the
|
|
61
|
-
* very next thing sent is that turn's `tool_result`s; a restored conversation
|
|
62
|
-
* always resumes with a fresh user message instead, so their signatures are
|
|
63
|
-
* dead weight at best and a stale-signature 400 at worst.
|
|
64
|
-
*/
|
|
65
|
-
// Re-exported: these were defined here before the engine needed them too, and
|
|
66
|
-
// the tests and the package surface still reach them through this module.
|
|
67
|
-
|
|
68
|
-
function resumableHistory(history) {
|
|
69
|
-
let end = history.length;
|
|
70
|
-
while (end > 0) {
|
|
71
|
-
const m = history[end - 1];
|
|
72
|
-
if (m.role === "assistant" && !m.toolCalls?.length) break;
|
|
73
|
-
end -= 1;
|
|
74
|
-
}
|
|
75
|
-
return history.slice(0, end).map(m => m.role === "assistant" && m.thinking ? {
|
|
76
|
-
...m,
|
|
77
|
-
thinking: undefined
|
|
78
|
-
} : m);
|
|
79
|
-
}
|
|
80
|
-
function createAskBuoySession(config) {
|
|
81
|
-
const catalog = config.catalog ?? _catalog.CATALOG;
|
|
82
|
-
const ledger = new _ledger.EffectLedger();
|
|
83
|
-
const policy = config.policy ?? {};
|
|
84
|
-
let messages = [];
|
|
85
|
-
/**
|
|
86
|
-
* Image-URL provenance for the whole conversation, not one turn of it.
|
|
87
|
-
*
|
|
88
|
-
* "Show me the shop as pictures" and "no, actually render them" are two
|
|
89
|
-
* turns; the second has no tool calls of its own, so a per-turn set held
|
|
90
|
-
* nothing and every image was stripped out from under the model. It kept
|
|
91
|
-
* describing artwork that was not on screen. The rule is unchanged — a URL
|
|
92
|
-
* still has to have been READ — the window is just the conversation. Reset
|
|
93
|
-
* with the conversation.
|
|
94
|
-
*/
|
|
95
|
-
const seenUrls = new Set();
|
|
96
|
-
/**
|
|
97
|
-
* Every result's full text, for `ask-buoy.retrieve` — see engine/evidence.ts.
|
|
98
|
-
* Conversation-scoped like `seenUrls`: "what were the stats in that response"
|
|
99
|
-
* is a normal second message. Cleared with the conversation; never persisted.
|
|
100
|
-
*/
|
|
101
|
-
const evidence = new _evidence.EvidenceStore();
|
|
102
|
-
/** Chars-per-token as measured on THIS session's provider — see engine/tokenCalibration.ts. */
|
|
103
|
-
const calibration = new _tokenCalibration.TokenCalibration();
|
|
104
|
-
let system;
|
|
105
|
-
let primed = false;
|
|
106
|
-
/** See `setApprovalBypass`. Consulted per turn, never captured. */
|
|
107
|
-
let bypassApproval = false;
|
|
108
|
-
/** Actions the user waived the card for in this conversation — see RunTurnInput.trusted. */
|
|
109
|
-
const trusted = new Set();
|
|
110
|
-
/** See `setReadOnly`. */
|
|
111
|
-
let deviceReadOnly = false;
|
|
112
|
-
/**
|
|
113
|
-
* Rounds dropped by `restore` — reported on the first send, because there is
|
|
114
|
-
* no open turn to carry the event when the restore happens.
|
|
115
|
-
*/
|
|
116
|
-
let trimmedOnRestore = 0;
|
|
117
|
-
/**
|
|
118
|
-
* Which conversation is live. Bumped by `reset`.
|
|
119
|
-
*
|
|
120
|
-
* A turn's history is committed when its generator drains, and a turn does
|
|
121
|
-
* not stop the moment Stop is pressed — an in-flight dispatch has to settle
|
|
122
|
-
* first, and nothing can pull a store write back out of an adapter. So the
|
|
123
|
-
* sequence Stop -> "New conversation" -> the write finishes used to end with
|
|
124
|
-
* the OLD turn assigning its whole history over the empty one: a chat the
|
|
125
|
-
* user had just cleared came back, carrying a change they never saw made.
|
|
126
|
-
* The turn still finishes and its effect still lands in the app; it simply
|
|
127
|
-
* no longer owns a conversation that has moved on.
|
|
128
|
-
*/
|
|
129
|
-
let generation = 0;
|
|
130
|
-
/**
|
|
131
|
-
* The grounding probe's result, held past `prime`.
|
|
132
|
-
*
|
|
133
|
-
* The system prompt is a pure function of this plus the policy, and the
|
|
134
|
-
* approval paragraph changes when the device toggle does — holding the
|
|
135
|
-
* digest lets the prompt be rebuilt without re-probing the app for state it
|
|
136
|
-
* already read.
|
|
137
|
-
*/
|
|
138
|
-
let digest = {};
|
|
139
|
-
/**
|
|
140
|
-
* `storage key -> the live store that owns it`, derived from the grounding.
|
|
141
|
-
*
|
|
142
|
-
* Kept because the prompt alone is advice, and a cheap model skips advice the
|
|
143
|
-
* same way it skips an optional parameter. With this the ENGINE can tell,
|
|
144
|
-
* at dispatch time, that a storage write landed on a store's saved copy and
|
|
145
|
-
* therefore changed nothing the user can see.
|
|
146
|
-
*/
|
|
147
|
-
let storeKeyOwners = {};
|
|
148
|
-
|
|
149
|
-
// So the agent's own requests to the model endpoint never come back to it
|
|
150
|
-
// out of the network tool — that would feed it its own auth headers.
|
|
151
|
-
(0, _redact.registerSelfEndpoint)(config.endpoint);
|
|
152
|
-
const provider = config.provider ?? (config.protocol === "openai" ? (0, _openai.createOpenAIProvider)(config) : (0, _anthropic.createAnthropicProvider)(config));
|
|
153
|
-
|
|
154
|
-
// Mutable: the device's tool list fills in over the first frames and can
|
|
155
|
-
// change while the sheet is open. See setAvailableActions.
|
|
156
|
-
let availableActions = config.availableActions;
|
|
157
|
-
let availableToolIds = Object.keys(availableActions);
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* THE policy object the engine reads, mutated in place.
|
|
161
|
-
*
|
|
162
|
-
* One object for the life of the session, not a fresh one per turn: the
|
|
163
|
-
* engine reads `policy.readOnly` and the approval lists at each call, so a
|
|
164
|
-
* toggle flipped mid-turn lands on the very next call of that turn. A new
|
|
165
|
-
* object per turn would leave a running turn on the old rules until it
|
|
166
|
-
* finished — "Read only" switched on while the agent was writing would let
|
|
167
|
-
* the rest of the batch through.
|
|
168
|
-
*/
|
|
169
|
-
const live = {
|
|
170
|
-
...policy
|
|
171
|
-
};
|
|
172
|
-
function refreshLive() {
|
|
173
|
-
for (const k of Object.keys(live)) delete live[k];
|
|
174
|
-
Object.assign(live, policy);
|
|
175
|
-
if (bypassApproval) {
|
|
176
|
-
live.requireApproval = [];
|
|
177
|
-
live.requireApprovalFor = [];
|
|
178
|
-
}
|
|
179
|
-
// OR, never override: the device can narrow, the app's own read-only stands.
|
|
180
|
-
if (deviceReadOnly) live.readOnly = true;
|
|
181
|
-
}
|
|
182
|
-
function activePolicy() {
|
|
183
|
-
return live;
|
|
184
|
-
}
|
|
185
|
-
function composeSystem() {
|
|
186
|
-
system = (0, _systemPrompt.buildSystemPrompt)({
|
|
187
|
-
catalog,
|
|
188
|
-
context: {
|
|
189
|
-
...config.context,
|
|
190
|
-
digest: {
|
|
191
|
-
...digest,
|
|
192
|
-
...config.context?.digest
|
|
193
|
-
}
|
|
194
|
-
},
|
|
195
|
-
isRelease: config.isRelease,
|
|
196
|
-
readOnly: live.readOnly ?? false,
|
|
197
|
-
readOnlySource: policy.readOnly ? "app" : deviceReadOnly ? "device" : undefined,
|
|
198
|
-
requireApproval: (0, _policy.resolveRequireApproval)(live),
|
|
199
|
-
approvalsBypassed: bypassApproval,
|
|
200
|
-
appName: config.appName,
|
|
201
|
-
platform: config.platform,
|
|
202
|
-
availableToolIds
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
async function prime() {
|
|
206
|
-
if (primed) return;
|
|
207
|
-
primed = true;
|
|
208
|
-
digest = {};
|
|
209
|
-
try {
|
|
210
|
-
digest = await (0, _buildContextPack.buildContextDigest)({
|
|
211
|
-
dispatch: config.dispatch,
|
|
212
|
-
readSnapshot: config.readSnapshot,
|
|
213
|
-
availableToolIds,
|
|
214
|
-
availableActions
|
|
215
|
-
});
|
|
216
|
-
} catch {
|
|
217
|
-
// Grounding is an optimisation. A chat with no digest still works; a
|
|
218
|
-
// chat that refuses to open because a probe timed out does not.
|
|
219
|
-
}
|
|
220
|
-
storeKeyOwners = {};
|
|
221
|
-
for (const raw of digest.zustandStores ?? []) {
|
|
222
|
-
const store = raw;
|
|
223
|
-
if (store?.persistsTo && store.name) storeKeyOwners[store.persistsTo] = store.name;
|
|
224
|
-
}
|
|
225
|
-
composeSystem();
|
|
226
|
-
}
|
|
227
|
-
return {
|
|
228
|
-
ledger,
|
|
229
|
-
evidence,
|
|
230
|
-
calibration,
|
|
231
|
-
trustedActions: () => [...trusted],
|
|
232
|
-
untrust: key => {
|
|
233
|
-
trusted.delete(key);
|
|
234
|
-
},
|
|
235
|
-
get messages() {
|
|
236
|
-
return messages;
|
|
237
|
-
},
|
|
238
|
-
prime,
|
|
239
|
-
setAvailableActions(actions) {
|
|
240
|
-
availableActions = actions;
|
|
241
|
-
availableToolIds = Object.keys(actions);
|
|
242
|
-
},
|
|
243
|
-
setApprovalBypass(enabled) {
|
|
244
|
-
if (bypassApproval === enabled) return;
|
|
245
|
-
bypassApproval = enabled;
|
|
246
|
-
refreshLive();
|
|
247
|
-
// The prompt tells the model whether a card will appear before its
|
|
248
|
-
// change lands; leaving that paragraph stale would have it announce an
|
|
249
|
-
// approval step that no longer exists. Rebuilt from the held digest, so
|
|
250
|
-
// toggling mid-conversation costs nothing and re-reads nothing.
|
|
251
|
-
if (system) composeSystem();
|
|
252
|
-
},
|
|
253
|
-
setReadOnly(enabled) {
|
|
254
|
-
if (deviceReadOnly === enabled) return;
|
|
255
|
-
deviceReadOnly = enabled;
|
|
256
|
-
refreshLive();
|
|
257
|
-
if (system) composeSystem();
|
|
258
|
-
},
|
|
259
|
-
restore(history) {
|
|
260
|
-
// Only into a session that has not been talked to. Restoring over a live
|
|
261
|
-
// conversation would drop whatever was said since the app started.
|
|
262
|
-
if (messages.length > 0) return;
|
|
263
|
-
const report = (0, _historyBudget.trimHistoryReport)(resumableHistory(history), calibration.charsPerToken, ledger.liveCallIds());
|
|
264
|
-
messages = report.messages;
|
|
265
|
-
trimmedOnRestore = report.droppedRounds;
|
|
266
|
-
},
|
|
267
|
-
async *send(text, signal) {
|
|
268
|
-
/**
|
|
269
|
-
* Captured FIRST, before any await.
|
|
270
|
-
*
|
|
271
|
-
* Priming and the live-context probes are awaits like any other, and a
|
|
272
|
-
* reset can land inside them. Reading the generation after them would
|
|
273
|
-
* pick up the NEW conversation's value and let this turn commit into it —
|
|
274
|
-
* the precise bug this guards against, reintroduced one line lower.
|
|
275
|
-
*/
|
|
276
|
-
const myGeneration = generation;
|
|
277
|
-
await prime();
|
|
278
|
-
// The RIGHT NOW block: re-read on EVERY turn, not once at open. The
|
|
279
|
-
// stable digest above was once the whole picture, and it was built
|
|
280
|
-
// before the user had navigated anywhere — so the query that owned the
|
|
281
|
-
// screen they were looking at was simply not in the prompt. This is the
|
|
282
|
-
// uncached tail of the system prompt (see ProviderRequest.systemVolatile).
|
|
283
|
-
let live;
|
|
284
|
-
try {
|
|
285
|
-
live = await (0, _buildContextPack.buildLiveContext)({
|
|
286
|
-
dispatch: config.dispatch,
|
|
287
|
-
readSnapshot: config.readSnapshot,
|
|
288
|
-
availableToolIds,
|
|
289
|
-
availableActions
|
|
290
|
-
});
|
|
291
|
-
} catch {
|
|
292
|
-
live = undefined;
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* The conversation was cleared while this turn was still getting ready.
|
|
296
|
-
*
|
|
297
|
-
* Priming and the live probes take real time, so "New conversation"
|
|
298
|
-
* lands here often enough to matter. Abandoning now costs nothing: the
|
|
299
|
-
* model has not been asked anything yet.
|
|
300
|
-
*/
|
|
301
|
-
if (myGeneration !== generation) {
|
|
302
|
-
yield {
|
|
303
|
-
type: "done",
|
|
304
|
-
stopReason: "superseded"
|
|
305
|
-
};
|
|
306
|
-
return;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* The turn works on its OWN array, and `messages` is assigned in exactly
|
|
311
|
-
* one place — the commit at the bottom.
|
|
312
|
-
*
|
|
313
|
-
* `runAgentTurn` pushes each round into the array it is handed. Handing
|
|
314
|
-
* it the session's own array made every push a live edit of session
|
|
315
|
-
* state, so a turn racing a reset wrote its whole history into the new
|
|
316
|
-
* conversation one push at a time and the commit guard had nothing left
|
|
317
|
-
* to protect. Copying makes ownership checkable in one place instead of
|
|
318
|
-
* spread across the loop.
|
|
319
|
-
*/
|
|
320
|
-
const trimmed = (0, _historyBudget.trimHistoryReport)([...messages, {
|
|
321
|
-
role: "user",
|
|
322
|
-
text
|
|
323
|
-
}], calibration.charsPerToken, ledger.liveCallIds());
|
|
324
|
-
const working = trimmed.messages;
|
|
325
|
-
// Said once, before the answer it affects. The screen still shows the
|
|
326
|
-
// rounds the model just lost; this is the only thing that tells the two
|
|
327
|
-
// apart. See the `history-trimmed` event.
|
|
328
|
-
const droppedRounds = trimmed.droppedRounds + trimmedOnRestore;
|
|
329
|
-
trimmedOnRestore = 0;
|
|
330
|
-
if (droppedRounds > 0) yield {
|
|
331
|
-
type: "history-trimmed",
|
|
332
|
-
droppedRounds,
|
|
333
|
-
droppedPinned: trimmed.droppedPinned
|
|
334
|
-
};
|
|
335
|
-
const gen = (0, _runAgentTurn.runAgentTurn)({
|
|
336
|
-
provider,
|
|
337
|
-
catalog,
|
|
338
|
-
messages: working,
|
|
339
|
-
system: system,
|
|
340
|
-
systemVolatile: (0, _systemPrompt.buildLiveBlock)(live,
|
|
341
|
-
// What this conversation has already done to the app. See
|
|
342
|
-
// buildLiveBlock — without it the model answers "what have you
|
|
343
|
-
// changed?" from memory of its own sentences, and reports a value it
|
|
344
|
-
// overwrote itself as the app's own.
|
|
345
|
-
ledger.list().map(e => ({
|
|
346
|
-
label: `${e.toolId}.${e.action}${e.effect.label ? ` — ${e.effect.label}` : ""}`,
|
|
347
|
-
undone: e.undoneAt !== undefined
|
|
348
|
-
}))),
|
|
349
|
-
model: config.model,
|
|
350
|
-
maxTokens: config.maxTokens ?? 4096,
|
|
351
|
-
dispatch: config.dispatch,
|
|
352
|
-
readSnapshot: config.readSnapshot,
|
|
353
|
-
storeKeyOwners,
|
|
354
|
-
ledger,
|
|
355
|
-
policy: activePolicy(),
|
|
356
|
-
isRelease: config.isRelease,
|
|
357
|
-
availableToolIds,
|
|
358
|
-
availableActions,
|
|
359
|
-
requestApproval: config.requestApproval,
|
|
360
|
-
seenUrls,
|
|
361
|
-
evidence,
|
|
362
|
-
calibration,
|
|
363
|
-
trusted,
|
|
364
|
-
procedures: (0, _systemPrompt.listableProcedures)(config.context?.procedures, availableToolIds),
|
|
365
|
-
signal
|
|
366
|
-
});
|
|
367
|
-
let next = await gen.next();
|
|
368
|
-
while (!next.done) {
|
|
369
|
-
yield next.value;
|
|
370
|
-
next = await gen.next();
|
|
371
|
-
}
|
|
372
|
-
// The generator's return value is the full history including tool turns.
|
|
373
|
-
// Committed only if this turn still owns the conversation — see
|
|
374
|
-
// `generation`.
|
|
375
|
-
if (myGeneration === generation) messages = next.value;
|
|
376
|
-
},
|
|
377
|
-
reset() {
|
|
378
|
-
// Before anything else: a turn still draining must not commit over this.
|
|
379
|
-
generation += 1;
|
|
380
|
-
messages = [];
|
|
381
|
-
trimmedOnRestore = 0;
|
|
382
|
-
ledger.clear();
|
|
383
|
-
seenUrls.clear();
|
|
384
|
-
evidence.clear();
|
|
385
|
-
trusted.clear();
|
|
386
|
-
// The grounding digest is a snapshot of the app AT OPEN — stores and
|
|
387
|
-
// routes registered since are invisible to it. A new conversation is
|
|
388
|
-
// the natural moment to re-read the world.
|
|
389
|
-
primed = false;
|
|
390
|
-
}
|
|
391
|
-
};
|
|
392
|
-
}
|
|
393
|
-
//# sourceMappingURL=session.js.map
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});Object.defineProperty(exports,"compressConsumed",{enumerable:true,get:function(){return _historyBudget.compressConsumed}});exports.createAskBuoySession=createAskBuoySession;exports.resumableHistory=resumableHistory;Object.defineProperty(exports,"trimHistory",{enumerable:true,get:function(){return _historyBudget.trimHistory}});Object.defineProperty(exports,"trimHistoryReport",{enumerable:true,get:function(){return _historyBudget.trimHistoryReport}});var _catalog=require("./catalog/catalog.g");var _buildContextPack=require("./context/buildContextPack");var _runAgentTurn=require("./engine/runAgentTurn");var _systemPrompt=require("./engine/systemPrompt");var _ledger=require("./effects/ledger");var _evidence=require("./engine/evidence");var _tokenCalibration=require("./engine/tokenCalibration");var _historyBudget=require("./engine/historyBudget");var _anthropic=require("./providers/anthropic");var _openai=require("./providers/openai");var _policy=require("./policy/policy");var _redact=require("./policy/redact");function resumableHistory(e){let n=e.length;while(n>0){const r=e[n-1];if(r.role==="assistant"&&!r.toolCalls?.length)break;n-=1}return e.slice(0,n).map(r=>r.role==="assistant"&&r.thinking?{...r,thinking:void 0}:r)}function createAskBuoySession(e){const n=e.catalog??_catalog.CATALOG;const r=new _ledger.EffectLedger;const A=e.policy??{};let o=[];const R=new Set;const g=new _evidence.EvidenceStore;const p=new _tokenCalibration.TokenCalibration;let u;let k=false;let c=false;const y=new Set;let m=false;let v=0;let b=0;let f={};let x={};(0,_redact.registerSelfEndpoint)(e.endpoint);const j=e.provider??(e.protocol==="openai"?(0,_openai.createOpenAIProvider)(e):(0,_anthropic.createAnthropicProvider)(e));let l=e.availableActions;let i=Object.keys(l);const s={...A};function S(){for(const t of Object.keys(s))delete s[t];Object.assign(s,A);if(c){s.requireApproval=[];s.requireApprovalFor=[]}if(m)s.readOnly=true}function H(){return s}function O(){u=(0,_systemPrompt.buildSystemPrompt)({catalog:n,context:{...e.context,digest:{...f,...e.context?.digest}},isRelease:e.isRelease,readOnly:s.readOnly??false,readOnlySource:A.readOnly?"app":m?"device":void 0,requireApproval:(0,_policy.resolveRequireApproval)(s),approvalsBypassed:c,appName:e.appName,platform:e.platform,availableToolIds:i})}async function w(){if(k)return;k=true;f={};try{f=await(0,_buildContextPack.buildContextDigest)({dispatch:e.dispatch,readSnapshot:e.readSnapshot,availableToolIds:i,availableActions:l})}catch{}x={};for(const t of f.zustandStores??[]){const a=t;if(a?.persistsTo&&a.name)x[a.persistsTo]=a.name}O()}return{ledger:r,evidence:g,calibration:p,trustedActions:()=>[...y],untrust:t=>{y.delete(t)},get messages(){return o},prime:w,setAvailableActions(t){l=t;i=Object.keys(t)},setApprovalBypass(t){if(c===t)return;c=t;S();if(u)O()},setReadOnly(t){if(m===t)return;m=t;S();if(u)O()},restore(t){if(o.length>0)return;const a=(0,_historyBudget.trimHistoryReport)(resumableHistory(t),p.charsPerToken,r.liveCallIds());o=a.messages;v=a.droppedRounds},async*send(t,a){const _=b;await w();let q;try{q=await(0,_buildContextPack.buildLiveContext)({dispatch:e.dispatch,readSnapshot:e.readSnapshot,availableToolIds:i,availableActions:l})}catch{q=void 0}if(_!==b){yield{type:"done",stopReason:"superseded"};return}const P=(0,_historyBudget.trimHistoryReport)([...o,{role:"user",text:t}],p.charsPerToken,r.liveCallIds());const B=P.messages;const C=P.droppedRounds+v;v=0;if(C>0)yield{type:"history-trimmed",droppedRounds:C,droppedPinned:P.droppedPinned};const T=(0,_runAgentTurn.runAgentTurn)({provider:j,catalog:n,messages:B,system:u,systemVolatile:(0,_systemPrompt.buildLiveBlock)(q,r.list().map(d=>({label:`${d.toolId}.${d.action}${d.effect.label?` \u2014 ${d.effect.label}`:""}`,undone:d.undoneAt!==void 0}))),model:e.model,maxTokens:e.maxTokens??4096,dispatch:e.dispatch,readSnapshot:e.readSnapshot,storeKeyOwners:x,ledger:r,policy:H(),isRelease:e.isRelease,availableToolIds:i,availableActions:l,requestApproval:e.requestApproval,seenUrls:R,evidence:g,calibration:p,trusted:y,procedures:(0,_systemPrompt.listableProcedures)(e.context?.procedures,i),signal:a});let h=await T.next();while(!h.done){yield h.value;h=await T.next()}if(_===b)o=h.value},reset(){b+=1;o=[];v=0;r.clear();R.clear();g.clear();y.clear();k=false}}}
|
package/lib/commonjs/types.js
CHANGED