@ziggs-ai/agent-sdk 0.1.3 → 0.1.5

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 (98) hide show
  1. package/README.md +3 -1
  2. package/package.json +9 -4
  3. package/src/AgentHost.ts +495 -0
  4. package/src/adapters/OpenAIAdapter.ts +146 -0
  5. package/src/agent/Agent.ts +101 -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 +105 -0
  16. package/src/ingress/normalizeIncoming.ts +162 -0
  17. package/src/memory/MemoryStore.ts +104 -0
  18. package/src/pricing/fleetDefaults.ts +218 -0
  19. package/src/pricing/fleetEvalFree.ts +24 -0
  20. package/src/pricing/fleetFreeTierA.gen.ts +12 -0
  21. package/src/pricing/fleetTierByAgentId.gen.ts +1022 -0
  22. package/src/runtime/AgentMachine.ts +364 -0
  23. package/src/runtime/PromptBuilder.ts +463 -0
  24. package/src/runtime/buildOutcome.ts +518 -0
  25. package/src/runtime/defaults.ts +75 -0
  26. package/src/runtime/runTurn.ts +691 -0
  27. package/src/runtime/validateWorkflow.ts +181 -0
  28. package/src/server/ConnectionPool.ts +155 -0
  29. package/src/server/EventQueue.ts +133 -0
  30. package/src/server/InboxCatchUp.ts +251 -0
  31. package/src/server/OutboxBuffer.ts +90 -0
  32. package/src/server/SeenMessages.ts +27 -0
  33. package/src/server/ZiggsEffectHandler.ts +409 -0
  34. package/src/server/agreements/AgreementService.ts +117 -0
  35. package/src/server/createHealthServer.ts +85 -0
  36. package/src/server/proactive/ProactiveTrigger.ts +83 -0
  37. package/src/server/runLauncher.ts +146 -0
  38. package/src/server/tasks/TaskService.ts +110 -0
  39. package/src/server/tasks/index.ts +1 -0
  40. package/src/server/telemetryIngest.ts +91 -0
  41. package/src/server/tools/index.ts +46 -0
  42. package/src/server/tools/tier1/protocolRunner.ts +133 -0
  43. package/src/server/tools/tier1/protocolTools.ts +99 -0
  44. package/src/server/tools/tier2/connectionTools.ts +75 -0
  45. package/src/server/tools/tier2/contextTools.ts +74 -0
  46. package/src/server/tools/tier2/discoveryTools.ts +34 -0
  47. package/src/server/tools/tier2/marketplaceTools.ts +25 -0
  48. package/src/server/tools/tier2/paymentTools.ts +193 -0
  49. package/src/server/ziggsconnect/ZiggsConnectClient.ts +126 -0
  50. package/src/server/ziggscontext/ZiggsContextClient.ts +137 -0
  51. package/src/server/ziggspay/ZiggsPayClient.ts +193 -0
  52. package/src/shared/ids.ts +3 -0
  53. package/src/shared/runtimeLog.ts +72 -0
  54. package/src/shared/types.ts +29 -0
  55. package/src/tasks/protocolRegistry.ts +25 -0
  56. package/src/tools/ToolManager.ts +95 -0
  57. package/src/tools/{ToolProvider.js → ToolProvider.ts} +5 -15
  58. package/src/tools/defineTool.ts +90 -0
  59. package/src/tools/index.ts +5 -0
  60. package/src/types.ts +407 -0
  61. package/src/utils/jsonExtractor.ts +100 -0
  62. package/src/ConnectionPool.js +0 -133
  63. package/src/adapters/OpenAIAdapter.js +0 -73
  64. package/src/agent/Agent.js +0 -121
  65. package/src/agent/EventQueue.js +0 -68
  66. package/src/agent/OutboxBuffer.js +0 -62
  67. package/src/cognition/PromptBuilder.js +0 -312
  68. package/src/cognition/resolveActionTool.js +0 -12
  69. package/src/cognition/runTurn.js +0 -578
  70. package/src/context/applyEffects.js +0 -133
  71. package/src/context/batch.js +0 -25
  72. package/src/context/classifyEnvelope.js +0 -82
  73. package/src/context/routingLabels.js +0 -54
  74. package/src/createHealthServer.js +0 -28
  75. package/src/formatters/HistoryFormatter.js +0 -257
  76. package/src/formatters/TaskFormatter.js +0 -180
  77. package/src/formatters/index.js +0 -9
  78. package/src/index.js +0 -76
  79. package/src/ingress/normalizeIncoming.js +0 -70
  80. package/src/runLauncher.js +0 -159
  81. package/src/shared/ids.js +0 -7
  82. package/src/shared/types.js +0 -86
  83. package/src/tasks/TaskService.js +0 -247
  84. package/src/tasks/index.js +0 -9
  85. package/src/tasks/taskCore.js +0 -229
  86. package/src/tasks/taskProtocolRegistry.js +0 -22
  87. package/src/tasks/taskProtocolRunner.js +0 -107
  88. package/src/tasks/taskProtocolTools.js +0 -87
  89. package/src/tools/ToolManager.js +0 -79
  90. package/src/tools/defineTool.js +0 -82
  91. package/src/tools/index.js +0 -11
  92. package/src/utils/jsonExtractor.js +0 -139
  93. package/src/workflow/AgentMachine.js +0 -250
  94. package/src/workflow/WorkflowRuntime.js +0 -63
  95. package/src/workflow/dsl.js +0 -287
  96. package/src/workflow/motifs.js +0 -435
  97. package/src/ziggs/runtime.js +0 -192
  98. /package/src/adapters/{index.js → index.ts} +0 -0
@@ -0,0 +1,166 @@
1
+ type AnyObj = Record<string, unknown>;
2
+
3
+ interface HistoryFormatterOptions {
4
+ showTimestamps?: boolean;
5
+ shortIds?: boolean;
6
+ }
7
+
8
+ export class HistoryFormatter {
9
+ private showTimestamps: boolean;
10
+ private shortIds: boolean;
11
+
12
+ constructor(options: HistoryFormatterOptions = {}) {
13
+ this.showTimestamps = options.showTimestamps ?? true;
14
+ this.shortIds = options.shortIds ?? false;
15
+ }
16
+
17
+ format(history: AnyObj[] | null | undefined, agentId: string, options: { mode?: string } = {}): string {
18
+ if (!history?.length) return 'No previous activity.';
19
+ const labelTypes = options.mode === 'mission';
20
+ const sorted = [...history].sort((a, b) => {
21
+ const tA = a['timestamp'] ? new Date(a['timestamp'] as string).getTime() : 0;
22
+ const tB = b['timestamp'] ? new Date(b['timestamp'] as string).getTime() : 0;
23
+ return tA - tB;
24
+ });
25
+ return sorted.map(entry => this.formatEntry(entry, agentId, labelTypes)).join('\n');
26
+ }
27
+
28
+ formatEntry(entry: AnyObj, agentId: string, labelTypes = false): string {
29
+ const timestamp = this.showTimestamps ? this.formatTime(entry['timestamp'] as string | undefined) : '';
30
+ const timePrefix = timestamp ? `[${timestamp}] ` : '';
31
+ const entryType = entry['entryType'] as string | undefined;
32
+
33
+ if (entryType === 'message' || !entryType) {
34
+ const label = labelTypes ? '[CHAT] ' : '';
35
+ return `${label}${this.formatMessage(entry, agentId, timePrefix)}`;
36
+ }
37
+ if (entryType === 'task_history') {
38
+ const label = labelTypes ? '[TASK] ' : '';
39
+ return `${label}${this.formatTaskHistory(entry, agentId, timePrefix)}`;
40
+ }
41
+ if (entryType === 'artifact') {
42
+ const line = this.formatArtifact(entry, agentId, timePrefix);
43
+ if (labelTypes) {
44
+ if (entry['content_type'] === 'thought') return `[THOUGHT] ${line}`;
45
+ const text = (entry['text'] as string) || '';
46
+ const json = text.replace(/^operation_(?:started|completed|error):/, '');
47
+ const data = json ? this.safeParseJSON(json) : null;
48
+ const label = (data?.['type'] === 'tool' || text.includes('operation_error')) ? '[TOOL] ' : '[TASK] ';
49
+ return `${label}${line}`;
50
+ }
51
+ return line;
52
+ }
53
+ return `${timePrefix}${entry['text'] || 'Unknown entry'}`;
54
+ }
55
+
56
+ formatMessage(entry: AnyObj, agentId: string, timePrefix: string): string {
57
+ const from = this.formatParticipant(entry['sender'] as AnyObj | undefined, agentId);
58
+ const to = entry['receiver'] ? ` → ${this.formatParticipant(entry['receiver'] as AnyObj, agentId)}` : '';
59
+ return `${timePrefix}${from}${to}: ${entry['text'] || ''}`;
60
+ }
61
+
62
+ formatTaskHistory(entry: AnyObj, agentId: string, timePrefix: string): string {
63
+ const service = entry['service'] as AnyObj | undefined;
64
+ const task = service?.['task'] as AnyObj | undefined;
65
+ if (!task) return `${timePrefix}Update`;
66
+ const changeType = service?.['changeType'] as string | undefined;
67
+ const taskId = this.shortId(task['taskId'] as string);
68
+ const agreementId = task['agreementId'] ? this.shortId(task['agreementId'] as string) : null;
69
+ const desc = (task['description'] as string) || 'No description';
70
+ const ids = agreementId ? `agreement=${agreementId} task=${taskId}` : `task=${taskId}`;
71
+ switch (changeType) {
72
+ case 'created': {
73
+ const ag = task['agreement'] as AnyObj | undefined;
74
+ const parties = (ag?.['parties'] as AnyObj) || {};
75
+ return `${timePrefix}Task created under agreement: "${desc}" (${ids})\n Owner: ${this.formatOwner(task, agentId)}\n Executor: ${this.formatExecutor(task, agentId)}\n Proposed to: ${parties['proposedTo'] || 'N/A'}`;
76
+ }
77
+ case 'state_changed': {
78
+ const prev = (service?.['previousState'] as AnyObj | undefined)?.['state'] || '?';
79
+ const next = (service?.['newState'] as AnyObj | undefined)?.['state'] || '?';
80
+ return `${timePrefix}Task "${desc}" (${ids}): ${prev} → ${next}`;
81
+ }
82
+ case 'proposal_responded': {
83
+ const action = (service?.['metadata'] as AnyObj | undefined)?.['action'] as string | undefined;
84
+ return `${timePrefix}Proposal ${action ?? 'responded'} for "${desc}" (${ids})`;
85
+ }
86
+ case 'processing_changed': {
87
+ const processing = (service?.['newState'] as AnyObj | undefined)?.['processing'];
88
+ return `${timePrefix}Task "${desc}" (${ids}): processing ${processing ? 'started' : 'stopped'}`;
89
+ }
90
+ default:
91
+ return `${timePrefix}${changeType}: "${desc}" (${ids})`;
92
+ }
93
+ }
94
+
95
+ formatArtifact(entry: AnyObj, _agentId: string, timePrefix: string): string {
96
+ const text = (entry['text'] as string) || '';
97
+ if (entry['content_type'] === 'thought') return `${timePrefix}Thought: ${text}`;
98
+ if (text.startsWith('operation_started:')) {
99
+ const data = this.safeParseJSON(text.replace('operation_started:', ''));
100
+ if (data?.['type'] === 'tool') return `${timePrefix}Tool starting: ${data['tool']}(${this.formatArgs(data['args'] as AnyObj)})`;
101
+ if (data?.['type'] === 'task') return `${timePrefix}Task operation: ${data['operation']}`;
102
+ return `${timePrefix}Operation started: ${data?.['type'] || 'unknown'}`;
103
+ }
104
+ if (text.startsWith('operation_completed:')) {
105
+ const data = this.safeParseJSON(text.replace('operation_completed:', ''));
106
+ if (data?.['type'] === 'tool') return `${timePrefix}Tool completed: ${data['tool']}`;
107
+ if (data?.['type'] === 'task') return `${timePrefix}Task operation completed: ${data['operation']}`;
108
+ return `${timePrefix}Operation completed: ${data?.['type'] || 'unknown'}`;
109
+ }
110
+ if (text.startsWith('operation_error:')) {
111
+ const data = this.safeParseJSON(text.replace('operation_error:', ''));
112
+ return `${timePrefix}ERROR: ${data?.['error'] || 'Unknown error'}`;
113
+ }
114
+ return `${timePrefix}${text}`;
115
+ }
116
+
117
+ formatParticipant(participant: AnyObj | null | undefined, agentId: string): string {
118
+ if (!participant) return 'unknown';
119
+ const id = (participant['id'] as string) || 'unknown';
120
+ if (participant['type'] === 'system') return 'System';
121
+ if (id === agentId) return 'You';
122
+ return `${id} (${participant['type'] || 'unknown'})`;
123
+ }
124
+
125
+ formatOwner(task: AnyObj, agentId: string): string {
126
+ const ag = task['agreement'] as AnyObj | undefined;
127
+ const creatorId = (ag?.['parties'] as AnyObj | undefined)?.['creator'] ?? task['agentId'];
128
+ return creatorId === agentId ? 'You' : String(creatorId ?? 'N/A');
129
+ }
130
+
131
+ formatExecutor(task: AnyObj, agentId: string): string {
132
+ const ag = task['agreement'] as AnyObj | undefined;
133
+ const providerId = (ag?.['parties'] as AnyObj | undefined)?.['provider'] ?? task['executorId'];
134
+ return providerId === agentId ? 'You' : String(providerId ?? 'N/A');
135
+ }
136
+
137
+ shortId(id: string | null | undefined): string {
138
+ if (!id) return '?';
139
+ if (!this.shortIds) return id;
140
+ const parts = id.split('_');
141
+ if (parts.length >= 3) return `task_...${parts[parts.length - 1]}`;
142
+ return id.length > 20 ? id.slice(0, 20) + '...' : id;
143
+ }
144
+
145
+ formatTime(timestamp: string | null | undefined): string {
146
+ if (!timestamp) return '??:??';
147
+ const date = new Date(timestamp);
148
+ if (isNaN(date.getTime())) return '??:??';
149
+ return date.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false, timeZone: 'UTC' });
150
+ }
151
+
152
+ formatArgs(args: AnyObj | null | undefined): string {
153
+ if (!args || typeof args !== 'object') return '';
154
+ const entries = Object.entries(args);
155
+ if (entries.length === 0) return '';
156
+ if (entries.length <= 3) return entries.map(([k, v]) => `${k}=${JSON.stringify(v)}`).join(', ');
157
+ return JSON.stringify(args);
158
+ }
159
+
160
+ private safeParseJSON(str: string): AnyObj | null {
161
+ try { return JSON.parse(str) as AnyObj; }
162
+ catch { return null; }
163
+ }
164
+ }
165
+
166
+ export const historyFormatter = new HistoryFormatter();
@@ -0,0 +1,2 @@
1
+ export { HistoryFormatter, historyFormatter } from './HistoryFormatter.js';
2
+ export { AgreementFormatter, agreementFormatter } from './AgreementFormatter.js';
package/src/index.ts ADDED
@@ -0,0 +1,105 @@
1
+ /**
2
+ * @ziggs-ai/agent-sdk public surface.
3
+ *
4
+ * Foundation:
5
+ * • Outcome union — discriminated, hybrid (closed core + extension)
6
+ * • Workflow shape — explicit kind: 'parked' | 'thinking'
7
+ * • Visible defaults — thinkingDefaults({ initial }) spread by author
8
+ * • Validation at load — defineAgent throws on hard problems, warns on soft
9
+ */
10
+
11
+ export { defineAgent } from './defineAgent.js';
12
+ export { AgentHost, createAgent, createAgentPool } from './AgentHost.js';
13
+ export { Agent } from './agent/Agent.js';
14
+ export type { TickInput, TickOutput, AgentOptions } from './agent/Agent.js';
15
+ export type { SessionResolver, AgentHostOptions } from './AgentHost.js';
16
+ export { createZiggsEffectHandler } from './server/ZiggsEffectHandler.js';
17
+ export type { ZiggsEffectDeps } from './server/ZiggsEffectHandler.js';
18
+ export { EventQueue } from './server/EventQueue.js';
19
+ export { OutboxBuffer } from './server/OutboxBuffer.js';
20
+ export { normalizeIncomingEvent } from './ingress/normalizeIncoming.js';
21
+
22
+ // Runtime primitives
23
+ export { AgentMachine, type TransitionEvent } from './runtime/AgentMachine.js';
24
+ export { runTurn } from './runtime/runTurn.js';
25
+ export { PromptBuilder } from './runtime/PromptBuilder.js';
26
+ export { thinkingDefaults, defineThinkingState, DEFAULT_WAIT_PROMPT } from './runtime/defaults.js';
27
+ export { validateWorkflow, WorkflowValidationError } from './runtime/validateWorkflow.js';
28
+ export { outcomeFromEvent, outcomeFromActionResult, applyOutcomeToCtx } from './runtime/buildOutcome.js';
29
+
30
+ // Types
31
+ export type {
32
+ Outcome, OutcomeKind,
33
+ Ctx, Identity, HistoryEntry, ToolResultEntry,
34
+ Workflow, State, ParkedState, ThinkingState,
35
+ Action, Transition, PromptDef, ActionPrompt, Produces, ProducesFn,
36
+ AgreementRef, TaskRef, ProposalRef,
37
+ AgentConfig, DefineAgentInput, ServicesConfig,
38
+ // Effects — Agent↔Server boundary
39
+ Effect, EffectKind, EffectResult, EffectResultMap, EffectHandler,
40
+ LlmMessage, LlmToolCall, LlmToolSchema, LlmResponse,
41
+ ContextSnapshot, ReadContextOpts, ToolCallResult, RecordedEntry,
42
+ } from './types.js';
43
+
44
+ export { default } from './AgentHost.js';
45
+
46
+ // ── Supporting modules ──────────────────────────────────────────────────────
47
+
48
+ export { validateContext, ContextWarning } from './cognition/validateContext.js';
49
+ export { runLauncher } from './server/runLauncher.js';
50
+ export { ConnectionPool } from './server/ConnectionPool.js';
51
+ export { createHealthServer } from './server/createHealthServer.js';
52
+ export { TaskService } from './server/tasks/TaskService.js';
53
+ export { AgreementService } from './server/agreements/AgreementService.js';
54
+ export { ToolManager } from './tools/ToolManager.js';
55
+ export { ToolProvider } from './tools/ToolProvider.js';
56
+ export { defineTool } from './tools/defineTool.js';
57
+ export { OpenAIAdapter } from './adapters/OpenAIAdapter.js';
58
+ export { extractJSON, safeParseJSON } from './utils/jsonExtractor.js';
59
+ export { HistoryFormatter, historyFormatter, AgreementFormatter, agreementFormatter } from './formatters/index.js';
60
+ export { ProactiveTrigger } from './server/proactive/ProactiveTrigger.js';
61
+ export { ZiggsPayClient, createZiggsPayClient } from './server/ziggspay/ZiggsPayClient.js';
62
+ export { ZiggsConnectClient, createZiggsConnectClient } from './server/ziggsconnect/ZiggsConnectClient.js';
63
+ export { ZiggsContextClient } from './server/ziggscontext/ZiggsContextClient.js';
64
+ export type {
65
+ ZiggsContextClientOptions,
66
+ ContextReachDescriptor,
67
+ DelegateContextGrantParams,
68
+ } from './server/ziggscontext/ZiggsContextClient.js';
69
+ // All agent tool bundles live under server/tools/ (tier1 = protocol grammar,
70
+ // tier2 = opt-in HTTP capability bundles). Single home: server/tools/index.ts.
71
+ export {
72
+ // Tier 1 — protocol grammar (framework-managed via taskTools).
73
+ PROTOCOL_TOOLS, agreementProposeTool, agreementSubcontractTool,
74
+ agreementRespondTool, agreementCounterProposalTool, agreementCheckProposalTool,
75
+ taskSpawnTool, taskUpdateTool, taskUpdatePlanStepTool,
76
+ dispatchProtocolOp,
77
+ // Tier 2 — opt-in capability bundles (spread into `tools:`).
78
+ PAYMENT_TOOLS, paymentBalanceTool, paymentTransferTool, paymentHoldTool, paymentReleaseTool, paymentResolveWalletTool,
79
+ CONNECTION_TOOLS, connectionProxyTool, connectionListGrantsTool,
80
+ DISCOVERY_TOOLS, agentSearchTool, agentGetTool,
81
+ MARKETPLACE_TOOLS, marketplaceViewTool,
82
+ } from './server/tools/index.js';
83
+ export { PROTOCOL_TOOL_NAMES, PROTOCOL_TOOL_TO_OPERATION, mapProtocolToolToOperation, isProtocolToolName } from './tasks/protocolRegistry.js';
84
+ export { InMemoryStore, FileMemoryStore } from './memory/MemoryStore.js';
85
+ export type { MemoryStore } from './memory/MemoryStore.js';
86
+ export type { SessionStore } from './AgentHost.js';
87
+ export { classifyIncomingEvent } from './context/classifyEnvelope.js';
88
+ export { buildContextUpdates, CONTEXT_RESET } from './context/applyEffects.js';
89
+ export { getBatchEvents, isTaskResultRelevantToAgent } from './context/batch.js';
90
+ export { classifyWorkflowEvent, findTaskResult, unwrapBatchEvent, findIncomingTaskResult } from './context/routingLabels.js';
91
+ export {
92
+ WebSocketClient,
93
+ MessagesClient,
94
+ ArtifactsClient,
95
+ ScopeClient,
96
+ AgentSearchClient,
97
+ getBackendUrl,
98
+ getWebSocketUrl,
99
+ } from '@ziggs-ai/api-client';
100
+
101
+ export { runInboxCatchUp } from './server/InboxCatchUp.js';
102
+ export type { InboxCatchUpDeps, InboxCatchUpResult } from './server/InboxCatchUp.js';
103
+ export { SeenMessages } from './server/SeenMessages.js';
104
+ export { runtimeLog, resetRuntimeLogLevelCache } from './shared/runtimeLog.js';
105
+ export type { RuntimeLogLevel } from './shared/runtimeLog.js';
@@ -0,0 +1,162 @@
1
+ import { OPEN_AGREEMENT_TARGET } from '@ziggs-ai/api-client';
2
+
3
+ /** Backend wire operations that map to FSM `proposal-resolved`. */
4
+ const PROPOSAL_RESOLVED_OPERATIONS = new Set([
5
+ 'proposal_approved',
6
+ 'proposal_rejected',
7
+ 'proposal_approved_execute',
8
+ ]);
9
+
10
+ export interface NormalizedEvent {
11
+ chatId: string | null;
12
+ event: {
13
+ senderId?: string;
14
+ senderType?: string;
15
+ receiverId?: string;
16
+ timestamp: number;
17
+ type: 'task_result' | 'message' | 'agreement_lifecycle';
18
+ operation?: string;
19
+ agreementId?: string;
20
+ result?: unknown;
21
+ text?: string;
22
+ };
23
+ shouldProcess: boolean;
24
+ reason: string | null;
25
+ }
26
+
27
+ interface WireParties {
28
+ creator?: string;
29
+ provider?: string;
30
+ payer?: string;
31
+ proposedTo?: string;
32
+ }
33
+
34
+ interface WireTask {
35
+ taskId?: string;
36
+ agentId?: string;
37
+ executorId?: string;
38
+ payerId?: string;
39
+ proposedTo?: string;
40
+ createdBy?: string;
41
+ parentTaskId?: string;
42
+ state?: string;
43
+ proposal?: unknown;
44
+ agreement?: { parties?: WireParties };
45
+ }
46
+
47
+ interface WireMetadata {
48
+ chatId?: string;
49
+ chat_id?: string;
50
+ entryType?: string;
51
+ content_type?: string;
52
+ contentType?: string;
53
+ receiverId?: string;
54
+ operation?: string;
55
+ agreementId?: string;
56
+ sender?: { id?: string; type?: string };
57
+ receiver?: { id?: string };
58
+ to?: { id?: string };
59
+ task?: WireTask;
60
+ [key: string]: unknown;
61
+ }
62
+
63
+ interface NormalizeArgs {
64
+ text: string;
65
+ metadata?: WireMetadata;
66
+ ownAgentId?: string | null;
67
+ }
68
+
69
+ function normalizeChatId(metadata: WireMetadata): string | null {
70
+ return metadata.chatId || metadata.chat_id || null;
71
+ }
72
+
73
+ function normalizeReceiverId(metadata: WireMetadata, taskData: WireTask | null): string | null {
74
+ return metadata.receiver?.id || metadata.receiverId || metadata.to?.id || taskData?.executorId || taskData?.proposedTo || null;
75
+ }
76
+
77
+ function isTaskRelated(metadata: WireMetadata, taskData: WireTask | null): boolean {
78
+ if (!taskData?.taskId) return false;
79
+ const entryType = metadata.entryType || 'message';
80
+ const contentType = metadata.content_type || metadata.contentType || 'text';
81
+ return (
82
+ entryType === 'notification' || entryType === 'task_history'
83
+ || contentType === 'task' || contentType === 'task_update'
84
+ || Boolean(taskData.state || taskData.proposal || taskData.parentTaskId || taskData.executorId || taskData.agentId || taskData.payerId || taskData.createdBy)
85
+ );
86
+ }
87
+
88
+ function isRelevantForAgent(ids: (string | null | undefined)[], ownAgentId: string | null): boolean {
89
+ if (!ownAgentId) return true;
90
+ return ids.includes(ownAgentId) || ids.includes(OPEN_AGREEMENT_TARGET);
91
+ }
92
+
93
+ function isAgreementLifecycleNotification(metadata: WireMetadata): boolean {
94
+ const entryType = metadata.entryType || 'message';
95
+ const operation = metadata.operation;
96
+ return (
97
+ entryType === 'notification' &&
98
+ typeof operation === 'string' &&
99
+ PROPOSAL_RESOLVED_OPERATIONS.has(operation)
100
+ );
101
+ }
102
+
103
+ export function normalizeIncomingEvent({ text, metadata = {}, ownAgentId = null }: NormalizeArgs): NormalizedEvent {
104
+ const taskData = metadata.task || null;
105
+ const chatId = normalizeChatId(metadata);
106
+ const receiverId = normalizeReceiverId(metadata, taskData);
107
+ const senderType = metadata.sender?.type ? String(metadata.sender.type).toUpperCase() : undefined;
108
+
109
+ const base = {
110
+ senderId: metadata.sender?.id,
111
+ senderType,
112
+ receiverId: receiverId || undefined,
113
+ timestamp: Date.now(),
114
+ };
115
+
116
+ if (isAgreementLifecycleNotification(metadata)) {
117
+ const relevant =
118
+ !ownAgentId ||
119
+ !receiverId ||
120
+ receiverId === ownAgentId ||
121
+ receiverId === OPEN_AGREEMENT_TARGET;
122
+ const agreementId =
123
+ typeof metadata.agreementId === 'string' ? metadata.agreementId : '';
124
+ return {
125
+ chatId,
126
+ event: {
127
+ ...base,
128
+ type: 'agreement_lifecycle',
129
+ operation: metadata.operation,
130
+ agreementId,
131
+ text,
132
+ },
133
+ shouldProcess: relevant,
134
+ reason: relevant ? null : 'message_not_targeted_to_agent',
135
+ };
136
+ }
137
+
138
+ if (isTaskRelated(metadata, taskData)) {
139
+ const receiverFromMeta = metadata.receiver?.id || metadata.receiverId || null;
140
+ const parties = taskData!.agreement?.parties ?? {};
141
+ const candidateIds = [
142
+ parties.creator, parties.provider, parties.payer, parties.proposedTo,
143
+ taskData!.agentId, taskData!.executorId, taskData!.payerId, taskData!.proposedTo, taskData!.createdBy,
144
+ receiverFromMeta,
145
+ ].filter(Boolean);
146
+ const relevant = isRelevantForAgent(candidateIds, ownAgentId);
147
+ return {
148
+ chatId,
149
+ event: { ...base, type: 'task_result', result: taskData, text },
150
+ shouldProcess: relevant,
151
+ reason: relevant ? null : 'task_not_targeted_to_agent',
152
+ };
153
+ }
154
+
155
+ const relevant = !ownAgentId || !receiverId || receiverId === ownAgentId || receiverId === OPEN_AGREEMENT_TARGET;
156
+ return {
157
+ chatId,
158
+ event: { ...base, type: 'message', text },
159
+ shouldProcess: relevant,
160
+ reason: relevant ? null : 'message_not_targeted_to_agent',
161
+ };
162
+ }
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Agent-private long-term memory.
3
+ *
4
+ * This is the SDK's contract; the *storage* is operator-provided. The agent
5
+ * picks the key shape (often `counterparty:<id>`, sometimes `agreement:<id>:notes`,
6
+ * etc.) — the SDK does not enforce one.
7
+ *
8
+ * Reference implementations: `InMemoryStore` (default; non-durable) and
9
+ * `FileMemoryStore` (single JSON file; useful for local dev). Anything
10
+ * production-grade (sqlite, redis, postgres, …) is operator-owned.
11
+ *
12
+ * Aligns with the project's "agents are third-party" framing: an agent's
13
+ * private notes are its own concern, not the backend's.
14
+ */
15
+ export interface MemoryStore {
16
+ get(key: string): Promise<unknown | undefined>;
17
+ put(key: string, value: unknown): Promise<void>;
18
+ list(prefix: string): Promise<{ key: string; value: unknown }[]>;
19
+ delete?(key: string): Promise<void>;
20
+ }
21
+
22
+ /** Non-durable reference impl. Per-process; lost on restart. */
23
+ export class InMemoryStore implements MemoryStore {
24
+ private readonly store = new Map<string, unknown>();
25
+
26
+ async get(key: string): Promise<unknown | undefined> {
27
+ return this.store.get(key);
28
+ }
29
+
30
+ async put(key: string, value: unknown): Promise<void> {
31
+ this.store.set(key, value);
32
+ }
33
+
34
+ async list(prefix: string): Promise<{ key: string; value: unknown }[]> {
35
+ const out: { key: string; value: unknown }[] = [];
36
+ for (const [k, v] of this.store) {
37
+ if (k.startsWith(prefix)) out.push({ key: k, value: v });
38
+ }
39
+ return out;
40
+ }
41
+
42
+ async delete(key: string): Promise<void> {
43
+ this.store.delete(key);
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Single-file JSON store. Reads on startup, writes on every put — fine for
49
+ * local dev and example agents; not for production (no concurrency control,
50
+ * no atomicity). Operators with serious durability needs ship their own
51
+ * `MemoryStore` (sqlite, redis, …) and pass it via `defineAgent({ memory })`.
52
+ */
53
+ export class FileMemoryStore implements MemoryStore {
54
+ private cache: Record<string, unknown> | null = null;
55
+ private loading: Promise<void> | null = null;
56
+
57
+ constructor(private readonly path: string) {}
58
+
59
+ private async _ensureLoaded(): Promise<void> {
60
+ if (this.cache) return;
61
+ if (!this.loading) {
62
+ this.loading = (async () => {
63
+ try {
64
+ const fs = await import('fs/promises');
65
+ const txt = await fs.readFile(this.path, 'utf-8');
66
+ this.cache = JSON.parse(txt) as Record<string, unknown>;
67
+ } catch {
68
+ this.cache = {};
69
+ }
70
+ })();
71
+ }
72
+ await this.loading;
73
+ }
74
+
75
+ private async _flush(): Promise<void> {
76
+ if (!this.cache) return;
77
+ const fs = await import('fs/promises');
78
+ await fs.writeFile(this.path, JSON.stringify(this.cache, null, 2), 'utf-8');
79
+ }
80
+
81
+ async get(key: string): Promise<unknown | undefined> {
82
+ await this._ensureLoaded();
83
+ return this.cache![key];
84
+ }
85
+
86
+ async put(key: string, value: unknown): Promise<void> {
87
+ await this._ensureLoaded();
88
+ this.cache![key] = value;
89
+ await this._flush();
90
+ }
91
+
92
+ async list(prefix: string): Promise<{ key: string; value: unknown }[]> {
93
+ await this._ensureLoaded();
94
+ return Object.entries(this.cache!)
95
+ .filter(([k]) => k.startsWith(prefix))
96
+ .map(([key, value]) => ({ key, value }));
97
+ }
98
+
99
+ async delete(key: string): Promise<void> {
100
+ await this._ensureLoaded();
101
+ delete this.cache![key];
102
+ await this._flush();
103
+ }
104
+ }