@noetic-tools/core 5.22.0-next.4 → 5.22.0-next.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/dist/harness/agent-harness.js +1 -0
- package/dist/harness/harness-options.d.ts +7 -0
- package/dist/index.d.ts +1 -1
- package/dist/interpreter/acp-events.d.ts +7 -0
- package/dist/interpreter/acp-events.js +23 -1
- package/dist/interpreter/acp-resume.d.ts +1 -0
- package/dist/interpreter/acp-resume.js +2 -0
- package/dist/interpreter/execute-acp-agent.js +106 -22
- package/dist/runtime/acp-session-store.js +0 -0
- package/dist/util/error-message.d.ts +24 -0
- package/dist/util/error-message.js +94 -0
- package/package.json +2 -2
|
@@ -349,6 +349,7 @@ export class AgentHarness {
|
|
|
349
349
|
onHeld: opts.onHeld,
|
|
350
350
|
steerWhileHeld: opts.steerWhileHeld,
|
|
351
351
|
now: opts.now,
|
|
352
|
+
killAgentsOnExit: opts.killAgentsOnExit,
|
|
352
353
|
};
|
|
353
354
|
this.outputStorage = this.config.storage ?? createInMemoryStorage();
|
|
354
355
|
const adapters = resolveEnvironmentAdapters(environment);
|
|
@@ -205,6 +205,13 @@ export interface AgentHarnessOpts<TParams extends Record<string, unknown> = Reco
|
|
|
205
205
|
* the progress it missed.
|
|
206
206
|
*/
|
|
207
207
|
emitItemsOnReplay?: boolean;
|
|
208
|
+
/**
|
|
209
|
+
* Stop every ACP agent this harness started when its process goes away
|
|
210
|
+
* without closing them — a crash, Ctrl-C, a killed process. By default
|
|
211
|
+
* (`false`) a local agent keeps working and holds what it sends until a
|
|
212
|
+
* resumed run reattaches to it (spec 27 § A local agent outlives its host).
|
|
213
|
+
*/
|
|
214
|
+
killAgentsOnExit?: boolean;
|
|
208
215
|
/**
|
|
209
216
|
* The version of the flow this harness runs: any string the host changes when the
|
|
210
217
|
* flow changes, such as a release tag or a hash (spec 23 § Versions). Every step a
|
package/dist/index.d.ts
CHANGED
|
@@ -199,7 +199,7 @@ export { BooleanQuestionSchema, ChoiceQuestionSchema, choiceValues, DECIDE_LIMIT
|
|
|
199
199
|
/** @public */
|
|
200
200
|
export type { RouteSwitchByFunctionOpts, RouteSwitchByKeyOpts, StepDecideOpts, StepInput, StepOutput, } from './builders/decide-builders.js';
|
|
201
201
|
/** @public */
|
|
202
|
-
export type { AcpAgent, AcpAgentCapabilities, AcpAgentConnection, AcpAgentGrant, AcpAllowlist, AcpAuthMethod, AcpAvailableCommand, AcpBoundPermissionHandler, AcpCapabilityGrant, AcpClientCapabilityConfig, AcpClientHost, AcpConnectOptions, AcpContentBlock, AcpKeepAlive, AcpLiveSession, AcpLoadSessionOptions, AcpMachine, AcpMachineFactory, AcpMcpServer, AcpNewSessionOptions, AcpPermissionGrant, AcpPermissionHandler, AcpPermissionOption, AcpPermissionOutcome, AcpPermissionPolicy, AcpPermissionRequestInfo, AcpPermissionRule, AcpPermissionSteerer, AcpPlanEntry, AcpPromptCapabilities, AcpPromptOptions, AcpRequestPermissionRequest, AcpSession, AcpSessionDisposer, AcpSessionGrant, AcpSessionInfo, AcpSessionMode, AcpSessionModeState, AcpSessionNotification, AcpSessionPolicy, AcpStopReason, AcpToolCallContent, AcpToolCallStatus, AcpToolKind, AcpToolRefusal, AcpTransport, AcpTransportFactory, AcpTransportOptions, AcpTurnResult, ByteStream, Spawn, SpawnCommand, ToolBridgeAddress, } from '@noetic-tools/types';
|
|
202
|
+
export type { AcpAgent, AcpAgentCapabilities, AcpAgentConnection, AcpAgentGrant, AcpAllowlist, AcpAuthMethod, AcpAvailableCommand, AcpBoundPermissionHandler, AcpCapabilityGrant, AcpClientCapabilityConfig, AcpClientHost, AcpConnectOptions, AcpContentBlock, AcpKeepAlive, AcpLiveSession, AcpLoadSessionOptions, AcpMachine, AcpMachineFactory, AcpMcpServer, AcpNewSessionOptions, AcpPermissionGrant, AcpPermissionHandler, AcpPermissionOption, AcpPermissionOutcome, AcpPermissionPolicy, AcpPermissionRequestInfo, AcpPermissionRule, AcpPermissionSteerer, AcpPlanEntry, AcpPromptCapabilities, AcpPromptOptions, AcpReattachHandle, AcpRequestPermissionRequest, AcpResumeTurnOptions, AcpSession, AcpSessionConfigOption, AcpSessionDisposer, AcpSessionGrant, AcpSessionInfo, AcpSessionMode, AcpSessionModeState, AcpSessionNotification, AcpSessionPolicy, AcpStopReason, AcpToolCallContent, AcpToolCallStatus, AcpToolKind, AcpToolRefusal, AcpTransport, AcpTransportFactory, AcpTransportOptions, AcpTurnResult, AcpUsage, AcpUsageUpdate, AcpWhenHostExits, ByteStream, Spawn, SpawnCommand, ToolBridgeAddress, } from '@noetic-tools/types';
|
|
203
203
|
/** @public */
|
|
204
204
|
export { ACP_AGENT_STEP_KIND, AcpCapabilityError, AcpConnectError, AcpPermissionDecision, isAcpCapabilityError, isAcpConnectError, } from '@noetic-tools/types';
|
|
205
205
|
/** @public */
|
|
@@ -65,6 +65,13 @@ export declare class AcpEventBridge {
|
|
|
65
65
|
itemAppended(item: Item): void;
|
|
66
66
|
/** Forward one notification: a structured framework event + the mapped sdk events. */
|
|
67
67
|
forward(notification: AcpSessionNotification): void;
|
|
68
|
+
/**
|
|
69
|
+
* Forward one update `session/load` replayed: the loaded session's history,
|
|
70
|
+
* not this turn's output. It reaches raw-event consumers as an `acp_event`
|
|
71
|
+
* marked `replay: true` (same step, agent and session attribution) and is
|
|
72
|
+
* never translated into text, reasoning or function-call events.
|
|
73
|
+
*/
|
|
74
|
+
forwardReplay(notification: AcpSessionNotification): void;
|
|
68
75
|
/**
|
|
69
76
|
* Ensure the turn's output reached the event surface. For an agent that
|
|
70
77
|
* returns a result without streaming, synthesize events from the result.
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
* synthesized into events.
|
|
15
15
|
*/
|
|
16
16
|
import { emitFrameworkEvent, getBroadcaster, shouldEmit } from '../runtime/broadcaster-utils.js';
|
|
17
|
+
import { errorMessage } from '../util/error-message.js';
|
|
17
18
|
import { isFunctionCall } from './typeguards.js';
|
|
18
19
|
/** Flatten an ACP content block for the text event surface. */
|
|
19
20
|
function blockText(block) {
|
|
@@ -128,6 +129,22 @@ export class AcpEventBridge {
|
|
|
128
129
|
});
|
|
129
130
|
this.translate(notification.update);
|
|
130
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Forward one update `session/load` replayed: the loaded session's history,
|
|
134
|
+
* not this turn's output. It reaches raw-event consumers as an `acp_event`
|
|
135
|
+
* marked `replay: true` (same step, agent and session attribution) and is
|
|
136
|
+
* never translated into text, reasoning or function-call events.
|
|
137
|
+
*/
|
|
138
|
+
forwardReplay(notification) {
|
|
139
|
+
if (!this.broadcaster) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
this.framework('acp_event', {
|
|
143
|
+
sessionId: notification.sessionId,
|
|
144
|
+
update: notification.update,
|
|
145
|
+
replay: true,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
131
148
|
/**
|
|
132
149
|
* Ensure the turn's output reached the event surface. For an agent that
|
|
133
150
|
* returns a result without streaming, synthesize events from the result.
|
|
@@ -156,6 +173,11 @@ export class AcpEventBridge {
|
|
|
156
173
|
this.start();
|
|
157
174
|
this.sdk('response.completed', {
|
|
158
175
|
stopReason: result.stopReason,
|
|
176
|
+
...(result.reportedUsage
|
|
177
|
+
? {
|
|
178
|
+
reportedUsage: result.reportedUsage,
|
|
179
|
+
}
|
|
180
|
+
: {}),
|
|
159
181
|
});
|
|
160
182
|
this.completed = true;
|
|
161
183
|
}
|
|
@@ -173,7 +195,7 @@ export class AcpEventBridge {
|
|
|
173
195
|
this.start();
|
|
174
196
|
this.sdk('response.completed', {
|
|
175
197
|
stopReason: 'error',
|
|
176
|
-
error:
|
|
198
|
+
error: errorMessage(cause),
|
|
177
199
|
});
|
|
178
200
|
this.completed = true;
|
|
179
201
|
}
|
|
@@ -15,6 +15,7 @@ import type { ToolCallResult } from '../runtime/tool-call.js';
|
|
|
15
15
|
declare const AcpSessionRecordSchema: z.ZodObject<{
|
|
16
16
|
sessionId: z.ZodString;
|
|
17
17
|
agentId: z.ZodString;
|
|
18
|
+
reattach: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
18
19
|
}, z.core.$strip>;
|
|
19
20
|
/** The agent session a step opened, as stored. */
|
|
20
21
|
export type AcpSessionRecord = z.infer<typeof AcpSessionRecordSchema>;
|
|
@@ -19,6 +19,8 @@ import { pendingEntryKey, runRootOf } from './connection-steps.js';
|
|
|
19
19
|
const AcpSessionRecordSchema = z.object({
|
|
20
20
|
sessionId: z.string(),
|
|
21
21
|
agentId: z.string(),
|
|
22
|
+
/** How to reach the agent if it outlived the host that opened the session. */
|
|
23
|
+
reattach: z.record(z.string(), z.unknown()).optional(),
|
|
22
24
|
});
|
|
23
25
|
const BridgedOutputSchema = z.object({
|
|
24
26
|
output: z.string(),
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
import { createMessage, extractAssistantText, frameworkCast, NoeticConfigError, NoeticErrorImpl, SteeringAction, } from '@noetic-tools/types';
|
|
10
10
|
import { assertAgentTakesTools } from '../builders/acp-agent-checks.js';
|
|
11
11
|
import { runtimeOf } from '../connections/model-tools.js';
|
|
12
|
+
import { isRpcError, RpcFailure } from '../util/error-message.js';
|
|
12
13
|
import { AcpEventBridge } from './acp-events.js';
|
|
13
14
|
import { withHistoryPrompt } from './acp-history.js';
|
|
14
15
|
import { renderLayerContext } from './acp-layers.js';
|
|
@@ -364,6 +365,7 @@ async function sessionOn(connection, opts) {
|
|
|
364
365
|
}
|
|
365
366
|
async function openSession(opts) {
|
|
366
367
|
let resumed = false;
|
|
368
|
+
let reattached = false;
|
|
367
369
|
const store = sessionStore(opts.baseCtx);
|
|
368
370
|
const keepAlive = keepAliveOf(opts.step.session);
|
|
369
371
|
const reuseKey = opts.step.session?.reuse;
|
|
@@ -376,10 +378,29 @@ async function openSession(opts) {
|
|
|
376
378
|
// its step has to be reachable, or nothing can ever close it.
|
|
377
379
|
const key = store.keyFor(parts);
|
|
378
380
|
const { live, fresh } = await store.acquire(key, parts, async () => {
|
|
379
|
-
const
|
|
381
|
+
const connectOptions = {
|
|
380
382
|
host: opts.host,
|
|
381
383
|
signal: abortSignalOf(opts.baseCtx),
|
|
382
|
-
|
|
384
|
+
whenHostExits: whenHostExitsOf(opts.baseCtx),
|
|
385
|
+
};
|
|
386
|
+
const kept = await reattachTo(opts.agent, connectOptions, {
|
|
387
|
+
reattach: opts.reattach,
|
|
388
|
+
sessionId: opts.resumeId,
|
|
389
|
+
});
|
|
390
|
+
if (kept) {
|
|
391
|
+
resumed = true;
|
|
392
|
+
reattached = true;
|
|
393
|
+
return {
|
|
394
|
+
connection: holdingConnections(kept.connection, opts.baseCtx, keepAlive),
|
|
395
|
+
session: kept.session,
|
|
396
|
+
host: opts.host,
|
|
397
|
+
agentId: opts.agent.agentId,
|
|
398
|
+
keepAlive,
|
|
399
|
+
cwd: opts.cwd,
|
|
400
|
+
tools: opts.tools?.tools,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
const connection = holdingConnections(await opts.agent.connect(connectOptions), opts.baseCtx, keepAlive);
|
|
383
404
|
// From here on the connection owns a live agent (usually a child process).
|
|
384
405
|
// Anything that throws before the caller can take responsibility for it
|
|
385
406
|
// must close it first — otherwise the agent outlives the step and, because
|
|
@@ -426,6 +447,39 @@ async function openSession(opts) {
|
|
|
426
447
|
keepAlive,
|
|
427
448
|
fresh,
|
|
428
449
|
resumed,
|
|
450
|
+
reattached,
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
/** The harness's choice for agents its process leaves behind. */
|
|
454
|
+
function whenHostExitsOf(ctx) {
|
|
455
|
+
return ctx.harness.config.killAgentsOnExit === true ? 'stop' : 'keep';
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* The live session an earlier attempt of the step left in an agent that
|
|
459
|
+
* outlived its host, or undefined when there is none to reach — the agent is
|
|
460
|
+
* gone, or no longer has the session — so the step reloads or starts over.
|
|
461
|
+
*/
|
|
462
|
+
async function reattachTo(agent, connectOptions, { reattach, sessionId, }) {
|
|
463
|
+
if (!reattach || sessionId === undefined) {
|
|
464
|
+
return undefined;
|
|
465
|
+
}
|
|
466
|
+
const connection = await agent
|
|
467
|
+
.connect({
|
|
468
|
+
...connectOptions,
|
|
469
|
+
reattach,
|
|
470
|
+
})
|
|
471
|
+
.catch(() => undefined);
|
|
472
|
+
if (!connection) {
|
|
473
|
+
return undefined;
|
|
474
|
+
}
|
|
475
|
+
const session = await connection.attachSession?.(sessionId).catch(() => undefined);
|
|
476
|
+
if (!session?.resumeTurn) {
|
|
477
|
+
await connection.close().catch(() => undefined);
|
|
478
|
+
return undefined;
|
|
479
|
+
}
|
|
480
|
+
return {
|
|
481
|
+
connection,
|
|
482
|
+
session,
|
|
429
483
|
};
|
|
430
484
|
}
|
|
431
485
|
/**
|
|
@@ -512,19 +566,24 @@ function turnFailure(step, cause, baseCtx) {
|
|
|
512
566
|
return new NoeticErrorImpl({
|
|
513
567
|
kind: 'step_failed',
|
|
514
568
|
stepId: step.id,
|
|
515
|
-
|
|
569
|
+
// A JSON-RPC rejection keeps its detail (`data`) in the message, so a
|
|
570
|
+
// failure reads "Internal error: Authentication required (-32603)", never
|
|
571
|
+
// "[object Object]" or a bare "Internal error".
|
|
572
|
+
cause: cause instanceof Error && !isRpcError(cause) ? cause : new RpcFailure(cause),
|
|
516
573
|
retriesExhausted: false,
|
|
517
574
|
});
|
|
518
575
|
}
|
|
519
|
-
/**
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
576
|
+
/**
|
|
577
|
+
* A failure while opening the session or applying its mode/model. Typed
|
|
578
|
+
* errors (capability, connect, config) pass through; a JSON-RPC rejection from
|
|
579
|
+
* the agent — or anything thrown that is not an Error — becomes the step's
|
|
580
|
+
* `step_failed` error with a readable message.
|
|
581
|
+
*/
|
|
582
|
+
function setupFailure(step, cause, baseCtx) {
|
|
583
|
+
if (cause instanceof Error && !isRpcError(cause)) {
|
|
584
|
+
return cause;
|
|
523
585
|
}
|
|
524
|
-
|
|
525
|
-
const data = Reflect.get(cause, 'data');
|
|
526
|
-
const head = typeof message === 'string' ? message : JSON.stringify(cause);
|
|
527
|
-
return data === undefined ? head : `${head}: ${JSON.stringify(data)}`;
|
|
586
|
+
return turnFailure(step, cause, baseCtx);
|
|
528
587
|
}
|
|
529
588
|
/**
|
|
530
589
|
* The tools the agent is served. No `tools` offers every tool the run has,
|
|
@@ -558,12 +617,21 @@ async function runTurn(params) {
|
|
|
558
617
|
bridge.begin();
|
|
559
618
|
let result;
|
|
560
619
|
try {
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
620
|
+
// Per-turn signal, so a session reused across turns is cancelled by the
|
|
621
|
+
// context running the CURRENT turn rather than the one that opened it.
|
|
622
|
+
const signal = abortSignalOf(baseCtx);
|
|
623
|
+
const session = resolution.live.session;
|
|
624
|
+
const collected = params.collect && session.resumeTurn
|
|
625
|
+
? await session.resumeTurn({
|
|
626
|
+
signal,
|
|
627
|
+
})
|
|
628
|
+
: undefined;
|
|
629
|
+
result =
|
|
630
|
+
collected ??
|
|
631
|
+
(await session.prompt({
|
|
632
|
+
content: params.content,
|
|
633
|
+
signal,
|
|
634
|
+
}));
|
|
567
635
|
}
|
|
568
636
|
catch (e) {
|
|
569
637
|
await settle(tools);
|
|
@@ -647,9 +715,12 @@ export async function executeAcpAgent(step, input, ctx, layers) {
|
|
|
647
715
|
// An earlier attempt of this step may have left its session behind.
|
|
648
716
|
const sessions = step.session?.load ? undefined : acpSessionRecords(baseCtx);
|
|
649
717
|
const recorded = await sessions?.load();
|
|
650
|
-
const
|
|
718
|
+
const sameAgent = recorded?.agentId === agent.agentId;
|
|
719
|
+
const resumeId = sameAgent ? recorded?.sessionId : undefined;
|
|
720
|
+
const reattach = sameAgent ? recorded?.reattach : undefined;
|
|
651
721
|
// `session/load` replays the session's conversation as updates. They are the
|
|
652
|
-
// agent's history, not this turn's work
|
|
722
|
+
// agent's history, not this turn's work: they reach raw-event consumers
|
|
723
|
+
// marked `replay: true`, and never the text, reasoning or item streams.
|
|
653
724
|
let reloading = false;
|
|
654
725
|
let bridge = new AcpEventBridge(step, agent.agentId, baseCtx);
|
|
655
726
|
// A tool pipeline error fails the step, as it does in `callModel`: it is
|
|
@@ -663,6 +734,7 @@ export async function executeAcpAgent(step, input, ctx, layers) {
|
|
|
663
734
|
layers,
|
|
664
735
|
onUpdate: (notification) => {
|
|
665
736
|
if (reloading) {
|
|
737
|
+
events.forwardReplay(notification);
|
|
666
738
|
return;
|
|
667
739
|
}
|
|
668
740
|
tools?.observe(notification.update);
|
|
@@ -685,7 +757,9 @@ export async function executeAcpAgent(step, input, ctx, layers) {
|
|
|
685
757
|
...bindingFor(bridge),
|
|
686
758
|
cwd,
|
|
687
759
|
});
|
|
688
|
-
|
|
760
|
+
// Both an explicit `session.load` and the resume of an earlier attempt's
|
|
761
|
+
// session load it; a reused live session is not reloaded and replays nothing.
|
|
762
|
+
reloading = resumeId !== undefined || step.session?.load !== undefined;
|
|
689
763
|
const resolution = await openSession({
|
|
690
764
|
step,
|
|
691
765
|
agent,
|
|
@@ -695,7 +769,12 @@ export async function executeAcpAgent(step, input, ctx, layers) {
|
|
|
695
769
|
binding: bindingFor(bridge),
|
|
696
770
|
tools,
|
|
697
771
|
resumeId,
|
|
698
|
-
|
|
772
|
+
reattach,
|
|
773
|
+
})
|
|
774
|
+
.catch((cause) => {
|
|
775
|
+
throw setupFailure(step, cause, baseCtx);
|
|
776
|
+
})
|
|
777
|
+
.finally(() => {
|
|
699
778
|
reloading = false;
|
|
700
779
|
});
|
|
701
780
|
if (resolution.resumed) {
|
|
@@ -705,9 +784,13 @@ export async function executeAcpAgent(step, input, ctx, layers) {
|
|
|
705
784
|
await sessions?.write({
|
|
706
785
|
sessionId: resolution.live.session.sessionId,
|
|
707
786
|
agentId: agent.agentId,
|
|
787
|
+
// How a resumed run reaches this agent if it outlives this host.
|
|
788
|
+
reattach: resolution.live.connection.reattach,
|
|
708
789
|
});
|
|
709
790
|
}
|
|
710
|
-
await applySessionSelection(step, ctx, resolution, baseCtx)
|
|
791
|
+
await applySessionSelection(step, ctx, resolution, baseCtx).catch((cause) => {
|
|
792
|
+
throw setupFailure(step, cause, baseCtx);
|
|
793
|
+
});
|
|
711
794
|
turnError.cancel = () => resolution.live.session.cancel();
|
|
712
795
|
// History seeds only a freshly opened session; a reused one already owns
|
|
713
796
|
// its conversation on the agent side.
|
|
@@ -739,6 +822,7 @@ export async function executeAcpAgent(step, input, ctx, layers) {
|
|
|
739
822
|
bridge,
|
|
740
823
|
tools,
|
|
741
824
|
turnError,
|
|
825
|
+
collect: retries === 0 && resolution.reattached,
|
|
742
826
|
content: buildContent({
|
|
743
827
|
text: [
|
|
744
828
|
context,
|
|
Binary file
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A readable message for anything thrown. JSON-RPC peers (an ACP agent) reject
|
|
3
|
+
* with `{ code, message, data }` — as a plain object or as an `Error` carrying
|
|
4
|
+
* `code`/`data` — and the useful part ("Authentication required") is often in
|
|
5
|
+
* `data`, so it is appended rather than lost as `[object Object]` or a bare
|
|
6
|
+
* "Internal error".
|
|
7
|
+
*/
|
|
8
|
+
export declare function errorMessage(cause: unknown): string;
|
|
9
|
+
/**
|
|
10
|
+
* The error a step fails with for a JSON-RPC rejection: the readable message,
|
|
11
|
+
* keeping the rejection's `code` and `data` (and the original as `cause`) for
|
|
12
|
+
* callers that branch on them, such as an auth-required code.
|
|
13
|
+
*/
|
|
14
|
+
export declare class RpcFailure extends Error {
|
|
15
|
+
readonly code?: number;
|
|
16
|
+
readonly data?: unknown;
|
|
17
|
+
constructor(cause: unknown);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Whether a thrown value is a JSON-RPC error: it carries a numeric `code` or a
|
|
21
|
+
* `data` payload. A `DOMException` (an AbortError, a TimeoutError) has a
|
|
22
|
+
* numeric legacy `code` too, and is not one.
|
|
23
|
+
*/
|
|
24
|
+
export declare function isRpcError(cause: unknown): boolean;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A readable message for anything thrown. JSON-RPC peers (an ACP agent) reject
|
|
3
|
+
* with `{ code, message, data }` — as a plain object or as an `Error` carrying
|
|
4
|
+
* `code`/`data` — and the useful part ("Authentication required") is often in
|
|
5
|
+
* `data`, so it is appended rather than lost as `[object Object]` or a bare
|
|
6
|
+
* "Internal error".
|
|
7
|
+
*/
|
|
8
|
+
export function errorMessage(cause) {
|
|
9
|
+
if (typeof cause !== 'object' || cause === null) {
|
|
10
|
+
return String(cause);
|
|
11
|
+
}
|
|
12
|
+
if (!isRpcError(cause)) {
|
|
13
|
+
const message = Reflect.get(cause, 'message');
|
|
14
|
+
if (typeof message === 'string') {
|
|
15
|
+
return message;
|
|
16
|
+
}
|
|
17
|
+
return safeJson(cause);
|
|
18
|
+
}
|
|
19
|
+
const rawMessage = Reflect.get(cause, 'message');
|
|
20
|
+
const message = typeof rawMessage === 'string' && rawMessage.length > 0 ? rawMessage : undefined;
|
|
21
|
+
const code = Reflect.get(cause, 'code');
|
|
22
|
+
const detail = detailOf(Reflect.get(cause, 'data'));
|
|
23
|
+
const parts = [
|
|
24
|
+
message ?? (detail === undefined ? 'JSON-RPC error' : undefined),
|
|
25
|
+
detail !== undefined && detail !== message ? detail : undefined,
|
|
26
|
+
].filter((part) => part !== undefined);
|
|
27
|
+
const text = parts.join(': ');
|
|
28
|
+
return typeof code === 'number' ? `${text} (${code})` : text;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The error a step fails with for a JSON-RPC rejection: the readable message,
|
|
32
|
+
* keeping the rejection's `code` and `data` (and the original as `cause`) for
|
|
33
|
+
* callers that branch on them, such as an auth-required code.
|
|
34
|
+
*/
|
|
35
|
+
export class RpcFailure extends Error {
|
|
36
|
+
code;
|
|
37
|
+
data;
|
|
38
|
+
constructor(cause) {
|
|
39
|
+
super(errorMessage(cause), {
|
|
40
|
+
cause,
|
|
41
|
+
});
|
|
42
|
+
this.name = 'RpcFailure';
|
|
43
|
+
const code = typeof cause === 'object' && cause !== null ? Reflect.get(cause, 'code') : undefined;
|
|
44
|
+
this.code = typeof code === 'number' ? code : undefined;
|
|
45
|
+
this.data =
|
|
46
|
+
typeof cause === 'object' && cause !== null ? Reflect.get(cause, 'data') : undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** The detail a JSON-RPC error's `data` carries: its message, its details, or the value itself. */
|
|
50
|
+
function detailOf(data) {
|
|
51
|
+
if (data === undefined || data === null) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
if (typeof data === 'string') {
|
|
55
|
+
return data;
|
|
56
|
+
}
|
|
57
|
+
if (typeof data !== 'object') {
|
|
58
|
+
return String(data);
|
|
59
|
+
}
|
|
60
|
+
for (const key of [
|
|
61
|
+
'message',
|
|
62
|
+
'details',
|
|
63
|
+
]) {
|
|
64
|
+
const value = Reflect.get(data, key);
|
|
65
|
+
if (typeof value === 'string' && value.length > 0) {
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return safeJson(data);
|
|
70
|
+
}
|
|
71
|
+
const MAX_DETAIL_CHARS = 500;
|
|
72
|
+
function safeJson(value) {
|
|
73
|
+
try {
|
|
74
|
+
const text = JSON.stringify(value) ?? String(value);
|
|
75
|
+
return text.length > MAX_DETAIL_CHARS ? `${text.slice(0, MAX_DETAIL_CHARS)}…` : text;
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return String(value);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Whether a thrown value is a JSON-RPC error: it carries a numeric `code` or a
|
|
83
|
+
* `data` payload. A `DOMException` (an AbortError, a TimeoutError) has a
|
|
84
|
+
* numeric legacy `code` too, and is not one.
|
|
85
|
+
*/
|
|
86
|
+
export function isRpcError(cause) {
|
|
87
|
+
if (typeof cause !== 'object' || cause === null) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
if (typeof DOMException !== 'undefined' && cause instanceof DOMException) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return typeof Reflect.get(cause, 'code') === 'number' || Reflect.get(cause, 'data') !== undefined;
|
|
94
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noetic-tools/core",
|
|
3
|
-
"version": "5.22.0-next.
|
|
3
|
+
"version": "5.22.0-next.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Step primitives, interpreter, runtime, and context layer contract for Noetic agents.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
71
|
"@noetic-tools/context": "^2.0.0-next.1",
|
|
72
|
-
"@noetic-tools/types": "^3.19.0-next.
|
|
72
|
+
"@noetic-tools/types": "^3.19.0-next.4",
|
|
73
73
|
"@openrouter/agent": "^0.11.0",
|
|
74
74
|
"@openrouter/sdk": "1.4.25",
|
|
75
75
|
"jsonata": "2.2.2",
|