@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.
- package/README.md +3 -1
- package/package.json +9 -4
- package/src/AgentHost.ts +495 -0
- package/src/adapters/OpenAIAdapter.ts +146 -0
- package/src/agent/Agent.ts +101 -0
- package/src/cognition/validateContext.ts +95 -0
- package/src/context/applyEffects.ts +80 -0
- package/src/context/batch.ts +17 -0
- package/src/context/classifyEnvelope.ts +38 -0
- package/src/context/routingLabels.ts +46 -0
- package/src/defineAgent.ts +62 -0
- package/src/formatters/AgreementFormatter.ts +111 -0
- package/src/formatters/HistoryFormatter.ts +166 -0
- package/src/formatters/index.ts +2 -0
- package/src/index.ts +105 -0
- package/src/ingress/normalizeIncoming.ts +162 -0
- package/src/memory/MemoryStore.ts +104 -0
- package/src/pricing/fleetDefaults.ts +218 -0
- package/src/pricing/fleetEvalFree.ts +24 -0
- package/src/pricing/fleetFreeTierA.gen.ts +12 -0
- package/src/pricing/fleetTierByAgentId.gen.ts +1022 -0
- package/src/runtime/AgentMachine.ts +364 -0
- package/src/runtime/PromptBuilder.ts +463 -0
- package/src/runtime/buildOutcome.ts +518 -0
- package/src/runtime/defaults.ts +75 -0
- package/src/runtime/runTurn.ts +691 -0
- package/src/runtime/validateWorkflow.ts +181 -0
- package/src/server/ConnectionPool.ts +155 -0
- package/src/server/EventQueue.ts +133 -0
- package/src/server/InboxCatchUp.ts +251 -0
- package/src/server/OutboxBuffer.ts +90 -0
- package/src/server/SeenMessages.ts +27 -0
- package/src/server/ZiggsEffectHandler.ts +409 -0
- package/src/server/agreements/AgreementService.ts +117 -0
- package/src/server/createHealthServer.ts +85 -0
- package/src/server/proactive/ProactiveTrigger.ts +83 -0
- package/src/server/runLauncher.ts +146 -0
- package/src/server/tasks/TaskService.ts +110 -0
- package/src/server/tasks/index.ts +1 -0
- package/src/server/telemetryIngest.ts +91 -0
- package/src/server/tools/index.ts +46 -0
- package/src/server/tools/tier1/protocolRunner.ts +133 -0
- package/src/server/tools/tier1/protocolTools.ts +99 -0
- package/src/server/tools/tier2/connectionTools.ts +75 -0
- package/src/server/tools/tier2/contextTools.ts +74 -0
- package/src/server/tools/tier2/discoveryTools.ts +34 -0
- package/src/server/tools/tier2/marketplaceTools.ts +25 -0
- package/src/server/tools/tier2/paymentTools.ts +193 -0
- package/src/server/ziggsconnect/ZiggsConnectClient.ts +126 -0
- package/src/server/ziggscontext/ZiggsContextClient.ts +137 -0
- package/src/server/ziggspay/ZiggsPayClient.ts +193 -0
- package/src/shared/ids.ts +3 -0
- package/src/shared/runtimeLog.ts +72 -0
- package/src/shared/types.ts +29 -0
- package/src/tasks/protocolRegistry.ts +25 -0
- package/src/tools/ToolManager.ts +95 -0
- package/src/tools/{ToolProvider.js → ToolProvider.ts} +5 -15
- package/src/tools/defineTool.ts +90 -0
- package/src/tools/index.ts +5 -0
- package/src/types.ts +407 -0
- package/src/utils/jsonExtractor.ts +100 -0
- package/src/ConnectionPool.js +0 -133
- package/src/adapters/OpenAIAdapter.js +0 -73
- package/src/agent/Agent.js +0 -121
- package/src/agent/EventQueue.js +0 -68
- package/src/agent/OutboxBuffer.js +0 -62
- package/src/cognition/PromptBuilder.js +0 -312
- package/src/cognition/resolveActionTool.js +0 -12
- package/src/cognition/runTurn.js +0 -578
- package/src/context/applyEffects.js +0 -133
- package/src/context/batch.js +0 -25
- package/src/context/classifyEnvelope.js +0 -82
- package/src/context/routingLabels.js +0 -54
- package/src/createHealthServer.js +0 -28
- package/src/formatters/HistoryFormatter.js +0 -257
- package/src/formatters/TaskFormatter.js +0 -180
- package/src/formatters/index.js +0 -9
- package/src/index.js +0 -76
- package/src/ingress/normalizeIncoming.js +0 -70
- package/src/runLauncher.js +0 -159
- package/src/shared/ids.js +0 -7
- package/src/shared/types.js +0 -86
- package/src/tasks/TaskService.js +0 -247
- package/src/tasks/index.js +0 -9
- package/src/tasks/taskCore.js +0 -229
- package/src/tasks/taskProtocolRegistry.js +0 -22
- package/src/tasks/taskProtocolRunner.js +0 -107
- package/src/tasks/taskProtocolTools.js +0 -87
- package/src/tools/ToolManager.js +0 -79
- package/src/tools/defineTool.js +0 -82
- package/src/tools/index.js +0 -11
- package/src/utils/jsonExtractor.js +0 -139
- package/src/workflow/AgentMachine.js +0 -250
- package/src/workflow/WorkflowRuntime.js +0 -63
- package/src/workflow/dsl.js +0 -287
- package/src/workflow/motifs.js +0 -435
- package/src/ziggs/runtime.js +0 -192
- /package/src/adapters/{index.js → index.ts} +0 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Foundation types for the agent SDK workflow runtime.
|
|
3
|
+
*
|
|
4
|
+
* One outcome vocabulary, one ctx shape, one transition signature.
|
|
5
|
+
* Every state — parked or thinking — re-enters with exactly one Outcome.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// ── Refs (opaque payloads from the protocol layer) ───────────────────────────
|
|
9
|
+
|
|
10
|
+
export interface AgreementRef {
|
|
11
|
+
agreementId: string;
|
|
12
|
+
parties?: {
|
|
13
|
+
creator?: string;
|
|
14
|
+
provider?: string;
|
|
15
|
+
payer?: string;
|
|
16
|
+
proposedTo?: string;
|
|
17
|
+
};
|
|
18
|
+
proposal?: { status?: 'pending' | 'approved' | 'rejected' | 'cancelled' };
|
|
19
|
+
money?: { price?: number; paymentStatus?: string };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface TaskRef {
|
|
23
|
+
taskId: string;
|
|
24
|
+
agreementId?: string;
|
|
25
|
+
agreement?: AgreementRef;
|
|
26
|
+
state?: string;
|
|
27
|
+
description?: string;
|
|
28
|
+
parentTaskId?: string;
|
|
29
|
+
proposal?: { status?: 'pending' | 'approved' | 'rejected' | 'cancelled' };
|
|
30
|
+
proposedToIsYou?: boolean;
|
|
31
|
+
providerIsYou?: boolean;
|
|
32
|
+
creatorIsYou?: boolean;
|
|
33
|
+
payerIsYou?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ProposalRef {
|
|
37
|
+
agreementId: string;
|
|
38
|
+
agreement?: AgreementRef;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ── Outcomes (hybrid: closed core + extension) ───────────────────────────────
|
|
42
|
+
|
|
43
|
+
export type OutcomeKind =
|
|
44
|
+
| 'enter'
|
|
45
|
+
| 'wait'
|
|
46
|
+
| 'timeout'
|
|
47
|
+
| 'message-sent'
|
|
48
|
+
| 'message-received'
|
|
49
|
+
| 'task-assigned'
|
|
50
|
+
| 'task-delegated'
|
|
51
|
+
| 'task-closed'
|
|
52
|
+
| 'subtask-finished'
|
|
53
|
+
| 'proposal-made'
|
|
54
|
+
| 'proposal-resolved'
|
|
55
|
+
| 'tool-result'
|
|
56
|
+
| 'error'
|
|
57
|
+
| 'extension';
|
|
58
|
+
|
|
59
|
+
export type Outcome =
|
|
60
|
+
| { kind: 'enter' }
|
|
61
|
+
| { kind: 'wait' }
|
|
62
|
+
| { kind: 'timeout' }
|
|
63
|
+
| { kind: 'message-sent'; text?: string; receiverId?: string }
|
|
64
|
+
| { kind: 'message-received'; text?: string; senderId?: string; senderType?: string }
|
|
65
|
+
| { kind: 'task-assigned'; task: TaskRef }
|
|
66
|
+
| { kind: 'task-delegated'; task: TaskRef }
|
|
67
|
+
| { kind: 'task-closed'; status: 'completed' | 'failed'; result?: unknown }
|
|
68
|
+
| { kind: 'subtask-finished'; status: 'completed' | 'failed'; task: TaskRef; result?: unknown }
|
|
69
|
+
| { kind: 'proposal-made'; proposal: ProposalRef }
|
|
70
|
+
| { kind: 'proposal-resolved'; action: 'approve' | 'reject'; proposal: ProposalRef }
|
|
71
|
+
| { kind: 'tool-result'; tool: string; result: unknown }
|
|
72
|
+
| {
|
|
73
|
+
kind: 'error';
|
|
74
|
+
source: 'llm' | 'tool' | 'protocol' | 'validation' | 'unknown';
|
|
75
|
+
cause: unknown;
|
|
76
|
+
retryable: boolean;
|
|
77
|
+
}
|
|
78
|
+
| { kind: 'extension'; name: string; payload: unknown };
|
|
79
|
+
|
|
80
|
+
// ── Ctx (memory only — no event-derived flags) ───────────────────────────────
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* A session is the Server-side abstraction the Agent operates against:
|
|
84
|
+
* `{ sessionId, ctx, queue, parked }`. `Identity` is the Agent's view of
|
|
85
|
+
* which session this tick belongs to. `sessionId` is opaque to the Agent —
|
|
86
|
+
* it's a string the Server uses to route inbound events and outbound
|
|
87
|
+
* effects. (Today, the Ziggs transport derives sessionId from `chatId` at
|
|
88
|
+
* the wire boundary; non-chat surfaces will produce sessionIds of other
|
|
89
|
+
* shapes.)
|
|
90
|
+
*/
|
|
91
|
+
export interface Identity {
|
|
92
|
+
agentId: string;
|
|
93
|
+
sessionId: string;
|
|
94
|
+
laneKey: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface HistoryEntry {
|
|
98
|
+
entryType?: string;
|
|
99
|
+
text?: string;
|
|
100
|
+
sender?: { id?: string; type?: string };
|
|
101
|
+
receiver?: { id?: string };
|
|
102
|
+
timestamp?: number;
|
|
103
|
+
[key: string]: unknown;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface ToolResultEntry {
|
|
107
|
+
tool: string;
|
|
108
|
+
result?: unknown;
|
|
109
|
+
error?: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Per-lane runtime state. Outcome is "what just happened"; Ctx is "what I know."
|
|
114
|
+
*
|
|
115
|
+
* Persistent identifiers (activeAgreementId, pendingProposalAgreementId,
|
|
116
|
+
* delegatedTaskIds) survive across outcomes — they're the memory of which
|
|
117
|
+
* agreement the agent is currently bound to and which subtasks it's awaiting.
|
|
118
|
+
*/
|
|
119
|
+
export interface Ctx {
|
|
120
|
+
identity: Identity;
|
|
121
|
+
|
|
122
|
+
// Persistent protocol state
|
|
123
|
+
activeAgreementId: string | null;
|
|
124
|
+
activeTaskId: string | null;
|
|
125
|
+
pendingProposalAgreementId: string | null;
|
|
126
|
+
delegatedAgreementIds: string[];
|
|
127
|
+
delegatedTaskIds: string[];
|
|
128
|
+
|
|
129
|
+
// Per-turn working memory
|
|
130
|
+
toolResults: ToolResultEntry[];
|
|
131
|
+
|
|
132
|
+
// Last event/turn that caused this entry
|
|
133
|
+
lastOutcome: Outcome;
|
|
134
|
+
|
|
135
|
+
// Server-provided context (history, agreements, agents, users) is fetched
|
|
136
|
+
// on demand inside runTurn; not persisted on Ctx.
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ── Workflow shape ───────────────────────────────────────────────────────────
|
|
140
|
+
|
|
141
|
+
export interface PromptDef {
|
|
142
|
+
role?: string;
|
|
143
|
+
goal?: string;
|
|
144
|
+
context?: string;
|
|
145
|
+
constraints?: string;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface ActionPrompt {
|
|
149
|
+
instruction: string;
|
|
150
|
+
format?: string;
|
|
151
|
+
when?: string;
|
|
152
|
+
examples?: Array<{ scenario?: string; input?: string; output?: string; query?: string }>;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* `produces` is the outcome-kind contract for an action. Static form for
|
|
157
|
+
* actions that always emit the same kind (most common); function form for
|
|
158
|
+
* tool-bound actions where the result determines the kind (e.g. success vs
|
|
159
|
+
* error). The runtime composes the kind with action metadata to build the
|
|
160
|
+
* full Outcome.
|
|
161
|
+
*/
|
|
162
|
+
export type ProducesFn = (result: unknown, args: unknown) => OutcomeKind;
|
|
163
|
+
export type Produces = OutcomeKind | ProducesFn;
|
|
164
|
+
|
|
165
|
+
export interface Action {
|
|
166
|
+
prompt: ActionPrompt;
|
|
167
|
+
tool?: string | null;
|
|
168
|
+
produces: Produces;
|
|
169
|
+
/**
|
|
170
|
+
* When true, the tool loop exits immediately after this action's tool result,
|
|
171
|
+
* even if the result is non-terminal. Prevents the LLM from issuing additional
|
|
172
|
+
* tool calls in the same turn (e.g. calling agreement_subcontract right after
|
|
173
|
+
* agent_search before the FSM can route to evaluatingCandidates).
|
|
174
|
+
*/
|
|
175
|
+
terminatesLoop?: boolean;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface Transition {
|
|
179
|
+
to: string;
|
|
180
|
+
when?: (outcome: Outcome, ctx: Ctx) => boolean;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export interface ParkedState {
|
|
184
|
+
kind: 'parked';
|
|
185
|
+
transitions: Transition[];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export interface ThinkingState {
|
|
189
|
+
kind: 'thinking';
|
|
190
|
+
prompt: PromptDef;
|
|
191
|
+
actions: Record<string, Action>;
|
|
192
|
+
transitions: Transition[];
|
|
193
|
+
/**
|
|
194
|
+
* Opt out of the validateWorkflow warning that flags thinking states
|
|
195
|
+
* missing a `wait` action / activeWait fallthrough. Use sparingly — silent
|
|
196
|
+
* states that loop on themselves are usually a bug.
|
|
197
|
+
*/
|
|
198
|
+
allowNoWait?: boolean;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export type State = ParkedState | ThinkingState;
|
|
202
|
+
|
|
203
|
+
export interface Workflow {
|
|
204
|
+
id: string;
|
|
205
|
+
description: string;
|
|
206
|
+
initial: string;
|
|
207
|
+
states: Record<string, State>;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// ── Effects (the Agent ↔ Server boundary) ────────────────────────────────────
|
|
211
|
+
//
|
|
212
|
+
// The Agent is pure: its only side channel is `EffectHandler`. Every impure
|
|
213
|
+
// operation — reading context, calling the LLM, executing a tool, sending a
|
|
214
|
+
// message — is expressed as an Effect. The Server implements the handler;
|
|
215
|
+
// the Agent emits effects via `await eff({ kind: ..., ... })` and consumes
|
|
216
|
+
// the typed result. This is the Node.js-style "sync dress over async" loop:
|
|
217
|
+
// `tick` reads top-to-bottom but every `await` is a yield to the Server.
|
|
218
|
+
|
|
219
|
+
export interface LlmToolCall {
|
|
220
|
+
id: string;
|
|
221
|
+
function: { name: string; arguments: string };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export interface LlmMessage {
|
|
225
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
226
|
+
content?: string;
|
|
227
|
+
tool_calls?: LlmToolCall[];
|
|
228
|
+
tool_call_id?: string;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export interface LlmToolSchema {
|
|
232
|
+
type: 'function';
|
|
233
|
+
function: { name: string; description?: string; parameters?: unknown };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export interface LlmResponse {
|
|
237
|
+
content?: string;
|
|
238
|
+
tool_calls?: LlmToolCall[];
|
|
239
|
+
usage?: {
|
|
240
|
+
total_tokens?: number;
|
|
241
|
+
prompt_tokens?: number;
|
|
242
|
+
completion_tokens?: number;
|
|
243
|
+
totalTokens?: number;
|
|
244
|
+
promptTokens?: number;
|
|
245
|
+
completionTokens?: number;
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export interface ContextSnapshot {
|
|
250
|
+
history: HistoryEntry[];
|
|
251
|
+
agreements: unknown[];
|
|
252
|
+
agents: unknown[];
|
|
253
|
+
users: unknown[];
|
|
254
|
+
timelineSummary?: string | null;
|
|
255
|
+
latestSequence?: string | null;
|
|
256
|
+
unavailable?: boolean;
|
|
257
|
+
/** Index signature so PromptBuilder's `ServerContext` can accept it directly. */
|
|
258
|
+
[key: string]: unknown;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export interface ReadContextOpts {
|
|
262
|
+
maxMessages?: number;
|
|
263
|
+
maxCharacters?: number;
|
|
264
|
+
maxTaskHistory?: number;
|
|
265
|
+
summarize?: boolean;
|
|
266
|
+
sinceSequence?: string;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Result of executing a single tool. Tool execution may emit multiple
|
|
271
|
+
* "events" (terminal markers like `message_sent`, `waited`, etc.) that the
|
|
272
|
+
* cognition loop inspects to decide whether to keep looping. Those carry
|
|
273
|
+
* through here as `events`; `result` is the value returned to the LLM.
|
|
274
|
+
*/
|
|
275
|
+
export interface ToolCallResult {
|
|
276
|
+
ok: boolean;
|
|
277
|
+
result?: unknown;
|
|
278
|
+
error?: string;
|
|
279
|
+
events?: unknown[];
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export interface RecordedEntry {
|
|
283
|
+
kind: string;
|
|
284
|
+
text?: string;
|
|
285
|
+
[k: string]: unknown;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
import type { MemoryStore } from './memory/MemoryStore.js';
|
|
289
|
+
|
|
290
|
+
export type EffectKind =
|
|
291
|
+
| 'read-context'
|
|
292
|
+
| 'llm-call'
|
|
293
|
+
| 'stream-text'
|
|
294
|
+
| 'tool-call'
|
|
295
|
+
| 'send-message'
|
|
296
|
+
| 'record-event'
|
|
297
|
+
| 'get-scope'
|
|
298
|
+
| 'list-messages'
|
|
299
|
+
| 'list-artifacts'
|
|
300
|
+
| 'list-task-history';
|
|
301
|
+
|
|
302
|
+
export interface ScopeRef { kind: 'chat' | 'agreement' | 'task' | 'counterparty'; id: string }
|
|
303
|
+
|
|
304
|
+
export type Effect =
|
|
305
|
+
| { kind: 'read-context'; sessionId: string; opts?: ReadContextOpts }
|
|
306
|
+
| {
|
|
307
|
+
kind: 'llm-call';
|
|
308
|
+
messages: LlmMessage[];
|
|
309
|
+
tools: LlmToolSchema[];
|
|
310
|
+
/** Chat/session id for monitoring ingest (ZIG-360). */
|
|
311
|
+
sessionId?: string;
|
|
312
|
+
/** Per-turn UUID grouping all LLM + tool calls in one turn (ZIG-440). */
|
|
313
|
+
runId?: string;
|
|
314
|
+
/** FSM state name at the time of this call (ZIG-440). */
|
|
315
|
+
stateId?: string;
|
|
316
|
+
}
|
|
317
|
+
| {
|
|
318
|
+
kind: 'stream-text';
|
|
319
|
+
sessionId: string;
|
|
320
|
+
receiverId: string;
|
|
321
|
+
/** Pre-allocated messageId shared across all chunks and the final send-message. */
|
|
322
|
+
messageId: string;
|
|
323
|
+
messages: LlmMessage[];
|
|
324
|
+
}
|
|
325
|
+
| {
|
|
326
|
+
kind: 'tool-call';
|
|
327
|
+
sessionId: string;
|
|
328
|
+
name: string;
|
|
329
|
+
args: unknown;
|
|
330
|
+
memory?: MemoryStore;
|
|
331
|
+
senderId?: string;
|
|
332
|
+
senderType?: string;
|
|
333
|
+
/** Per-turn UUID (ZIG-440). */
|
|
334
|
+
runId?: string;
|
|
335
|
+
/** FSM state name (ZIG-440). */
|
|
336
|
+
stateId?: string;
|
|
337
|
+
}
|
|
338
|
+
| { kind: 'send-message'; sessionId: string; receiverId: string; text: string; messageId?: string }
|
|
339
|
+
| { kind: 'record-event'; sessionId: string; entry: RecordedEntry }
|
|
340
|
+
| { kind: 'get-scope'; via: ScopeRef }
|
|
341
|
+
| { kind: 'list-messages'; chatId: string; after?: string; limit?: number }
|
|
342
|
+
| { kind: 'list-artifacts'; chatId?: string; agreementId?: string; after?: string; limit?: number }
|
|
343
|
+
| { kind: 'list-task-history'; taskId: string; after?: string; limit?: number };
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Typed result map. With this shape, `await eff({ kind: 'read-context', ... })`
|
|
347
|
+
* resolves to `ContextSnapshot`, not a union — the discriminant carries the
|
|
348
|
+
* result type. Tests and Server impls implement this map exhaustively.
|
|
349
|
+
*/
|
|
350
|
+
export interface EffectResultMap {
|
|
351
|
+
'read-context': ContextSnapshot;
|
|
352
|
+
'llm-call': LlmResponse;
|
|
353
|
+
'stream-text': { text: string };
|
|
354
|
+
'tool-call': ToolCallResult;
|
|
355
|
+
'send-message': { ok: true };
|
|
356
|
+
'record-event': { ok: true };
|
|
357
|
+
'get-scope': unknown;
|
|
358
|
+
'list-messages': { messages: unknown[]; latestSequence: string | null };
|
|
359
|
+
'list-artifacts': { artifacts: unknown[]; latestSequence: string | null };
|
|
360
|
+
'list-task-history': { taskId: string; history: unknown[]; latestSequence: string | null };
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export type EffectResult<K extends EffectKind> = EffectResultMap[K];
|
|
364
|
+
|
|
365
|
+
/** The single side-channel between Agent and Server. */
|
|
366
|
+
export type EffectHandler = <K extends EffectKind>(
|
|
367
|
+
effect: Extract<Effect, { kind: K }>,
|
|
368
|
+
) => Promise<EffectResult<K>>;
|
|
369
|
+
|
|
370
|
+
// ── defineAgent input/output ─────────────────────────────────────────────────
|
|
371
|
+
|
|
372
|
+
export interface ServicesConfig {
|
|
373
|
+
llm?: { model: string; temperature?: number };
|
|
374
|
+
tools?: unknown[];
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export interface DefineAgentInput {
|
|
378
|
+
agentId: string;
|
|
379
|
+
description: string;
|
|
380
|
+
specialization?: string;
|
|
381
|
+
initial: string;
|
|
382
|
+
states: Record<string, State>;
|
|
383
|
+
tools?: unknown[];
|
|
384
|
+
services?: ServicesConfig;
|
|
385
|
+
operatorKey?: string;
|
|
386
|
+
taskTools?: 'all' | 'none' | string[];
|
|
387
|
+
[key: string]: unknown;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Frozen output of defineAgent. Consumed directly by AgentHost / createAgent.
|
|
392
|
+
* No mutation — defineAgent validates and returns; injection of defaults is
|
|
393
|
+
* the workflow author's responsibility (visible spread of `thinkingDefaults`).
|
|
394
|
+
*/
|
|
395
|
+
export interface AgentConfig {
|
|
396
|
+
openaiKey: string | undefined;
|
|
397
|
+
operatorKey?: string;
|
|
398
|
+
agentId: string;
|
|
399
|
+
description: string;
|
|
400
|
+
specialization?: string;
|
|
401
|
+
tools: unknown[];
|
|
402
|
+
model: string;
|
|
403
|
+
workflow: Workflow;
|
|
404
|
+
services?: ServicesConfig;
|
|
405
|
+
taskTools?: 'all' | 'none' | string[];
|
|
406
|
+
[key: string]: unknown;
|
|
407
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { runtimeLog } from '../shared/runtimeLog.js';
|
|
2
|
+
|
|
3
|
+
export function extractJSON(content: string): string {
|
|
4
|
+
if (!content || typeof content !== 'string') return '';
|
|
5
|
+
|
|
6
|
+
const trimmed = content.trim();
|
|
7
|
+
if (!trimmed.startsWith('{') && !trimmed.startsWith('[')) {
|
|
8
|
+
const jsonBlockMatch = content.match(/```(?:json)?\s*([\s\S]*?)\s*```/);
|
|
9
|
+
if (jsonBlockMatch) {
|
|
10
|
+
content = jsonBlockMatch[1].trim();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const firstBrace = content.indexOf('{');
|
|
15
|
+
const firstBracket = content.indexOf('[');
|
|
16
|
+
|
|
17
|
+
let startIdx = -1;
|
|
18
|
+
let isArray = false;
|
|
19
|
+
|
|
20
|
+
if (firstBrace !== -1 && (firstBracket === -1 || firstBrace < firstBracket)) {
|
|
21
|
+
startIdx = firstBrace;
|
|
22
|
+
isArray = false;
|
|
23
|
+
} else if (firstBracket !== -1) {
|
|
24
|
+
startIdx = firstBracket;
|
|
25
|
+
isArray = true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (startIdx === -1) return content.trim();
|
|
29
|
+
|
|
30
|
+
const openChar = isArray ? '[' : '{';
|
|
31
|
+
const closeChar = isArray ? ']' : '}';
|
|
32
|
+
let depth = 0;
|
|
33
|
+
let endIdx = startIdx;
|
|
34
|
+
|
|
35
|
+
for (let i = startIdx; i < content.length; i++) {
|
|
36
|
+
const char = content[i];
|
|
37
|
+
if (char === openChar) {
|
|
38
|
+
depth++;
|
|
39
|
+
} else if (char === closeChar) {
|
|
40
|
+
depth--;
|
|
41
|
+
if (depth === 0) { endIdx = i + 1; break; }
|
|
42
|
+
} else if (char === '"') {
|
|
43
|
+
i++;
|
|
44
|
+
while (i < content.length && content[i] !== '"') {
|
|
45
|
+
if (content[i] === '\\') i++;
|
|
46
|
+
i++;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (endIdx > startIdx) content = content.substring(startIdx, endIdx);
|
|
52
|
+
|
|
53
|
+
content = content.trim();
|
|
54
|
+
content = content.replace(/(?<!:)\/\/.*$/gm, '');
|
|
55
|
+
content = content.replace(/\/\*[\s\S]*?\*\//g, '');
|
|
56
|
+
content = content.replace(/,(\s*[}\]])/g, '$1');
|
|
57
|
+
|
|
58
|
+
return content;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function repairJsonStrings(jsonText: string): string {
|
|
62
|
+
if (!jsonText || typeof jsonText !== 'string') return jsonText;
|
|
63
|
+
const validEscapes = new Set(['"', '\\', '/', 'b', 'f', 'n', 'r', 't', 'u']);
|
|
64
|
+
let out = '';
|
|
65
|
+
let inString = false;
|
|
66
|
+
|
|
67
|
+
for (let i = 0; i < jsonText.length; i++) {
|
|
68
|
+
const ch = jsonText[i];
|
|
69
|
+
if (!inString) {
|
|
70
|
+
if (ch === '"') inString = true;
|
|
71
|
+
out += ch;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (ch === '\\') {
|
|
75
|
+
const next = jsonText[i + 1];
|
|
76
|
+
out += (next && validEscapes.has(next)) ? ch : '\\\\';
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (ch === '"') { inString = false; out += ch; continue; }
|
|
80
|
+
if (ch === '\n') { out += '\\n'; continue; }
|
|
81
|
+
if (ch === '\r') { out += '\\r'; continue; }
|
|
82
|
+
out += ch;
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function safeParseJSON<T = unknown>(content: string, fallback: T | null = null): T | null {
|
|
88
|
+
try {
|
|
89
|
+
const extracted = extractJSON(content);
|
|
90
|
+
if (!extracted || extracted.trim().length === 0) throw new Error('Empty JSON content');
|
|
91
|
+
try {
|
|
92
|
+
return JSON.parse(extracted) as T;
|
|
93
|
+
} catch {
|
|
94
|
+
return JSON.parse(repairJsonStrings(extracted)) as T;
|
|
95
|
+
}
|
|
96
|
+
} catch {
|
|
97
|
+
runtimeLog.error('JSONExtractor', 'Error parsing JSON', content.length > 500 ? `${content.slice(0, 500)}…` : content);
|
|
98
|
+
return fallback ?? null;
|
|
99
|
+
}
|
|
100
|
+
}
|
package/src/ConnectionPool.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ConnectionPool — ZiggsAgent-aware wrapper around api-client's ConnectionManager.
|
|
3
|
-
*
|
|
4
|
-
* The actual pool mechanics (LRU, idle timeout, control socket) live in
|
|
5
|
-
* api-client and know nothing about agents. This class just registers each
|
|
6
|
-
* `defineAgent` config with the manager, supplying the open/close functions
|
|
7
|
-
* that instantiate and tear down a `ZiggsAgent`.
|
|
8
|
-
*
|
|
9
|
-
* Usage:
|
|
10
|
-
* const pool = new ConnectionPool({ maxActive: 50, idleTimeoutMs: 60_000 });
|
|
11
|
-
* pool.register(agentConfigs, agentMetadata);
|
|
12
|
-
* pool.startControl({ wsUrl, operatorKey }); // optional, enables wake-on-demand
|
|
13
|
-
* const agent = await pool.wake('agent-42');
|
|
14
|
-
* await pool.sendTo('agent-42', text, metadata);
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { ConnectionManager } from '@ziggs-ai/api-client';
|
|
18
|
-
import { ZiggsAgent } from './ziggs/runtime.js';
|
|
19
|
-
|
|
20
|
-
export class ConnectionPool {
|
|
21
|
-
/**
|
|
22
|
-
* @param {object} opts
|
|
23
|
-
* @param {number} [opts.maxActive=50]
|
|
24
|
-
* @param {number} [opts.idleTimeoutMs=60000]
|
|
25
|
-
*/
|
|
26
|
-
constructor({ maxActive = 50, idleTimeoutMs = 60_000 } = {}) {
|
|
27
|
-
this._mgr = new ConnectionManager({ maxActive, idleTimeoutMs });
|
|
28
|
-
// agentId → defineAgent config — kept for ZiggsAgent construction at wake time
|
|
29
|
-
this._configs = new Map();
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// ---- registration ----
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Register agents without connecting them. Pool keys use `config.agentId`,
|
|
36
|
-
* falling back to `config.workflow?.id` / `config.id`.
|
|
37
|
-
*
|
|
38
|
-
* @param {Array<object>} configs defineAgent-compatible configs
|
|
39
|
-
* @param {Array<object>} [metaArr] Parallel metadata objects keyed by the resolved agentId
|
|
40
|
-
*/
|
|
41
|
-
register(configs, metaArr = []) {
|
|
42
|
-
const metaByAgentId = Object.fromEntries(metaArr.map(m => [m.agentId, m]));
|
|
43
|
-
for (const config of configs) {
|
|
44
|
-
const agentId = config.agentId
|
|
45
|
-
?? config.workflow?.id
|
|
46
|
-
?? config.id;
|
|
47
|
-
if (!agentId) {
|
|
48
|
-
console.warn('[ConnectionPool] Skipping config with no resolvable agentId (need config.agentId)');
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
this._configs.set(agentId, config);
|
|
52
|
-
this._mgr.register(
|
|
53
|
-
agentId,
|
|
54
|
-
async () => {
|
|
55
|
-
const agent = new ZiggsAgent(this._configs.get(agentId));
|
|
56
|
-
await agent.connectAsync();
|
|
57
|
-
console.log(`[ConnectionPool] Woke "${agentId}" (active: ${this._mgr.listActive().length + 1}/${this._mgr.maxActive})`);
|
|
58
|
-
return agent;
|
|
59
|
-
},
|
|
60
|
-
(agent) => {
|
|
61
|
-
agent.disconnect();
|
|
62
|
-
console.log(`[ConnectionPool] Disconnected "${agentId}"`);
|
|
63
|
-
},
|
|
64
|
-
metaByAgentId[agentId],
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// ---- connection lifecycle ----
|
|
70
|
-
|
|
71
|
-
async wake(agentId) { return this._mgr.wake(agentId); }
|
|
72
|
-
async sleep(agentId) { return this._mgr.sleep(agentId); }
|
|
73
|
-
async disconnectAll() { return this._mgr.sleepAll(); }
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Wake an agent, deliver a message, and return the agent's response text.
|
|
77
|
-
* Awaits the full LLM turn before returning. Resets the idle timer after delivery.
|
|
78
|
-
*/
|
|
79
|
-
async sendTo(agentId, text, metadata = {}, { timeoutMs = 30_000 } = {}) {
|
|
80
|
-
const agent = await this.wake(agentId);
|
|
81
|
-
const chatId = metadata.chatId;
|
|
82
|
-
|
|
83
|
-
const msgBuf = agent.agent?.messageBuffer?._pending;
|
|
84
|
-
const bufBefore = msgBuf?.get(chatId)?.length ?? 0;
|
|
85
|
-
|
|
86
|
-
const timeoutPromise = new Promise((_, reject) =>
|
|
87
|
-
setTimeout(() => reject(new Error(`sendTo timeout for "${agentId}" after ${timeoutMs}ms`)), timeoutMs)
|
|
88
|
-
);
|
|
89
|
-
await Promise.race([agent.handleMessage(text, metadata), timeoutPromise]);
|
|
90
|
-
this._mgr.touch(agentId);
|
|
91
|
-
|
|
92
|
-
const pending = msgBuf?.get(chatId);
|
|
93
|
-
if (pending && pending.length > bufBefore) {
|
|
94
|
-
const newEntries = pending.splice(bufBefore);
|
|
95
|
-
if (pending.length === 0) msgBuf.delete(chatId);
|
|
96
|
-
return newEntries.map(e => e.text).join('\n\n');
|
|
97
|
-
}
|
|
98
|
-
return null;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// ---- control socket ----
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Open the launcher's control socket to the backend. Agents in this pool
|
|
105
|
-
* will show as "available" in the store and the backend will push
|
|
106
|
-
* `launcher:wake` events for them as needed.
|
|
107
|
-
*/
|
|
108
|
-
startControl({ wsUrl, operatorKey } = {}) {
|
|
109
|
-
if (!wsUrl || !operatorKey) {
|
|
110
|
-
console.warn('[ConnectionPool] startControl: wsUrl and operatorKey are required — skipping');
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
this._mgr._controlOpts = { wsUrl, operatorKey };
|
|
114
|
-
this._mgr.start();
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/** Close the control socket. Agents flip to "offline" on the backend immediately. */
|
|
118
|
-
stopControl() {
|
|
119
|
-
this._mgr._controlHandle?.close();
|
|
120
|
-
this._mgr._controlHandle = null;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// ---- querying ----
|
|
124
|
-
|
|
125
|
-
query(filter) { return this._mgr.query(filter); }
|
|
126
|
-
list() { return this._mgr.list(); }
|
|
127
|
-
listActive() { return this._mgr.listActive(); }
|
|
128
|
-
get size() { return this._mgr.size; }
|
|
129
|
-
get maxActive() { return this._mgr.maxActive; }
|
|
130
|
-
|
|
131
|
-
// Legacy compatibility: p1000's list_agents tool reads _meta directly.
|
|
132
|
-
get _meta() { return { get: (id) => this._mgr.getMeta(id) }; }
|
|
133
|
-
}
|