@myclaw163/clawclaw-cli 0.9.2 → 0.10.1
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/QUICKSTART_OTHER.md +5 -5
- package/README.md +7 -8
- package/bin/clawclaw-channel.mjs +2 -2
- package/bin/clawclaw-cli.mjs +2 -2
- package/docs/configuration.md +25 -99
- package/docs/custom-modules/README.md +23 -35
- package/docs/custom-modules/api-reference.md +95 -273
- package/docs/custom-modules/behavior.md +94 -78
- package/docs/custom-modules/examples/auto-escape.ts +5 -5
- package/docs/custom-modules/examples/go-to-point.ts +11 -11
- package/docs/custom-modules/examples/nearby-reflex.ts +2 -2
- package/docs/custom-modules/examples/task-reminder.ts +4 -5
- package/docs/custom-modules/perception.md +42 -603
- package/docs/custom-modules/skill.md +1 -1
- package/docs/desktop-event-push.md +3 -3
- package/docs/runtime/README.md +16 -17
- package/docs/runtime/agent-notifications.md +31 -12
- package/docs/runtime/behavior-runtime.md +47 -46
- package/docs/runtime/components-and-boundaries.md +70 -28
- package/docs/runtime/events-and-state.md +41 -18
- package/docs/runtime/framework-package.md +73 -0
- package/docs/runtime/official-perception-modules.md +5 -5
- package/docs/runtime/owner-lifecycle-and-observability.md +6 -6
- package/docs/runtime/short-commands-and-capabilities.md +8 -9
- package/docs/user-guide/README.md +10 -7
- package/docs/user-guide/api-reference.md +81 -164
- package/docs/user-guide/behavior.md +84 -68
- package/docs/user-guide/installation.md +7 -7
- package/docs/user-guide/perception.md +42 -586
- package/docs/user-guide/skill.md +1 -1
- package/package.json +24 -9
- package/scripts/check-framework-boundaries.mjs +27 -0
- package/scripts/check-skill-command-surface.mjs +8 -13
- package/scripts/install-local-runtime.mjs +35 -0
- package/scripts/qa-install.mjs +105 -0
- package/skills/clawclaw/SKILL.md +49 -49
- package/skills/clawclaw/references/BEHAVIORS.md +42 -73
- package/skills/clawclaw/references/CHANNELS.md +2 -2
- package/skills/clawclaw/references/COMMANDS.md +60 -101
- package/skills/clawclaw/references/CUSTOM-BEHAVIOR-DEVELOPMENT.md +94 -78
- package/skills/clawclaw/references/CUSTOM-MODULE-API.md +95 -180
- package/skills/clawclaw/references/CUSTOM-MODULES.md +24 -24
- package/skills/clawclaw/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +42 -608
- package/skills/clawclaw/references/CUSTOM-SKILL-DEVELOPMENT.md +1 -1
- package/skills/clawclaw/references/HUB.md +1 -1
- package/skills/clawclaw/references/STREAM.md +8 -8
- package/skills/clawclaw/references/TACTICS.md +1 -1
- package/src/channel/claude-channel-ipc-server.ts +1 -0
- package/src/cli.ts +4 -2
- package/src/commands/behavior.ts +26 -10
- package/src/commands/capability.ts +3 -3
- package/src/commands/load.ts +3 -2
- package/src/commands/perception.ts +5 -0
- package/src/commands/strategy.ts +2 -0
- package/src/lib/capability-load-diagnostics.ts +3 -3
- package/src/lib/perception-config.ts +18 -6
- package/src/lib/runtime-cadence-config.ts +16 -0
- package/src/lib/workspace-runtime-lock.ts +3 -46
- package/src/pipeline/canonical-event-presentation.ts +23 -124
- package/src/pipeline/event-format.ts +12 -21
- package/src/pipeline/event-hints.ts +6 -6
- package/src/pipeline/game-sync-event-normalization.ts +57 -63
- package/src/runtime/clawclaw/behavior-decision.ts +7 -3
- package/src/runtime/clawclaw/behavior-input.ts +8 -4
- package/src/runtime/clawclaw/behavior-observation-memory.ts +3 -2
- package/src/runtime/clawclaw/notification-policy.ts +42 -0
- package/src/runtime/clawclaw/notification-turn.ts +19 -0
- package/src/runtime/clawclaw/observation-frame-projector.ts +3 -3
- package/src/runtime/clawclaw/owner.ts +18 -4
- package/src/runtime/clawclaw/perception-module-manifest.ts +8 -3
- package/src/runtime/clawclaw/perception-strategy-names.ts +17 -0
- package/src/runtime/clawclaw/persistence-compatibility.ts +36 -0
- package/src/runtime/clawclaw/plugins/action/action-plugin.ts +3 -3
- package/src/runtime/clawclaw/plugins/assembly.ts +7 -9
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +1 -495
- package/src/runtime/clawclaw/plugins/behavior/behavior-analysis-thread-host.ts +17 -190
- package/src/runtime/clawclaw/plugins/behavior/behavior-instance-snapshot.ts +1 -123
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +126 -1779
- package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +13 -276
- package/src/runtime/clawclaw/plugins/behavior/behavior-source.ts +2 -2
- package/src/runtime/clawclaw/plugins/behavior/behavior-worker-bridge.ts +8 -0
- package/src/runtime/clawclaw/plugins/behavior/control-driver.ts +3 -180
- package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.ts +13 -972
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.ts +8 -109
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-rules.ts +2 -2
- package/src/runtime/clawclaw/plugins/derived-event/reminder-plugin.ts +25 -17
- package/src/runtime/clawclaw/plugins/derived-event/reminder-runner.ts +1 -0
- package/src/runtime/clawclaw/plugins/derived-event/state-change-rules.ts +3 -3
- package/src/runtime/clawclaw/plugins/derived-event/task-change-tracker.ts +1 -259
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.ts +4 -4
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.ts +9 -97
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-socket.ts +8 -343
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.ts +60 -572
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.ts +6 -434
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.ts +1 -266
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +6 -161
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-presentation.ts +4 -23
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +49 -752
- package/src/runtime/clawclaw/plugins/perception/map-concurrent.ts +1 -0
- package/src/runtime/clawclaw/plugins/perception/notification-content.ts +60 -0
- package/src/runtime/clawclaw/plugins/perception/perception-module-plugin.ts +119 -297
- package/src/runtime/clawclaw/plugins/perception/perception-module-runner.ts +7 -56
- package/src/runtime/clawclaw/plugins/perception/perception-module-worker.ts +11 -33
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +18 -7
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +12 -6
- package/src/runtime/clawclaw/plugins/perception/task-baseline.ts +29 -0
- package/src/runtime/clawclaw/plugins/source/source-plugin.ts +6 -0
- package/src/runtime/clawclaw/plugins/system-perception/rule-runner.ts +107 -0
- package/src/runtime/clawclaw/plugins/system-perception/system-perceptions.ts +107 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.ts +8 -3
- package/src/runtime/clawclaw/reaction-behavior-resolution.ts +1 -23
- package/src/runtime/clawclaw/reminder-manifest.ts +5 -2
- package/src/runtime/clawclaw/runtime-cadence.ts +24 -0
- package/src/runtime/clawclaw/runtime-state.ts +11 -0
- package/src/runtime/clawclaw/runtime.ts +8 -0
- package/src/runtime/clawclaw/strategy-memory-rpc.ts +1 -63
- package/src/runtime/clawclaw/strategy-memory.ts +1 -77
- package/src/runtime/clawclaw-event-catalog.ts +12 -12
- package/src/runtime/framework/action-diagnostics.ts +3 -83
- package/src/runtime/framework/capability-registry.ts +2 -395
- package/src/runtime/framework/contracts.ts +2 -144
- package/src/runtime/framework/control/control-scheduler.ts +2 -1268
- package/src/runtime/framework/control/domain-set.ts +2 -81
- package/src/runtime/framework/control/index.ts +1 -3
- package/src/runtime/framework/control/types.ts +2 -191
- package/src/runtime/framework/control-coordinator.ts +2 -11
- package/src/runtime/framework/events/event-catalog.ts +2 -74
- package/src/runtime/framework/events/event-delivery-service.ts +2 -200
- package/src/runtime/framework/events/event-ingress.ts +2 -99
- package/src/runtime/framework/events/event-repository.ts +2 -82
- package/src/runtime/framework/events/index.ts +2 -7
- package/src/runtime/framework/events/memory-event-repository.ts +2 -1100
- package/src/runtime/framework/events/runtime-resume.ts +2 -20
- package/src/runtime/framework/events/sqlite-event-repository.ts +5 -2280
- package/src/runtime/framework/events/test-helpers.ts +1 -1
- package/src/runtime/framework/events/types.ts +2 -312
- package/src/runtime/framework/index.ts +3 -7
- package/src/runtime/framework/load-node-sqlite.ts +2 -17
- package/src/runtime/framework/mailbox/coalescer.ts +2 -83
- package/src/runtime/framework/mailbox/host-notification-sink.ts +2 -44
- package/src/runtime/framework/mailbox/identity.ts +2 -38
- package/src/runtime/framework/mailbox/index.ts +1 -7
- package/src/runtime/framework/mailbox/mailbox-dispatcher.ts +2 -282
- package/src/runtime/framework/mailbox/mailbox-repository.ts +2 -44
- package/src/runtime/framework/mailbox/notification-projector.ts +2 -65
- package/src/runtime/framework/mailbox/types.ts +2 -145
- package/src/runtime/framework/modules/runtime-module-loader-hooks.mjs +2 -154
- package/src/runtime/framework/modules/runtime-module-loader.ts +7 -51
- package/src/runtime/framework/modules/runtime-module-output.ts +2 -24
- package/src/runtime/framework/modules/runtime-module-worker-bootstrap.mjs +3 -23
- package/src/runtime/framework/participation-manager.ts +2 -253
- package/src/runtime/framework/persistence/async-sqlite-persistence-worker.ts +2 -77
- package/src/runtime/framework/persistence/memory-first-persistence.ts +4 -489
- package/src/runtime/framework/plugins/contracts.ts +2 -396
- package/src/runtime/framework/plugins/index.ts +1 -4
- package/src/runtime/framework/plugins/runtime-event-identity.ts +2 -17
- package/src/runtime/framework/plugins/runtime-input-journal.ts +2 -866
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +2 -2071
- package/src/runtime/framework/runtime.ts +5 -1843
- package/src/runtime/framework/serial-task-queue.ts +2 -14
- package/src/runtime/framework/state/contracts.ts +2 -49
- package/src/runtime/framework/state/current-state-store.ts +2 -184
- package/src/runtime/framework/state/durable-agent-state-snapshot.ts +2 -209
- package/src/runtime/framework/state/freshness.ts +2 -72
- package/src/runtime/framework/state/history/contracts.ts +2 -158
- package/src/runtime/framework/state/history/history-writer.ts +2 -194
- package/src/runtime/framework/state/history/memory-state-history-repository.ts +2 -230
- package/src/runtime/framework/state/history/sqlite-state-history-repository.ts +2 -648
- package/src/runtime/framework/state/history/trajectory-projector.ts +2 -209
- package/src/runtime/framework/state/index.ts +1 -13
- package/src/runtime/framework/state/state-ingress.ts +2 -95
- package/src/runtime/framework/state/state-provider.ts +2 -62
- package/src/runtime/framework/state/types.ts +2 -155
- package/src/runtime/host-monitor-rate-gate.ts +1 -123
- package/src/runtime/mailbox-host-relay.ts +1 -326
- package/src/runtime/observability/detail-capture.ts +4 -108
- package/src/runtime/observability/process-timing.ts +2 -59
- package/src/runtime/observability/recorder.ts +2 -238
- package/src/runtime/observability/redaction.ts +2 -181
- package/src/runtime/observability/reporter.ts +2 -154
- package/src/runtime/observability/sinks.ts +2 -135
- package/src/runtime/observability/types.ts +2 -92
- package/src/runtime/owner-control.ts +45 -320
- package/src/runtime/parent-process-watch.ts +1 -51
- package/src/runtime/process-identity.ts +1 -72
- package/src/sdk/behavior-plugin/index.ts +9 -3
- package/src/sdk/index.ts +40 -2
- package/src/sdk/module-input/index.ts +5 -0
- package/src/sdk/perception-plugin/index.ts +3 -1
- package/src/sdk/perception-plugin/module.ts +12 -2
- package/src/sdk/perception-plugin/reminder-rules.ts +8 -42
- package/src/sdk/perception-plugin/reminder.ts +2 -1
- package/src/sdk/perception-plugin/task-change-tracker.ts +258 -0
- package/src/sdk/strategy-memory.ts +1 -12
- package/src/sdk/strategy-plugin/index.ts +36 -0
- package/src/sdk/trigger-plugin/index.ts +1 -0
- package/docs/custom-modules/trigger.md +0 -295
|
@@ -1,102 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from '../../../../../pipeline/canonical-event-presentation.js';
|
|
4
|
-
import type { CanonicalEvent, EventRepository } from '../../../../framework/events/index.js';
|
|
5
|
-
import type { RuntimeReadyView } from '../../../../framework/runtime.js';
|
|
6
|
-
import {
|
|
7
|
-
AgentMailboxCoordinator,
|
|
8
|
-
type PreparedAgentMailboxBatch,
|
|
9
|
-
} from '../agent-mailbox-coordinator.js';
|
|
1
|
+
import { ClaudeChannelMailboxAdapter as CoreAdapter, type ClaudeChannelMailboxAdapterOptions as CoreOptions } from '@myclaw163/game-agent-runtime/mailbox/adapters/claude-channel-adapter.js';
|
|
2
|
+
import { isClawclawAgentNotification } from '../../../notification-policy.js';
|
|
3
|
+
import { presentCanonicalEventForChannel } from '../../../../../pipeline/canonical-event-presentation.js';
|
|
10
4
|
import { CLAUDE_CHANNEL_MAILBOX_CONSUMER_ID } from '../consumer-ids.js';
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
export { CLAUDE_CHANNEL_COALESCE_MS } from '@myclaw163/game-agent-runtime/mailbox/adapters/claude-channel-adapter.js';
|
|
13
6
|
export const CLAUDE_CHANNEL_ADAPTER_ID = 'clawclaw.mailbox.adapter.claude-channel';
|
|
14
7
|
export const CLAUDE_CHANNEL_CONSUMER_ID = CLAUDE_CHANNEL_MAILBOX_CONSUMER_ID;
|
|
15
|
-
export
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
readyView: () => RuntimeReadyView;
|
|
21
|
-
consumerId?: string;
|
|
22
|
-
now?: () => number;
|
|
23
|
-
diagnostic?: (name: string, attributes?: Record<string, unknown>) => void;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Mailbox projection and delivery state for the Claude Channel consumer.
|
|
28
|
-
* Transport is handled separately by the Owner-side Channel socket sink.
|
|
29
|
-
*/
|
|
30
|
-
export class ClaudeChannelMailboxAdapter implements ClawclawMailboxDriver {
|
|
31
|
-
readonly consumerId: string;
|
|
32
|
-
readonly adapter: ClawclawMailboxDriver['adapter'];
|
|
33
|
-
private readonly coordinator: AgentMailboxCoordinator;
|
|
34
|
-
|
|
35
|
-
constructor(options: ClaudeChannelMailboxAdapterOptions) {
|
|
36
|
-
this.consumerId = options.consumerId ?? CLAUDE_CHANNEL_CONSUMER_ID;
|
|
37
|
-
this.adapter = {
|
|
38
|
-
id: CLAUDE_CHANNEL_ADAPTER_ID,
|
|
39
|
-
acknowledgement: 'on_delivery',
|
|
40
|
-
consumerId: this.consumerId,
|
|
41
|
-
};
|
|
42
|
-
this.coordinator = new AgentMailboxCoordinator({
|
|
43
|
-
onDelivered: options.onDelivered,
|
|
44
|
-
repository: options.repository,
|
|
45
|
-
readyView: options.readyView,
|
|
46
|
-
presentEvent: presentCanonicalEventForChannel,
|
|
47
|
-
// Channel v2 is Event-only. Other Mailbox adapters keep their own
|
|
48
|
-
// compatibility projections instead of repeating Ready State here.
|
|
49
|
-
compactState: () => null,
|
|
50
|
-
consumerId: this.consumerId,
|
|
51
|
-
coalesceMs: CLAUDE_CHANNEL_COALESCE_MS,
|
|
52
|
-
now: options.now,
|
|
53
|
-
diagnostic: options.diagnostic,
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
prepare(readyView?: RuntimeReadyView): PreparedAgentMailboxBatch {
|
|
58
|
-
const prepared = this.coordinator.prepareEvents({
|
|
59
|
-
readyView,
|
|
60
|
-
deliveryVia: 'claude_channel',
|
|
61
|
-
ownerIdPrefix: 'claude_channel',
|
|
62
|
-
});
|
|
63
|
-
return {
|
|
64
|
-
...prepared,
|
|
65
|
-
batch: {
|
|
66
|
-
...prepared.batch,
|
|
67
|
-
// The repository selects newest-first for legacy pull consumers.
|
|
68
|
-
// Channel Events must be applied in occurrence order now that there is
|
|
69
|
-
// no repeated State snapshot masking backwards transitions.
|
|
70
|
-
events: [...prepared.batch.events].reverse(),
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
ack(deliveryToken: string): boolean {
|
|
76
|
-
return this.coordinator.ack(deliveryToken);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
renew(deliveryToken: string): boolean {
|
|
80
|
-
return this.coordinator.renew(deliveryToken);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
release(deliveryToken: string): boolean {
|
|
84
|
-
return this.coordinator.release(deliveryToken);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
observeEvents(events: readonly CanonicalEvent[]): void {
|
|
88
|
-
this.coordinator.observeEvents(events);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
flush(): Promise<void> {
|
|
92
|
-
return this.coordinator.flush();
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
waitForDrain(shouldAbort: () => boolean = () => false): Promise<boolean> {
|
|
96
|
-
return this.coordinator.waitForDrain(shouldAbort);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
stop(): Promise<void> {
|
|
100
|
-
return this.coordinator.stop();
|
|
8
|
+
export interface ClaudeChannelMailboxAdapterOptions extends Omit<CoreOptions,'id'|'presentEvent'|'admitEvent'|'consumerId'> { notificationOnly?:boolean; consumerId?:string }
|
|
9
|
+
export class ClaudeChannelMailboxAdapter extends CoreAdapter {
|
|
10
|
+
constructor(options:ClaudeChannelMailboxAdapterOptions) {
|
|
11
|
+
super({...options,id:CLAUDE_CHANNEL_ADAPTER_ID,consumerId:options.consumerId ?? CLAUDE_CHANNEL_CONSUMER_ID,
|
|
12
|
+
presentEvent:presentCanonicalEventForChannel,admitEvent:options.notificationOnly ? isClawclawAgentNotification : undefined});
|
|
101
13
|
}
|
|
102
14
|
}
|
|
@@ -1,346 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
readClaudeChannelEndpoint,
|
|
8
|
-
type ClaudeChannelEndpointDescriptor,
|
|
9
|
-
} from '../../../../../channel/claude-channel-ipc.js';
|
|
10
|
-
import type {
|
|
11
|
-
MailboxEventBatchAcceptance,
|
|
12
|
-
MailboxEventBatchEnvelope,
|
|
13
|
-
MailboxEventBatchSink,
|
|
14
|
-
} from '../mailbox-event-batch-delivery.js';
|
|
15
|
-
|
|
16
|
-
const DEFAULT_RECEIPT_TIMEOUT_MS = 10_000;
|
|
17
|
-
const DEFAULT_RECONNECT_BASE_MS = 100;
|
|
18
|
-
const DEFAULT_RECONNECT_MAX_MS = 5_000;
|
|
19
|
-
|
|
20
|
-
export interface ClaudeChannelSocketOptions {
|
|
21
|
-
receiptTimeoutMs?: number;
|
|
22
|
-
reconnectBaseMs?: number;
|
|
23
|
-
reconnectMaxMs?: number;
|
|
24
|
-
now?: () => number;
|
|
25
|
-
endpoint?: () => ClaudeChannelEndpointDescriptor | null;
|
|
26
|
-
onConnected?: () => void;
|
|
27
|
-
diagnostic?: (name: string, attributes?: Record<string, unknown>) => void;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
interface PendingReceipt {
|
|
31
|
-
connectionId: string;
|
|
32
|
-
seq: number;
|
|
33
|
-
deliveryId: string;
|
|
34
|
-
timer: ReturnType<typeof setTimeout>;
|
|
35
|
-
signal: AbortSignal;
|
|
36
|
-
onAbort: () => void;
|
|
37
|
-
resolve: (acceptance: MailboxEventBatchAcceptance) => void;
|
|
38
|
-
reject: (error: Error) => void;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export class ClaudeChannelSocketDeliveryError extends Error {
|
|
42
|
-
readonly code: string;
|
|
43
|
-
readonly retryable: boolean;
|
|
44
|
-
|
|
45
|
-
constructor(code: string, message: string, retryable = true) {
|
|
46
|
-
super(message);
|
|
47
|
-
this.name = 'ClaudeChannelSocketDeliveryError';
|
|
48
|
-
this.code = code;
|
|
49
|
-
this.retryable = retryable;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** Owner-side IPC client. Mailbox batches flow outward to the Channel MCP process. */
|
|
54
|
-
export class ClaudeChannelSocketSink implements MailboxEventBatchSink {
|
|
55
|
-
private readonly receiptTimeoutMs: number;
|
|
56
|
-
private readonly reconnectBaseMs: number;
|
|
57
|
-
private readonly reconnectMaxMs: number;
|
|
58
|
-
private readonly now: () => number;
|
|
59
|
-
private readonly endpoint: () => ClaudeChannelEndpointDescriptor | null;
|
|
60
|
-
private readonly onConnected?: ClaudeChannelSocketOptions['onConnected'];
|
|
61
|
-
private readonly diagnostic?: ClaudeChannelSocketOptions['diagnostic'];
|
|
62
|
-
|
|
63
|
-
private socket?: Socket;
|
|
64
|
-
private ready = false;
|
|
65
|
-
private stopped = false;
|
|
66
|
-
private connectionId?: string;
|
|
67
|
-
private sequence = 0;
|
|
68
|
-
private reconnectAttempt = 0;
|
|
69
|
-
private reconnectTimer?: ReturnType<typeof setTimeout>;
|
|
70
|
-
private inputBuffer = '';
|
|
71
|
-
private pendingReceipt?: PendingReceipt;
|
|
72
|
-
|
|
1
|
+
import { ClaudeChannelSocketSink as CoreChannelSink, type ClaudeChannelSocketOptions as CoreOptions } from '@myclaw163/game-agent-runtime/mailbox/adapters/claude-channel-socket.js';
|
|
2
|
+
import { CLAUDE_CHANNEL_IPC_SCHEMA, CLAUDE_CHANNEL_MAX_FRAME_CHARACTERS, CLAWCLAW_CHANNEL_EVENTS_SCHEMA, readClaudeChannelEndpoint,
|
|
3
|
+
type ClaudeChannelEndpointDescriptor } from '../../../../../channel/claude-channel-ipc.js';
|
|
4
|
+
export { ClaudeChannelSocketDeliveryError } from '@myclaw163/game-agent-runtime/mailbox/adapters/claude-channel-socket.js';
|
|
5
|
+
export interface ClaudeChannelSocketOptions extends Omit<CoreOptions,'protocol'|'endpoint'> { endpoint?: () => ClaudeChannelEndpointDescriptor | null }
|
|
6
|
+
export class ClaudeChannelSocketSink extends CoreChannelSink {
|
|
73
7
|
constructor(options: ClaudeChannelSocketOptions = {}) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
this.reconnectMaxMs = options.reconnectMaxMs ?? DEFAULT_RECONNECT_MAX_MS;
|
|
77
|
-
this.now = options.now ?? Date.now;
|
|
78
|
-
this.endpoint = options.endpoint ?? (() => readClaudeChannelEndpoint());
|
|
79
|
-
this.onConnected = options.onConnected;
|
|
80
|
-
this.diagnostic = options.diagnostic;
|
|
81
|
-
if (!Number.isFinite(this.receiptTimeoutMs) || this.receiptTimeoutMs <= 0) {
|
|
82
|
-
throw new Error('Claude Channel IPC receipt timeout must be positive.');
|
|
83
|
-
}
|
|
84
|
-
if (
|
|
85
|
-
!Number.isFinite(this.reconnectBaseMs)
|
|
86
|
-
|| this.reconnectBaseMs <= 0
|
|
87
|
-
|| !Number.isFinite(this.reconnectMaxMs)
|
|
88
|
-
|| this.reconnectMaxMs < this.reconnectBaseMs
|
|
89
|
-
) {
|
|
90
|
-
throw new Error('Claude Channel IPC reconnect delays are invalid.');
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
start(): void {
|
|
95
|
-
if (this.stopped || this.socket || this.reconnectTimer) return;
|
|
96
|
-
this.connect();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async deliver(
|
|
100
|
-
envelope: MailboxEventBatchEnvelope,
|
|
101
|
-
signal: AbortSignal,
|
|
102
|
-
): Promise<MailboxEventBatchAcceptance> {
|
|
103
|
-
if (this.stopped) {
|
|
104
|
-
throw new ClaudeChannelSocketDeliveryError(
|
|
105
|
-
'claude_channel_socket_stopped',
|
|
106
|
-
'Claude Channel IPC is stopped.',
|
|
107
|
-
false,
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
if (signal.aborted) throw this.abortError(signal);
|
|
111
|
-
const socket = this.socket;
|
|
112
|
-
const connectionId = this.connectionId;
|
|
113
|
-
if (!socket || !this.ready || !connectionId) {
|
|
114
|
-
this.start();
|
|
115
|
-
throw new ClaudeChannelSocketDeliveryError(
|
|
116
|
-
'claude_channel_socket_not_connected',
|
|
117
|
-
'Claude Channel IPC is not connected.',
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
if (this.pendingReceipt) {
|
|
121
|
-
throw new ClaudeChannelSocketDeliveryError(
|
|
122
|
-
'claude_channel_batch_in_flight',
|
|
123
|
-
'A Claude Channel batch is already awaiting its transport receipt.',
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const seq = ++this.sequence;
|
|
128
|
-
const frame = {
|
|
129
|
-
schema: CLAUDE_CHANNEL_IPC_SCHEMA,
|
|
130
|
-
type: 'mailbox_batch',
|
|
131
|
-
connection_id: connectionId,
|
|
132
|
-
seq,
|
|
133
|
-
delivery_id: envelope.deliveryId,
|
|
134
|
-
payload: {
|
|
135
|
-
schema: CLAWCLAW_CHANNEL_EVENTS_SCHEMA,
|
|
136
|
-
events: envelope.batch.events,
|
|
137
|
-
},
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
return new Promise<MailboxEventBatchAcceptance>((resolve, reject) => {
|
|
141
|
-
const onAbort = (): void => {
|
|
142
|
-
if (this.pendingReceipt?.seq !== seq) return;
|
|
143
|
-
this.rejectPending(this.abortError(signal));
|
|
144
|
-
socket.destroy();
|
|
145
|
-
};
|
|
146
|
-
const timer = setTimeout(() => {
|
|
147
|
-
if (this.pendingReceipt?.seq !== seq) return;
|
|
148
|
-
this.rejectPending(new ClaudeChannelSocketDeliveryError(
|
|
149
|
-
'claude_channel_receipt_timeout',
|
|
150
|
-
'Channel MCP did not return the transport receipt in time.',
|
|
151
|
-
));
|
|
152
|
-
socket.destroy();
|
|
153
|
-
}, this.receiptTimeoutMs);
|
|
154
|
-
timer.unref?.();
|
|
155
|
-
this.pendingReceipt = {
|
|
156
|
-
connectionId,
|
|
157
|
-
seq,
|
|
158
|
-
deliveryId: envelope.deliveryId,
|
|
159
|
-
timer,
|
|
160
|
-
signal,
|
|
161
|
-
onAbort,
|
|
162
|
-
resolve,
|
|
163
|
-
reject,
|
|
164
|
-
};
|
|
165
|
-
signal.addEventListener('abort', onAbort, { once: true });
|
|
166
|
-
socket.write(`${JSON.stringify(frame)}\n`, (error?: Error | null) => {
|
|
167
|
-
if (!error || this.pendingReceipt?.seq !== seq) return;
|
|
168
|
-
this.rejectPending(new ClaudeChannelSocketDeliveryError(
|
|
169
|
-
'claude_channel_socket_write_failed',
|
|
170
|
-
error.message,
|
|
171
|
-
));
|
|
172
|
-
socket.destroy();
|
|
173
|
-
});
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
async close(): Promise<void> {
|
|
178
|
-
if (this.stopped) return;
|
|
179
|
-
this.stopped = true;
|
|
180
|
-
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
|
|
181
|
-
this.reconnectTimer = undefined;
|
|
182
|
-
this.rejectPending(new ClaudeChannelSocketDeliveryError(
|
|
183
|
-
'claude_channel_socket_stopped',
|
|
184
|
-
'Claude Channel IPC stopped before transport acceptance.',
|
|
185
|
-
false,
|
|
186
|
-
));
|
|
187
|
-
const socket = this.socket;
|
|
188
|
-
this.socket = undefined;
|
|
189
|
-
this.ready = false;
|
|
190
|
-
this.connectionId = undefined;
|
|
191
|
-
if (!socket || socket.destroyed) return;
|
|
192
|
-
await new Promise<void>((resolve) => {
|
|
193
|
-
socket.once('close', () => resolve());
|
|
194
|
-
socket.destroy();
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
private connect(): void {
|
|
199
|
-
if (this.stopped || this.socket) return;
|
|
200
|
-
const endpoint = this.endpoint();
|
|
201
|
-
if (!endpoint) {
|
|
202
|
-
this.scheduleReconnect('endpoint_unavailable');
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
const connectionId = randomUUID();
|
|
206
|
-
const socket = createConnection(endpoint.socket_path);
|
|
207
|
-
this.socket = socket;
|
|
208
|
-
this.connectionId = connectionId;
|
|
209
|
-
this.ready = false;
|
|
210
|
-
this.inputBuffer = '';
|
|
211
|
-
socket.setEncoding('utf8');
|
|
212
|
-
socket.setNoDelay(true);
|
|
213
|
-
socket.once('connect', () => {
|
|
214
|
-
if (this.socket !== socket || this.stopped) return;
|
|
215
|
-
socket.write(`${JSON.stringify({
|
|
216
|
-
schema: CLAUDE_CHANNEL_IPC_SCHEMA,
|
|
217
|
-
type: 'hello',
|
|
218
|
-
connection_id: connectionId,
|
|
219
|
-
token: endpoint.token,
|
|
220
|
-
owner_pid: process.pid,
|
|
221
|
-
})}\n`);
|
|
222
|
-
});
|
|
223
|
-
socket.on('data', (chunk: string) => this.onData(socket, chunk));
|
|
224
|
-
socket.once('error', (error) => {
|
|
225
|
-
this.diagnostic?.('runtime.channel.ipc_error', {
|
|
226
|
-
code: typeof (error as NodeJS.ErrnoException).code === 'string'
|
|
227
|
-
? (error as NodeJS.ErrnoException).code
|
|
228
|
-
: error.name,
|
|
229
|
-
});
|
|
230
|
-
});
|
|
231
|
-
socket.once('close', () => this.onClose(socket));
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
private onData(socket: Socket, chunk: string): void {
|
|
235
|
-
if (this.socket !== socket || this.stopped) return;
|
|
236
|
-
this.inputBuffer += chunk;
|
|
237
|
-
if (this.inputBuffer.length > CLAUDE_CHANNEL_MAX_FRAME_CHARACTERS) {
|
|
238
|
-
socket.destroy(new Error('claude_channel_ipc_receipt_frame_too_large'));
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
for (;;) {
|
|
242
|
-
const newline = this.inputBuffer.indexOf('\n');
|
|
243
|
-
if (newline < 0) return;
|
|
244
|
-
const line = this.inputBuffer.slice(0, newline).trim();
|
|
245
|
-
this.inputBuffer = this.inputBuffer.slice(newline + 1);
|
|
246
|
-
if (!line) continue;
|
|
247
|
-
let frame: Record<string, unknown>;
|
|
248
|
-
try {
|
|
249
|
-
const parsed = JSON.parse(line);
|
|
250
|
-
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) throw new Error();
|
|
251
|
-
frame = parsed as Record<string, unknown>;
|
|
252
|
-
} catch {
|
|
253
|
-
socket.destroy(new Error('claude_channel_ipc_receipt_invalid_json'));
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
this.handleFrame(frame);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
private handleFrame(frame: Record<string, unknown>): void {
|
|
261
|
-
if (
|
|
262
|
-
!this.ready
|
|
263
|
-
&& frame.schema === CLAUDE_CHANNEL_IPC_SCHEMA
|
|
264
|
-
&& frame.type === 'ready'
|
|
265
|
-
&& frame.connection_id === this.connectionId
|
|
266
|
-
) {
|
|
267
|
-
this.ready = true;
|
|
268
|
-
this.reconnectAttempt = 0;
|
|
269
|
-
this.diagnostic?.('runtime.channel.ipc_connected');
|
|
270
|
-
this.onConnected?.();
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
273
|
-
const pending = this.pendingReceipt;
|
|
274
|
-
if (
|
|
275
|
-
!pending
|
|
276
|
-
|| frame.schema !== CLAUDE_CHANNEL_IPC_SCHEMA
|
|
277
|
-
|| frame.type !== 'receipt'
|
|
278
|
-
|| frame.connection_id !== pending.connectionId
|
|
279
|
-
|| frame.seq !== pending.seq
|
|
280
|
-
|| frame.delivery_id !== pending.deliveryId
|
|
281
|
-
) {
|
|
282
|
-
this.diagnostic?.('runtime.channel.ipc_receipt_ignored');
|
|
283
|
-
return;
|
|
284
|
-
}
|
|
285
|
-
const acceptedAt = typeof frame.accepted_at === 'string'
|
|
286
|
-
? frame.accepted_at
|
|
287
|
-
: new Date(this.now()).toISOString();
|
|
288
|
-
const receiptId = typeof frame.receipt_id === 'string' && frame.receipt_id.length <= 256
|
|
289
|
-
? frame.receipt_id
|
|
290
|
-
: undefined;
|
|
291
|
-
this.pendingReceipt = undefined;
|
|
292
|
-
clearTimeout(pending.timer);
|
|
293
|
-
pending.signal.removeEventListener('abort', pending.onAbort);
|
|
294
|
-
pending.resolve({ acceptedAt, ...(receiptId ? { receiptId } : {}) });
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
private onClose(socket: Socket): void {
|
|
298
|
-
if (this.socket !== socket) return;
|
|
299
|
-
this.socket = undefined;
|
|
300
|
-
this.ready = false;
|
|
301
|
-
this.connectionId = undefined;
|
|
302
|
-
this.inputBuffer = '';
|
|
303
|
-
this.rejectPending(new ClaudeChannelSocketDeliveryError(
|
|
304
|
-
'claude_channel_socket_disconnected',
|
|
305
|
-
'Claude Channel IPC disconnected before transport acceptance.',
|
|
306
|
-
));
|
|
307
|
-
if (!this.stopped) this.scheduleReconnect('disconnected');
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
private scheduleReconnect(reason: string): void {
|
|
311
|
-
if (this.stopped || this.reconnectTimer) return;
|
|
312
|
-
this.reconnectAttempt += 1;
|
|
313
|
-
const delayMs = Math.min(
|
|
314
|
-
this.reconnectMaxMs,
|
|
315
|
-
this.reconnectBaseMs * (2 ** Math.min(8, this.reconnectAttempt - 1)),
|
|
316
|
-
);
|
|
317
|
-
this.diagnostic?.('runtime.channel.ipc_reconnecting', {
|
|
318
|
-
reason,
|
|
319
|
-
retry_in_ms: delayMs,
|
|
320
|
-
});
|
|
321
|
-
const timer = setTimeout(() => {
|
|
322
|
-
this.reconnectTimer = undefined;
|
|
323
|
-
this.connect();
|
|
324
|
-
}, delayMs);
|
|
325
|
-
timer.unref?.();
|
|
326
|
-
this.reconnectTimer = timer;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
private rejectPending(error: Error): void {
|
|
330
|
-
const pending = this.pendingReceipt;
|
|
331
|
-
if (!pending) return;
|
|
332
|
-
this.pendingReceipt = undefined;
|
|
333
|
-
clearTimeout(pending.timer);
|
|
334
|
-
pending.signal.removeEventListener('abort', pending.onAbort);
|
|
335
|
-
pending.reject(error);
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
private abortError(signal: AbortSignal): ClaudeChannelSocketDeliveryError {
|
|
339
|
-
return new ClaudeChannelSocketDeliveryError(
|
|
340
|
-
'claude_channel_delivery_aborted',
|
|
341
|
-
signal.reason instanceof Error
|
|
342
|
-
? signal.reason.message
|
|
343
|
-
: 'Claude Channel delivery aborted.',
|
|
344
|
-
);
|
|
8
|
+
super({...options,endpoint:options.endpoint ?? (()=>readClaudeChannelEndpoint()),
|
|
9
|
+
protocol:{schema:CLAUDE_CHANNEL_IPC_SCHEMA,payloadSchema:CLAWCLAW_CHANNEL_EVENTS_SCHEMA,maxFrameCharacters:CLAUDE_CHANNEL_MAX_FRAME_CHARACTERS}});
|
|
345
10
|
}
|
|
346
11
|
}
|