@yeaft/webchat-agent 0.1.467 → 0.1.468

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yeaft/webchat-agent",
3
- "version": "0.1.467",
3
+ "version": "0.1.468",
4
4
  "description": "Remote agent for Yeaft WebChat — connects worker machines to the central server",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -122,6 +122,56 @@ export function budgetExceededResult(agent, reason) {
122
122
  };
123
123
  }
124
124
 
125
+ /**
126
+ * Apply an incremental delta to an agent's usage, then check budget.
127
+ * If exceeded: abort the agent's signal, set result to the budget envelope,
128
+ * flip status to 'completed', and return the envelope. Otherwise returns null.
129
+ *
130
+ * Call this at each turn boundary inside the sub-agent's execution loop.
131
+ *
132
+ * @param {string} agentId
133
+ * @param {{ tokens?: number, turns?: number, partial_output?: string }} [delta]
134
+ * @param {number} [now=Date.now()]
135
+ * @returns {object|null} — budget envelope if exceeded, else null
136
+ */
137
+ export function tickAgent(agentId, delta = {}, now = Date.now()) {
138
+ const agent = agents.get(agentId);
139
+ if (!agent) return null;
140
+ if (agent.status === 'completed' || agent.status === 'closed') return null;
141
+
142
+ if (typeof delta.tokens === 'number' && delta.tokens > 0) {
143
+ agent.usage.tokens += delta.tokens;
144
+ }
145
+ if (typeof delta.turns === 'number' && delta.turns > 0) {
146
+ agent.usage.turns += delta.turns;
147
+ }
148
+ if (typeof delta.partial_output === 'string' && delta.partial_output) {
149
+ agent.partial_output = delta.partial_output;
150
+ }
151
+
152
+ const check = checkBudget(agent, now);
153
+ if (!check.exceeded) return null;
154
+
155
+ const envelope = budgetExceededResult(agent, check.reason);
156
+ agent.result = envelope;
157
+ agent.status = 'completed';
158
+ agent.diagnostics.push({
159
+ type: 'budget_exceeded',
160
+ limit: check.limit,
161
+ reason: check.reason,
162
+ at: now,
163
+ });
164
+ // Signal any in-flight sub-agent work to stop
165
+ if (agent.abortController && !agent.abortController.signal.aborted) {
166
+ try {
167
+ agent.abortController.abort(check.reason);
168
+ } catch {
169
+ // ignore double-abort
170
+ }
171
+ }
172
+ return envelope;
173
+ }
174
+
125
175
  export default defineTool({
126
176
  name: 'Agent',
127
177
  description: `Create a sub-agent to work on an independent task in parallel.
@@ -219,6 +269,7 @@ Guidelines:
219
269
  usage: { tokens: 0, turns: 0, startedAt: now },
220
270
  createdAt: now,
221
271
  trace: [],
272
+ abortController: new AbortController(),
222
273
  };
223
274
 
224
275
  agents.set(agentId, agent);