@ziggs-ai/agent-sdk 0.1.3 → 0.1.4

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.
Files changed (85) hide show
  1. package/README.md +1 -1
  2. package/package.json +9 -4
  3. package/src/AgentHost.ts +342 -0
  4. package/src/adapters/OpenAIAdapter.ts +125 -0
  5. package/src/agent/Agent.ts +98 -0
  6. package/src/cognition/validateContext.ts +95 -0
  7. package/src/context/applyEffects.ts +80 -0
  8. package/src/context/batch.ts +17 -0
  9. package/src/context/classifyEnvelope.ts +38 -0
  10. package/src/context/routingLabels.ts +46 -0
  11. package/src/defineAgent.ts +62 -0
  12. package/src/formatters/AgreementFormatter.ts +111 -0
  13. package/src/formatters/HistoryFormatter.ts +166 -0
  14. package/src/formatters/index.ts +2 -0
  15. package/src/index.ts +86 -0
  16. package/src/ingress/normalizeIncoming.ts +119 -0
  17. package/src/memory/MemoryStore.ts +104 -0
  18. package/src/runtime/AgentMachine.ts +298 -0
  19. package/src/runtime/PromptBuilder.ts +461 -0
  20. package/src/runtime/buildOutcome.ts +488 -0
  21. package/src/runtime/defaults.ts +72 -0
  22. package/src/runtime/runTurn.ts +637 -0
  23. package/src/runtime/validateWorkflow.ts +165 -0
  24. package/src/server/ConnectionPool.ts +155 -0
  25. package/src/server/EventQueue.ts +119 -0
  26. package/src/server/OutboxBuffer.ts +90 -0
  27. package/src/server/ZiggsEffectHandler.ts +335 -0
  28. package/src/server/agreements/AgreementService.ts +111 -0
  29. package/src/server/createHealthServer.ts +8 -0
  30. package/src/server/proactive/ProactiveTrigger.ts +83 -0
  31. package/src/server/runLauncher.ts +131 -0
  32. package/src/server/tasks/TaskService.ts +111 -0
  33. package/src/server/tasks/index.ts +4 -0
  34. package/src/server/tasks/paymentTools.ts +156 -0
  35. package/src/server/tasks/protocolRunner.ts +101 -0
  36. package/src/server/tasks/protocolTools.ts +96 -0
  37. package/src/server/ziggspay/ZiggsPayClient.ts +193 -0
  38. package/src/shared/ids.ts +3 -0
  39. package/src/shared/runtimeLog.ts +72 -0
  40. package/src/shared/types.ts +31 -0
  41. package/src/tasks/protocolRegistry.ts +25 -0
  42. package/src/tasks/taskCore.ts +139 -0
  43. package/src/tools/ToolManager.ts +95 -0
  44. package/src/tools/{ToolProvider.js → ToolProvider.ts} +5 -15
  45. package/src/tools/defineTool.ts +90 -0
  46. package/src/tools/index.ts +5 -0
  47. package/src/types.ts +368 -0
  48. package/src/utils/jsonExtractor.ts +100 -0
  49. package/src/ConnectionPool.js +0 -133
  50. package/src/adapters/OpenAIAdapter.js +0 -73
  51. package/src/agent/Agent.js +0 -121
  52. package/src/agent/EventQueue.js +0 -68
  53. package/src/agent/OutboxBuffer.js +0 -62
  54. package/src/cognition/PromptBuilder.js +0 -312
  55. package/src/cognition/resolveActionTool.js +0 -12
  56. package/src/cognition/runTurn.js +0 -578
  57. package/src/context/applyEffects.js +0 -133
  58. package/src/context/batch.js +0 -25
  59. package/src/context/classifyEnvelope.js +0 -82
  60. package/src/context/routingLabels.js +0 -54
  61. package/src/createHealthServer.js +0 -28
  62. package/src/formatters/HistoryFormatter.js +0 -257
  63. package/src/formatters/TaskFormatter.js +0 -180
  64. package/src/formatters/index.js +0 -9
  65. package/src/index.js +0 -76
  66. package/src/ingress/normalizeIncoming.js +0 -70
  67. package/src/runLauncher.js +0 -159
  68. package/src/shared/ids.js +0 -7
  69. package/src/shared/types.js +0 -86
  70. package/src/tasks/TaskService.js +0 -247
  71. package/src/tasks/index.js +0 -9
  72. package/src/tasks/taskCore.js +0 -229
  73. package/src/tasks/taskProtocolRegistry.js +0 -22
  74. package/src/tasks/taskProtocolRunner.js +0 -107
  75. package/src/tasks/taskProtocolTools.js +0 -87
  76. package/src/tools/ToolManager.js +0 -79
  77. package/src/tools/defineTool.js +0 -82
  78. package/src/tools/index.js +0 -11
  79. package/src/utils/jsonExtractor.js +0 -139
  80. package/src/workflow/AgentMachine.js +0 -250
  81. package/src/workflow/WorkflowRuntime.js +0 -63
  82. package/src/workflow/dsl.js +0 -287
  83. package/src/workflow/motifs.js +0 -435
  84. package/src/ziggs/runtime.js +0 -192
  85. /package/src/adapters/{index.js → index.ts} +0 -0
@@ -0,0 +1,488 @@
1
+ import type {
2
+ Action,
3
+ AgreementRef,
4
+ Ctx,
5
+ Outcome,
6
+ OutcomeKind,
7
+ ProposalRef,
8
+ TaskRef,
9
+ ToolResultEntry,
10
+ } from '../types.js';
11
+ import { isProtocolToolName, mapProtocolToolToOperation } from '../tasks/protocolRegistry.js';
12
+ import { getBatchEvents, isTaskResultRelevantToAgent } from '../context/batch.js';
13
+
14
+ // ── Event shape (untyped wire payload normalized into a small set of types) ──
15
+
16
+ /** Wire payload from the backend for task/agreement state. */
17
+ export interface WireResult {
18
+ taskId?: string;
19
+ state?: string;
20
+ status?: string;
21
+ agreement?: AgreementRef;
22
+ agreementId?: string;
23
+ proposal?: { status?: 'pending' | 'approved' | 'rejected' | 'cancelled' };
24
+ task?: TaskRef;
25
+ success?: boolean;
26
+ error?: string;
27
+ providerIsYou?: boolean;
28
+ [key: string]: unknown;
29
+ }
30
+
31
+ export interface RawEvent {
32
+ type?: string;
33
+ result?: WireResult;
34
+ receiverId?: string;
35
+ senderId?: string;
36
+ text?: string;
37
+ events?: RawEvent[];
38
+ [key: string]: unknown;
39
+ }
40
+
41
+ export interface EmittedEvent {
42
+ type: string;
43
+ tool?: string;
44
+ result?: WireResult;
45
+ error?: string;
46
+ receiverId?: string;
47
+ chatId?: string;
48
+ message?: string;
49
+ text?: string;
50
+ operation?: string;
51
+ senderId?: string;
52
+ [key: string]: unknown;
53
+ }
54
+
55
+ // ── outcomeFromEvent: incoming wire event → Outcome ──────────────────────────
56
+
57
+ /**
58
+ * Converts a raw incoming event into the single Outcome that re-enters the
59
+ * machine.
60
+ *
61
+ * For batched events, returns the *most significant* outcome (task assignments
62
+ * win over messages). EventQueue already flattened multiple distinct events
63
+ * via re-entry, so a batch here usually means events that arrived together
64
+ * within a single backend frame.
65
+ */
66
+ export function outcomeFromEvent(
67
+ rawEvent: RawEvent | null | undefined,
68
+ ownAgentId: string | null,
69
+ ): Outcome {
70
+ if (!rawEvent) return { kind: 'enter' };
71
+
72
+ const events: RawEvent[] = getBatchEvents(rawEvent) as RawEvent[];
73
+ let best: Outcome | null = null;
74
+
75
+ for (const ev of events) {
76
+ if (!ev) continue;
77
+ const candidate = singleEventToOutcome(ev, ownAgentId);
78
+ if (!candidate) continue;
79
+ if (!best || outcomeRank(candidate) > outcomeRank(best)) best = candidate;
80
+ }
81
+
82
+ return best ?? { kind: 'enter' };
83
+ }
84
+
85
+ function singleEventToOutcome(ev: RawEvent, ownAgentId: string | null): Outcome | null {
86
+ if (ev.type === 'task_result') {
87
+ const result = ev.result || {};
88
+ if (
89
+ ownAgentId &&
90
+ !isTaskResultRelevantToAgent(result, ownAgentId) &&
91
+ ev.receiverId !== ownAgentId
92
+ ) {
93
+ return null;
94
+ }
95
+
96
+ // Agreement lifecycle notification routed as task_result by normalizeIncomingEvent.
97
+ // _operation is set to the backend operation name (e.g. 'proposal_approved').
98
+ const inboundOp = result._operation as string | undefined;
99
+ if (inboundOp) {
100
+ const proposalAction = inboundOp === 'proposal_approved' ? 'approve'
101
+ : inboundOp === 'proposal_rejected' ? 'reject'
102
+ : null;
103
+ if (proposalAction) {
104
+ const agId = result.agreementId;
105
+ const proposal: ProposalRef = agId
106
+ ? { agreementId: agId, agreement: result as unknown as AgreementRef }
107
+ : { agreementId: '' };
108
+ return { kind: 'proposal-resolved', action: proposalAction as 'approve' | 'reject', proposal };
109
+ }
110
+ // Other lifecycle ops (countered, expired) treated as enter — no FSM action needed here.
111
+ return null;
112
+ }
113
+
114
+ // Tasks are born active in the new model — no `proposal` task state.
115
+ // The wire shape `task_result` always carries an active or terminal task.
116
+ const task: TaskRef = result as unknown as TaskRef;
117
+ const ag: AgreementRef | undefined = result.agreement;
118
+ const parties = ag?.parties || {};
119
+ const state = result.state;
120
+
121
+ // Direct assignment to me (provider) — task spawned, citing an active agreement.
122
+ const isAssignedToMe =
123
+ (result.providerIsYou || parties.providerId === ownAgentId) &&
124
+ (state === 'active' || state === 'in-progress');
125
+ if (isAssignedToMe) {
126
+ return { kind: 'task-assigned', task };
127
+ }
128
+
129
+ if (state === 'active' || state === 'in-progress') {
130
+ return { kind: 'task-assigned', task };
131
+ }
132
+
133
+ if (state === 'completed' || state === 'failed') {
134
+ return {
135
+ kind: 'subtask-finished',
136
+ status: state === 'completed' ? 'completed' : 'failed',
137
+ task,
138
+ result,
139
+ };
140
+ }
141
+
142
+ if (state === 'cancelled') {
143
+ return {
144
+ kind: 'subtask-finished',
145
+ status: 'failed',
146
+ task,
147
+ result,
148
+ };
149
+ }
150
+
151
+ // Fallback: addressed to us with no clear state.
152
+ if (ownAgentId && ev.receiverId === ownAgentId) {
153
+ return { kind: 'task-assigned', task };
154
+ }
155
+
156
+ return { kind: 'subtask-finished', status: 'completed', task, result };
157
+ }
158
+
159
+ if (ev.type === 'resource_changed') {
160
+ const resource = ((ev as { resource?: Record<string, unknown> }).resource ?? ev) as {
161
+ kind?: string;
162
+ reason?: string;
163
+ resourceId?: string;
164
+ agreementId?: string;
165
+ taskId?: string;
166
+ };
167
+ const reason = resource.reason;
168
+ if (
169
+ resource.kind === 'agreement' &&
170
+ (reason === 'proposal_approved' || reason === 'proposal_rejected')
171
+ ) {
172
+ const agreementId = resource.agreementId ?? resource.resourceId ?? '';
173
+ const proposal: ProposalRef = { agreementId };
174
+ return {
175
+ kind: 'proposal-resolved',
176
+ action: reason === 'proposal_approved' ? 'approve' : 'reject',
177
+ proposal,
178
+ };
179
+ }
180
+ if (resource.kind === 'task-state' && reason === 'subtask_completed') {
181
+ const taskId = resource.taskId ?? resource.resourceId ?? '';
182
+ const task = { taskId, state: 'completed' } as TaskRef;
183
+ return { kind: 'subtask-finished', status: 'completed', task, result: task };
184
+ }
185
+ return null;
186
+ }
187
+
188
+ // Only real inbound chat messages should drive the FSM. Backend bookkeeping
189
+ // (presence/heartbeat, raw `batch` outer frames, etc.) are NOT chat traffic.
190
+ if (ev.type !== 'message') return null;
191
+
192
+ // Suppress our own messages echoed back through the chat fanout.
193
+ if (ownAgentId && ev.senderId && ev.senderId === ownAgentId) return null;
194
+
195
+ const text = typeof ev.text === 'string' ? ev.text : undefined;
196
+ return { kind: 'message-received', text, senderId: ev.senderId };
197
+ }
198
+
199
+ // Higher rank wins when collapsing batched events into one outcome.
200
+ function outcomeRank(o: Outcome): number {
201
+ switch (o.kind) {
202
+ case 'task-assigned': return 100;
203
+ case 'proposal-resolved': return 90;
204
+ case 'subtask-finished': return 80;
205
+ case 'message-received': return 10;
206
+ default: return 50;
207
+ }
208
+ }
209
+
210
+ // ── outcomeFromActionResult: turn output → Outcome ───────────────────────────
211
+
212
+ /**
213
+ * Builds the single Outcome that ends a thinking-state turn from the
214
+ * action that ran and the events it emitted.
215
+ *
216
+ * Action's `produces` declares the outcome kind — runtime fills in payload.
217
+ * - static string: kind is fixed; runtime constructs payload from events.
218
+ * - function: receives the tool result and args, returns the kind.
219
+ *
220
+ * If `produces` is missing or doesn't match the events, falls back to a
221
+ * best-effort inference.
222
+ */
223
+ export function outcomeFromActionResult(
224
+ actionName: string | null,
225
+ action: Action | null,
226
+ args: unknown,
227
+ emittedEvents: EmittedEvent[],
228
+ toolResultsOut: ToolResultEntry[],
229
+ ): Outcome {
230
+ if (!emittedEvents.length) {
231
+ return { kind: 'error', source: 'unknown', cause: `action "${actionName}" emitted nothing`, retryable: false };
232
+ }
233
+
234
+ // Collect tool results in caller's accumulator (used in prompts and ctx).
235
+ for (const e of emittedEvents) {
236
+ if (e?.type === 'tool_result') toolResultsOut.push({ tool: e.tool || '', result: e.result });
237
+ if (e?.type === 'tool_error') toolResultsOut.push({ tool: e.tool || '', error: e.error });
238
+ }
239
+
240
+ // Pick the "primary" event the outcome describes.
241
+ //
242
+ // Protocol-level state changes (agreement creation, task assignment/closure,
243
+ // proposal response) outrank EVERYTHING else, including message_sent and
244
+ // waited. Why: when the LLM batches `delegate` + `wait` in one round, the
245
+ // sub-agreement DOES get created server-side, but if `waited` wins the FSM
246
+ // sees `wait` and bounces back to `idle` instead of transitioning to
247
+ // `awaitingSubAgreementApproval` — and the chain stalls. The meaningful
248
+ // turn outcome is the work that landed, not the LLM's terminal decision.
249
+ //
250
+ // Among non-protocol events: message_sent / waited still outrank pure
251
+ // query tool_results (e.g. agent_network search) and tool_errors, because
252
+ // those are the LLM's explicit final intent in turns that did no protocol
253
+ // work.
254
+ const primary =
255
+ emittedEvents.find(e => e.type === 'task_result' && isStateChangingProtocolEvent(e)) ??
256
+ emittedEvents.find(e => e.type === 'tool_result' && isStateChangingProtocolEvent(e)) ??
257
+ emittedEvents.find(e => e.type === 'message_sent') ??
258
+ emittedEvents.find(e => e.type === 'message_duplicate_skipped') ??
259
+ emittedEvents.find(e => e.type === 'waited') ??
260
+ emittedEvents.find(e => e.type === 'tool_result') ??
261
+ emittedEvents.find(e => e.type === 'tool_error') ??
262
+ emittedEvents.find(e => e.type === 'task_result') ??
263
+ emittedEvents.find(e => e.type === 'task_error') ??
264
+ emittedEvents[0];
265
+
266
+ // Resolve the kind via produces (preferred) or by inferring from primary.
267
+ let kind: OutcomeKind | null = null;
268
+ if (action?.produces) {
269
+ const p = action.produces;
270
+ const result = primary?.type === 'tool_result' ? primary.result : undefined;
271
+ kind = typeof p === 'function' ? p(result, args) : p;
272
+ }
273
+ if (!kind) kind = inferKindFromEvent(primary);
274
+
275
+ return buildOutcomeOfKind(kind, primary, action);
276
+ }
277
+
278
+ /**
279
+ * Whether a tool_result / task_result represents a protocol state change
280
+ * (agreement creation, task assignment / closure, proposal response). Used
281
+ * to outrank pure query tool_results (e.g. `agent_network search`) when the
282
+ * same turn batches multiple tools.
283
+ */
284
+ function isStateChangingProtocolEvent(ev: EmittedEvent): boolean {
285
+ const op = ev.type === 'task_result'
286
+ ? ev.operation
287
+ : (ev.tool && isProtocolToolName(ev.tool) ? mapProtocolToolToOperation(ev.tool) : null);
288
+ if (!op) return false;
289
+ const r: WireResult = ev.result ?? ({} as WireResult);
290
+ if (r.success === false || r.error) return false;
291
+ const ag = r.agreement ?? (r.agreementId ? r : null);
292
+ const task = r.task || (r.taskId ? r : null);
293
+ const proposalStatus = ag?.proposal?.status;
294
+ const state = task?.state || r.state;
295
+ if (op === 'agreement-propose' || op === 'agreement-subcontract') {
296
+ return proposalStatus === 'pending' || proposalStatus === 'approved' || !!ag?.agreementId;
297
+ }
298
+ if (op === 'agreement-respond') return true;
299
+ if (op === 'task-update') {
300
+ return state === 'completed' || state === 'failed' || state === 'cancelled';
301
+ }
302
+ if (op === 'task-spawn') return !!task?.taskId;
303
+ return false;
304
+ }
305
+
306
+ function inferKindFromEvent(primary: EmittedEvent | undefined): OutcomeKind {
307
+ if (!primary) return 'error';
308
+ switch (primary.type) {
309
+ case 'message_sent': return 'message-sent';
310
+ case 'message_duplicate_skipped': return 'message-sent';
311
+ case 'waited': return 'wait';
312
+ case 'tool_error': return 'error';
313
+ case 'task_error': return 'error';
314
+ case 'tool_result': return inferKindFromToolResult(primary);
315
+ case 'task_result': return inferKindFromTaskResult(primary);
316
+ default: return 'tool-result';
317
+ }
318
+ }
319
+
320
+ function inferKindFromToolResult(ev: EmittedEvent): OutcomeKind {
321
+ if (!isProtocolToolName(ev.tool ?? '')) return 'tool-result';
322
+ const op = mapProtocolToolToOperation(ev.tool ?? '');
323
+ return inferKindFromProtocolOp(op ?? undefined, ev.result);
324
+ }
325
+
326
+ function inferKindFromTaskResult(ev: EmittedEvent): OutcomeKind {
327
+ return inferKindFromProtocolOp(ev.operation, ev.result);
328
+ }
329
+
330
+ function inferKindFromProtocolOp(op: string | undefined, result: unknown): OutcomeKind {
331
+ const r: WireResult = (result as WireResult) || ({} as WireResult);
332
+ const ag: AgreementRef | undefined = r.agreement ?? (r.agreementId ? r as unknown as AgreementRef : undefined);
333
+ const task: TaskRef | undefined = r.task || (r.taskId ? r as unknown as TaskRef : undefined);
334
+
335
+ // Agreement creation verbs no longer create paired tasks. The caller (creator)
336
+ // has made a proposal awaiting response from the proposedTo party.
337
+ if (
338
+ op === 'agreement-propose' ||
339
+ op === 'agreement-subcontract'
340
+ ) {
341
+ return 'proposal-made';
342
+ }
343
+ if (op === 'task-spawn') {
344
+ // A task was just spawned under an active agreement. From the spawner's
345
+ // side this is a delegation; the executor sees `task-assigned` via the
346
+ // task_result event channel.
347
+ return 'task-delegated';
348
+ }
349
+ if (op === 'task-update') {
350
+ const status = task?.state || r.state || r.status;
351
+ if (status === 'completed' || status === 'failed') return 'task-closed';
352
+ return 'tool-result';
353
+ }
354
+ if (op === 'agreement-respond') {
355
+ // The responder's tool result is the agreement after approve/reject.
356
+ return 'proposal-resolved';
357
+ }
358
+ void ag;
359
+ return 'tool-result';
360
+ }
361
+
362
+ function buildOutcomeOfKind(
363
+ kind: OutcomeKind,
364
+ primary: EmittedEvent | undefined,
365
+ action: Action | null,
366
+ ): Outcome {
367
+ const tool = action?.tool || primary?.tool || '';
368
+ const result = primary?.type === 'tool_result' ? primary.result : primary?.result;
369
+
370
+ switch (kind) {
371
+ case 'enter':
372
+ case 'wait':
373
+ case 'timeout':
374
+ return { kind };
375
+
376
+ case 'message-sent':
377
+ return { kind, text: primary?.message, receiverId: primary?.receiverId };
378
+
379
+ case 'message-received':
380
+ return { kind, text: primary?.text };
381
+
382
+ case 'task-assigned': {
383
+ const task: TaskRef = (result?.task || result || {}) as unknown as TaskRef;
384
+ return { kind, task };
385
+ }
386
+ case 'task-delegated': {
387
+ const task: TaskRef = (result?.task || result || {}) as unknown as TaskRef;
388
+ return { kind, task };
389
+ }
390
+ case 'task-closed': {
391
+ const raw = result?.task?.state || result?.state;
392
+ const status: 'completed' | 'failed' = raw === 'failed' ? 'failed' : 'completed';
393
+ return { kind, status, result };
394
+ }
395
+ case 'subtask-finished': {
396
+ const task: TaskRef = (result?.task || result || {}) as unknown as TaskRef;
397
+ const status = (task.state === 'failed' ? 'failed' : 'completed') as 'completed' | 'failed';
398
+ return { kind, status, task, result };
399
+ }
400
+ case 'proposal-made': {
401
+ const ag: AgreementRef | undefined = result?.agreement ?? (result?.agreementId ? result as unknown as AgreementRef : undefined);
402
+ const proposal: ProposalRef = ag
403
+ ? { agreementId: ag.agreementId, agreement: ag }
404
+ : { agreementId: result?.agreementId || '' };
405
+ return { kind, proposal };
406
+ }
407
+ case 'proposal-resolved': {
408
+ const ag: AgreementRef | undefined = result?.agreement ?? (result?.agreementId ? result as unknown as AgreementRef : undefined);
409
+ const status = ag?.proposal?.status;
410
+ const action_: 'approve' | 'reject' = status === 'rejected' ? 'reject' : 'approve';
411
+ const proposal: ProposalRef = ag
412
+ ? { agreementId: ag.agreementId, agreement: ag }
413
+ : { agreementId: result?.agreementId || '' };
414
+ return { kind, action: action_, proposal };
415
+ }
416
+ case 'tool-result':
417
+ return { kind, tool, result };
418
+
419
+ case 'error': {
420
+ const cause = primary?.error || primary?.result || 'unknown error';
421
+ const source = primary?.type === 'tool_error' || primary?.type === 'tool_result' ? 'tool' : 'unknown';
422
+ return { kind, source, cause, retryable: false };
423
+ }
424
+ case 'extension':
425
+ return { kind, name: tool || 'unknown', payload: result };
426
+ }
427
+ }
428
+
429
+ // ── applyOutcomeToCtx: persistent ctx updates from a new outcome ─────────────
430
+
431
+ /**
432
+ * Mutates ctx to absorb persistent state from a new outcome:
433
+ * - active agreement/task ids on assignment, cleared on close
434
+ * - pending proposal id on proposal-made, cleared on resolution
435
+ * - delegated task ids accumulated on delegation, used to match later subtask events
436
+ */
437
+ export function applyOutcomeToCtx(ctx: Ctx, outcome: Outcome): void {
438
+ ctx.lastOutcome = outcome;
439
+
440
+ switch (outcome.kind) {
441
+ case 'task-assigned': {
442
+ const t = outcome.task || ({} as TaskRef);
443
+ const tid = t.taskId || (t as unknown as Record<string, unknown>).id as string | undefined;
444
+ const agId = t.agreementId || t.agreement?.agreementId || ctx.pendingProposalAgreementId;
445
+ if (tid) ctx.activeTaskId = tid;
446
+ if (agId) ctx.activeAgreementId = agId;
447
+ ctx.pendingProposalAgreementId = null;
448
+ return;
449
+ }
450
+ case 'task-delegated': {
451
+ const t = outcome.task || ({} as TaskRef);
452
+ const tid = t.taskId;
453
+ const agId = t.agreementId || t.agreement?.agreementId;
454
+ if (tid && !ctx.delegatedTaskIds.includes(tid)) ctx.delegatedTaskIds.push(tid);
455
+ if (agId && !ctx.delegatedAgreementIds.includes(agId)) ctx.delegatedAgreementIds.push(agId);
456
+ return;
457
+ }
458
+ case 'proposal-made': {
459
+ const agId = outcome.proposal?.agreementId;
460
+ if (agId) ctx.pendingProposalAgreementId = agId;
461
+ return;
462
+ }
463
+ case 'proposal-resolved': {
464
+ // Approval of the agent's PRIMARY proposal (the one binding it to its
465
+ // counterparty — e.g. Ziggs ↔ user) promotes the pending id into
466
+ // activeAgreementId so transitions gated on activeAgreementId fire.
467
+ // We only do this when no agreement is currently active, so that
468
+ // approvals of CHILD sub-agreements (e.g. coffee-agent approving the
469
+ // Ziggs-issued subcontract) don't overwrite the root the orchestrator
470
+ // is bound to. Sub-agreement bookkeeping lives in delegatedAgreementIds
471
+ // (recorded on task-delegated) — not here.
472
+ if (outcome.action === 'approve' && !ctx.activeAgreementId) {
473
+ const agId = outcome.proposal?.agreementId || ctx.pendingProposalAgreementId;
474
+ if (agId) ctx.activeAgreementId = agId;
475
+ }
476
+ ctx.pendingProposalAgreementId = null;
477
+ return;
478
+ }
479
+ case 'task-closed': {
480
+ ctx.activeAgreementId = null;
481
+ ctx.activeTaskId = null;
482
+ ctx.pendingProposalAgreementId = null;
483
+ return;
484
+ }
485
+ default:
486
+ return;
487
+ }
488
+ }
@@ -0,0 +1,72 @@
1
+ import type { Action, Transition, ThinkingState } from '../types.js';
2
+
3
+ /**
4
+ * Defaults that workflow authors spread into thinking states explicitly.
5
+ * No auto-injection — what you read in the file is what runs.
6
+ *
7
+ * const dflt = thinkingDefaults({ initial: 'idle' });
8
+ *
9
+ * listening: {
10
+ * kind: 'thinking',
11
+ * ...dflt,
12
+ * prompt: { ... },
13
+ * actions: { ...dflt.actions, greetUser: { ... } },
14
+ * transitions: [
15
+ * { to: 'executing', when: o => o.kind === 'task-assigned' },
16
+ * ...dflt.transitions,
17
+ * ],
18
+ * }
19
+ */
20
+ export interface ThinkingDefaults {
21
+ actions: Record<string, Action>;
22
+ transitions: Transition[];
23
+ }
24
+
25
+ export const DEFAULT_WAIT_PROMPT = Object.freeze({
26
+ instruction: 'Do nothing - wait for the next event.',
27
+ when: 'No action is needed, you are waiting for external input, or you should pause without repeating yourself.',
28
+ format: '{"thought": "...", "action": "wait"}',
29
+ });
30
+
31
+ export function thinkingDefaults(opts: { initial: string }): ThinkingDefaults {
32
+ const { initial } = opts;
33
+ return {
34
+ actions: {
35
+ wait: {
36
+ prompt: { ...DEFAULT_WAIT_PROMPT },
37
+ produces: 'wait',
38
+ },
39
+ },
40
+ transitions: [
41
+ { to: initial, when: o => o.kind === 'wait' },
42
+ { to: initial, when: o => o.kind === 'message-sent' },
43
+ ],
44
+ };
45
+ }
46
+
47
+ /**
48
+ * Convenience builder for a `thinking` state that always carries the defaults.
49
+ * Equivalent to spreading manually but the spread points are in one place.
50
+ *
51
+ * defineThinkingState({ initial: 'idle' }, {
52
+ * prompt: { ... },
53
+ * actions: { greetUser: { ... } },
54
+ * transitions: [ { to: 'executing', when: o => o.kind === 'task-assigned' } ],
55
+ * })
56
+ */
57
+ export function defineThinkingState(
58
+ opts: { initial: string },
59
+ state: Omit<ThinkingState, 'kind' | 'actions' | 'transitions'> & {
60
+ actions: Record<string, Action>;
61
+ transitions: Transition[];
62
+ }
63
+ ): ThinkingState {
64
+ const dflt = thinkingDefaults(opts);
65
+ return {
66
+ kind: 'thinking',
67
+ prompt: state.prompt,
68
+ allowNoWait: state.allowNoWait,
69
+ actions: { ...dflt.actions, ...state.actions },
70
+ transitions: [...state.transitions, ...dflt.transitions],
71
+ };
72
+ }