@myagentroam/agent 0.9.159 → 0.9.160

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 (128) hide show
  1. package/dist/agent-core/extensions/agents.d.ts +69 -0
  2. package/dist/agent-core/extensions/agents.js +160 -0
  3. package/dist/agent-core/extensions/contracts.d.ts +260 -0
  4. package/dist/agent-core/extensions/contracts.js +1 -0
  5. package/dist/agent-core/extensions/data.d.ts +84 -0
  6. package/dist/agent-core/extensions/data.js +83 -0
  7. package/dist/agent-core/extensions/environment.d.ts +27 -0
  8. package/dist/agent-core/extensions/environment.js +52 -0
  9. package/dist/agent-core/extensions/files.d.ts +64 -0
  10. package/dist/agent-core/extensions/files.js +26 -0
  11. package/dist/agent-core/extensions/hooks.d.ts +32 -0
  12. package/dist/agent-core/extensions/hooks.js +135 -0
  13. package/dist/agent-core/extensions/manager.d.ts +39 -0
  14. package/dist/agent-core/extensions/manager.js +458 -0
  15. package/dist/agent-core/extensions/mcp.d.ts +99 -0
  16. package/dist/agent-core/extensions/mcp.js +433 -0
  17. package/dist/agent-core/extensions/model-request.d.ts +60 -0
  18. package/dist/agent-core/extensions/model-request.js +167 -0
  19. package/dist/agent-core/extensions/model-stream.d.ts +9 -0
  20. package/dist/agent-core/extensions/model-stream.js +101 -0
  21. package/dist/agent-core/extensions/models.d.ts +43 -0
  22. package/dist/agent-core/extensions/models.js +1 -0
  23. package/dist/agent-core/extensions/network.d.ts +32 -0
  24. package/dist/agent-core/extensions/network.js +97 -0
  25. package/dist/agent-core/extensions/operation.d.ts +29 -0
  26. package/dist/agent-core/extensions/operation.js +98 -0
  27. package/dist/agent-core/extensions/processes.d.ts +52 -0
  28. package/dist/agent-core/extensions/processes.js +283 -0
  29. package/dist/agent-core/extensions/prompt-composition.d.ts +24 -0
  30. package/dist/agent-core/extensions/prompt-composition.js +106 -0
  31. package/dist/agent-core/extensions/prompt-input.d.ts +23 -0
  32. package/dist/agent-core/extensions/prompt-input.js +46 -0
  33. package/dist/agent-core/extensions/resources.d.ts +27 -0
  34. package/dist/agent-core/extensions/resources.js +221 -0
  35. package/dist/agent-core/extensions/services.d.ts +31 -0
  36. package/dist/agent-core/extensions/services.js +61 -0
  37. package/dist/agent-core/extensions/session-input.d.ts +151 -0
  38. package/dist/agent-core/extensions/session-input.js +351 -0
  39. package/dist/agent-core/extensions/session-messages.d.ts +41 -0
  40. package/dist/agent-core/extensions/session-messages.js +76 -0
  41. package/dist/agent-core/extensions/session.d.ts +68 -0
  42. package/dist/agent-core/extensions/session.js +593 -0
  43. package/dist/agent-core/extensions/settings.d.ts +59 -0
  44. package/dist/agent-core/extensions/settings.js +115 -0
  45. package/dist/agent-core/extensions/store.d.ts +10 -0
  46. package/dist/agent-core/extensions/store.js +131 -0
  47. package/dist/agent-core/extensions/tools.d.ts +61 -0
  48. package/dist/agent-core/extensions/tools.js +266 -0
  49. package/dist/agent-core/host/contracts.d.ts +25 -0
  50. package/dist/agent-core/host/local-agent-host.d.ts +10 -1
  51. package/dist/agent-core/host/local-agent-host.js +132 -20
  52. package/dist/agent-core/host/local-file-access.d.ts +30 -0
  53. package/dist/agent-core/host/local-file-access.js +107 -0
  54. package/dist/agent-core/host/local-network-access.d.ts +6 -0
  55. package/dist/agent-core/host/local-network-access.js +97 -0
  56. package/dist/agent-core/host/local-process-executor.d.ts +5 -1
  57. package/dist/agent-core/host/local-session-environment.d.ts +18 -0
  58. package/dist/agent-core/host/local-session-environment.js +102 -0
  59. package/dist/agent-core/model/anthropic-messages.js +73 -23
  60. package/dist/agent-core/model/contracts.d.ts +12 -0
  61. package/dist/agent-core/model/openai-responses.js +73 -12
  62. package/dist/agent-core/model/text-blocks.d.ts +7 -0
  63. package/dist/agent-core/model/text-blocks.js +40 -0
  64. package/dist/agent-core/prompts/index.d.ts +6 -2
  65. package/dist/agent-core/prompts/index.js +38 -24
  66. package/dist/agent-core/runtime/context/gc-tokens.js +6 -1
  67. package/dist/agent-core/runtime/context/normalize.js +21 -1
  68. package/dist/agent-core/runtime/context/projection.js +35 -1
  69. package/dist/agent-core/runtime/context/request-budget.js +5 -2
  70. package/dist/agent-core/runtime/context/restore.d.ts +5 -0
  71. package/dist/agent-core/runtime/context/restore.js +47 -4
  72. package/dist/agent-core/runtime/context/usage.js +2 -3
  73. package/dist/agent-core/runtime/contracts.d.ts +2 -0
  74. package/dist/agent-core/runtime/execution.js +5 -2
  75. package/dist/agent-core/sdk/agent.js +177 -40
  76. package/dist/agent-core/sdk/extension-agents.d.ts +9 -0
  77. package/dist/agent-core/sdk/extension-agents.js +51 -0
  78. package/dist/agent-core/sdk/extension-models.d.ts +13 -0
  79. package/dist/agent-core/sdk/extension-models.js +128 -0
  80. package/dist/agent-core/sdk/extension-session.d.ts +30 -0
  81. package/dist/agent-core/sdk/extension-session.js +103 -0
  82. package/dist/agent-core/sdk/extension-tools.d.ts +16 -0
  83. package/dist/agent-core/sdk/extension-tools.js +126 -0
  84. package/dist/agent-core/sdk/session.d.ts +26 -2
  85. package/dist/agent-core/sdk/session.js +1215 -506
  86. package/dist/agent-core/sdk/types.d.ts +3 -0
  87. package/dist/agent-core/session/context-gc-checkpoint.js +1 -1
  88. package/dist/agent-core/session/jsonl-store.d.ts +28 -8
  89. package/dist/agent-core/session/jsonl-store.js +124 -84
  90. package/dist/agent-core/session/types.d.ts +4 -1
  91. package/dist/agent-core/storage/file-lock.d.ts +4 -0
  92. package/dist/agent-core/storage/file-lock.js +83 -0
  93. package/dist/agent-core/subagent/contracts.d.ts +11 -1
  94. package/dist/agent-core/subagent/scheduler.d.ts +8 -0
  95. package/dist/agent-core/subagent/session-controller.d.ts +22 -1
  96. package/dist/agent-core/subagent/session-controller.js +355 -98
  97. package/dist/agent-core/tools/agent/agent-start.d.ts +1 -1
  98. package/dist/agent-core/tools/code-mode/cells.js +5 -15
  99. package/dist/agent-core/tools/code-mode/dispatch.d.ts +1 -0
  100. package/dist/agent-core/tools/code-mode/dispatch.js +13 -1
  101. package/dist/agent-core/tools/code-mode/index.js +1 -2
  102. package/dist/agent-core/tools/code-mode/observations.d.ts +5 -0
  103. package/dist/agent-core/tools/code-mode/observations.js +16 -5
  104. package/dist/agent-core/tools/code-mode/quickjs-engine.js +10 -5
  105. package/dist/agent-core/tools/execution.d.ts +11 -2
  106. package/dist/agent-core/tools/execution.js +58 -21
  107. package/dist/agent-core/tools/input.d.ts +2 -0
  108. package/dist/agent-core/tools/input.js +36 -0
  109. package/dist/agent-core/tools/local/apply-patch.js +21 -17
  110. package/dist/agent-core/tools/local/exec.d.ts +4 -4
  111. package/dist/agent-core/tools/local/exec.js +234 -62
  112. package/dist/agent-core/tools/local/registry.d.ts +6 -0
  113. package/dist/agent-core/tools/local/registry.js +60 -9
  114. package/dist/agent-core/tools/mcp/catalog.d.ts +6 -2
  115. package/dist/agent-core/tools/mcp/catalog.js +4 -4
  116. package/dist/agent-core/tools/mcp/history.js +2 -2
  117. package/dist/agent-core/tools/mcp/tool-search.d.ts +3 -1
  118. package/dist/agent-core/tools/mcp/tool-search.js +2 -2
  119. package/dist/agent-core/tools/output-projection.d.ts +3 -1
  120. package/dist/agent-core/tools/output-projection.js +11 -2
  121. package/dist/agent-core/tools/runtime-context.d.ts +1 -0
  122. package/dist/agent-core/tools/runtime.d.ts +3 -1
  123. package/dist/agent-core/tools/runtime.js +22 -7
  124. package/dist/agent-core/tools/scheduler.d.ts +2 -0
  125. package/dist/agent-core/tools/scheduler.js +43 -6
  126. package/dist/index.d.ts +21 -0
  127. package/dist/sandbox/linux/bubblewrap-sandbox-executor.js +5 -2
  128. package/package.json +1 -1
@@ -0,0 +1,69 @@
1
+ import type { MarAgentReasoningEffort } from '../model/configuration.js';
2
+ import type { SubagentController, SubagentTaskSnapshot, SubagentHandledResult } from '../subagent/contracts.js';
3
+ import type { CoreOperationContext } from './operation.js';
4
+ import type { ExtensionResourceScope } from './resources.js';
5
+ import type { ExtensionHooks } from './hooks.js';
6
+ import type { CoreDomainOperations, CoreDomainFacts, CoreCapabilitySnapshot } from './contracts.js';
7
+ export type CoreAgentSnapshot = Readonly<SubagentTaskSnapshot>;
8
+ export type CoreAgentHandledResult = SubagentHandledResult;
9
+ export type CoreAgentSpawnResult = CoreAgentSnapshot | CoreAgentHandledResult;
10
+ export declare function isHandledAgentResult(value: CoreAgentSpawnResult): value is CoreAgentHandledResult;
11
+ export interface CoreAgentSpawnInput {
12
+ readonly turnSignal?: AbortSignal;
13
+ readonly waitFor?: 'start' | 'completion';
14
+ readonly background?: boolean;
15
+ readonly prompt: string;
16
+ readonly description?: string;
17
+ readonly modelId?: string;
18
+ readonly reasoningEffort?: MarAgentReasoningEffort;
19
+ readonly role?: 'general-purpose' | 'explore';
20
+ readonly signal?: AbortSignal;
21
+ }
22
+ export interface CoreAgentTarget {
23
+ readonly agentId: string;
24
+ readonly signal?: AbortSignal;
25
+ }
26
+ export interface CoreAgentWaitInput {
27
+ readonly untilSettled?: true;
28
+ readonly agentId?: string;
29
+ readonly signal?: AbortSignal;
30
+ readonly waitMs?: number;
31
+ readonly waitFor?: 'completion' | 'message';
32
+ }
33
+ export interface CoreAgentMessageInput extends CoreAgentTarget {
34
+ readonly message: string;
35
+ readonly delivery?: 'append' | 'replace';
36
+ }
37
+ export interface CoreAgentsBackend {
38
+ own(ownerInstanceId: string): () => Promise<void>;
39
+ spawn(input: CoreAgentSpawnInput, call: CoreOperationContext): Promise<CoreAgentSnapshot>;
40
+ list(call: CoreOperationContext): Promise<readonly CoreAgentSnapshot[]>;
41
+ inspect(input: CoreAgentTarget, call: CoreOperationContext): Promise<CoreAgentSnapshot>;
42
+ wait(input: CoreAgentWaitInput, call: CoreOperationContext): Promise<CoreAgentSnapshot>;
43
+ message(input: CoreAgentMessageInput, call: CoreOperationContext): Promise<CoreAgentSnapshot>;
44
+ cancel(input: CoreAgentTarget, call: CoreOperationContext): Promise<CoreAgentSnapshot>;
45
+ }
46
+ export interface CoreAgentsApi {
47
+ spawn(input: CoreAgentSpawnInput): Promise<CoreAgentSpawnResult>;
48
+ list(input?: {
49
+ readonly signal?: AbortSignal;
50
+ }): Promise<readonly CoreAgentSnapshot[]>;
51
+ inspect(input: CoreAgentTarget): Promise<CoreAgentSnapshot>;
52
+ wait(input: CoreAgentWaitInput): Promise<CoreAgentSnapshot>;
53
+ message(input: CoreAgentMessageInput): Promise<CoreAgentSnapshot>;
54
+ cancel(input: CoreAgentTarget): Promise<CoreAgentSnapshot>;
55
+ }
56
+ export interface CoreAgentCompletion {
57
+ readonly operation: 'spawn' | 'wait' | 'message' | 'cancel';
58
+ readonly agentId: string;
59
+ readonly status: SubagentTaskSnapshot['status'];
60
+ readonly deliveryStatus?: SubagentTaskSnapshot['deliveryStatus'];
61
+ }
62
+ type AgentOperation = 'agent.spawn' | 'agent.wait' | 'agent.message' | 'agent.cancel';
63
+ export type CoreAgentAction = <Key extends AgentOperation>(key: Key, input: CoreDomainOperations[Key]['input'], leaf: (input: CoreDomainOperations[Key]['input'], call: CoreOperationContext) => Promise<CoreDomainOperations[Key]['output']>) => Promise<CoreDomainOperations[Key]['output']>;
64
+ /** All callers share one interception and authoritative leaf-completion boundary. */
65
+ export declare function invokeCoreAgentOperation<Key extends AgentOperation>(hooks: ExtensionHooks<CoreDomainOperations, CoreDomainFacts>, key: Key, input: CoreDomainOperations[Key]['input'], call: CoreOperationContext, leaf: (input: CoreDomainOperations[Key]['input'], call: CoreOperationContext) => Promise<CoreDomainOperations[Key]['output']>): Promise<CoreDomainOperations[Key]['output']>;
66
+ export declare function bindAgentController(controller: SubagentController, action: CoreAgentAction): SubagentController;
67
+ export declare function agentCapabilities(capabilities: CoreCapabilitySnapshot, available: boolean): CoreCapabilitySnapshot;
68
+ export declare function bindCoreAgents(backend: CoreAgentsBackend, scope: ExtensionResourceScope, run: <Value>(operation: (call: CoreOperationContext) => Promise<Value>, signal?: AbortSignal) => Promise<Value>, hooks: ExtensionHooks<CoreDomainOperations, CoreDomainFacts>): CoreAgentsApi;
69
+ export {};
@@ -0,0 +1,160 @@
1
+ import { MarAgentError } from '../error.js';
2
+ export function isHandledAgentResult(value) {
3
+ return value.outcome === 'handled' && !Object.hasOwn(value, 'agentId');
4
+ }
5
+ /** All callers share one interception and authoritative leaf-completion boundary. */
6
+ export function invokeCoreAgentOperation(hooks, key, input, call, leaf) {
7
+ return hooks.invoke(key, input, call, async (input, effective) => {
8
+ const result = structuredClone(await leaf(input, effective));
9
+ if (isHandledAgentResult(result))
10
+ return result;
11
+ await hooks.emit('agent.completed', {
12
+ operation: key.slice('agent.'.length),
13
+ agentId: result.agentId,
14
+ status: result.status,
15
+ ...(result.deliveryStatus === undefined ? {} : { deliveryStatus: result.deliveryStatus })
16
+ }, effective);
17
+ return result;
18
+ });
19
+ }
20
+ function resolveAgentSpawn(input) {
21
+ if (!input ||
22
+ typeof input !== 'object' ||
23
+ Array.isArray(input) ||
24
+ typeof input.prompt !== 'string' ||
25
+ !input.prompt.trim() ||
26
+ (input.turnSignal !== undefined && !(input.turnSignal instanceof AbortSignal)) ||
27
+ (input.description !== undefined &&
28
+ (typeof input.description !== 'string' || !input.description.trim())) ||
29
+ (input.modelId !== undefined && (typeof input.modelId !== 'string' || !input.modelId)) ||
30
+ (input.role !== undefined && input.role !== 'general-purpose' && input.role !== 'explore') ||
31
+ (input.background !== undefined && typeof input.background !== 'boolean') ||
32
+ (input.waitFor !== undefined && input.waitFor !== 'start' && input.waitFor !== 'completion') ||
33
+ (input.reasoningEffort !== undefined &&
34
+ !['low', 'medium', 'high', 'xhigh', 'max', 'ultra'].includes(input.reasoningEffort)))
35
+ throw invalid('Agent spawn input is invalid.');
36
+ return {
37
+ prompt: input.prompt,
38
+ ...(input.turnSignal === undefined ? {} : { turnSignal: input.turnSignal }),
39
+ ...(input.waitFor === undefined ? {} : { waitFor: input.waitFor }),
40
+ ...(input.background === undefined ? {} : { background: input.background }),
41
+ description: input.description ?? 'Extension agent',
42
+ ...(input.signal === undefined ? {} : { signal: input.signal }),
43
+ ...(input.modelId === undefined ? {} : { modelId: input.modelId }),
44
+ ...(input.reasoningEffort === undefined ? {} : { reasoningEffort: input.reasoningEffort }),
45
+ role: input.role ?? 'general-purpose'
46
+ };
47
+ }
48
+ export function bindAgentController(controller, action) {
49
+ const wait = (input) => action('agent.wait', input, (target, call) => target.agentId === undefined
50
+ ? controller.outputAny({ ...target, signal: call.signal })
51
+ : controller.output(target.agentId, { ...target, signal: call.signal }));
52
+ return {
53
+ run: (input, options) => {
54
+ return action('agent.spawn', {
55
+ ...(options?.waitFor === undefined ? {} : { waitFor: options.waitFor }),
56
+ prompt: input.prompt,
57
+ description: input.description,
58
+ role: input.role,
59
+ background: input.background ?? false,
60
+ ...(input.modelId === undefined ? {} : { modelId: input.modelId }),
61
+ ...(input.reasoningEffort === undefined ? {} : { reasoningEffort: input.reasoningEffort })
62
+ }, async (target, call) => {
63
+ const task = resolveAgentSpawn(target);
64
+ return controller.run({
65
+ prompt: task.prompt,
66
+ description: task.description ?? input.description,
67
+ role: task.role ?? input.role,
68
+ background: task.background ?? false,
69
+ ...(input.parentSubagentId === undefined
70
+ ? {}
71
+ : { parentSubagentId: input.parentSubagentId }),
72
+ ...(task.modelId === undefined ? {} : { modelId: task.modelId }),
73
+ ...(task.reasoningEffort === undefined
74
+ ? {}
75
+ : { reasoningEffort: task.reasoningEffort })
76
+ }, {
77
+ ...options,
78
+ ...(task.turnSignal === undefined ? {} : { turnSignal: task.turnSignal }),
79
+ ...(task.waitFor === undefined ? {} : { waitFor: task.waitFor }),
80
+ signal: call.signal
81
+ });
82
+ });
83
+ },
84
+ output: (id, options) => wait({ ...options, agentId: id }),
85
+ outputAny: (options) => wait({ ...options }),
86
+ message: (id, message, options) => action('agent.message', { ...options, agentId: id, message }, (target, call) => controller.message(target.agentId, target.message, {
87
+ ...(target.delivery === undefined ? {} : { delivery: target.delivery }),
88
+ signal: call.signal
89
+ })),
90
+ cancel: (id) => action('agent.cancel', { agentId: id }, (target) => controller.cancel(target.agentId)),
91
+ listSessionResources: () => controller.listSessionResources(),
92
+ dispose: () => controller.dispose()
93
+ };
94
+ }
95
+ export function agentCapabilities(capabilities, available) {
96
+ const values = Object.freeze(capabilities.list().filter((id) => id !== 'agents' || available));
97
+ return Object.freeze({ has: (id) => values.includes(id), list: () => values });
98
+ }
99
+ export function bindCoreAgents(backend, scope, run, hooks) {
100
+ scope.effect(() => backend.own(scope.instanceId));
101
+ const invoke = (key, input) => run((call) => invokeCoreAgentOperation(hooks, key, input, call, async (effectiveInput, effective) => {
102
+ let result;
103
+ switch (key) {
104
+ case 'agent.spawn':
105
+ result = await backend.spawn({
106
+ ...resolveAgentSpawn(effectiveInput),
107
+ ...(effectiveInput.turnSignal === undefined
108
+ ? {}
109
+ : {
110
+ turnSignal: AbortSignal.any([
111
+ scope.signal,
112
+ effectiveInput.turnSignal
113
+ ])
114
+ })
115
+ }, effective);
116
+ break;
117
+ case 'agent.wait':
118
+ result = await backend.wait(effectiveInput, effective);
119
+ break;
120
+ case 'agent.message':
121
+ result = await backend.message(effectiveInput, effective);
122
+ break;
123
+ case 'agent.cancel':
124
+ result = await backend.cancel(effectiveInput, effective);
125
+ break;
126
+ }
127
+ return result;
128
+ }), input.signal);
129
+ const target = (input) => {
130
+ if (!input ||
131
+ typeof input !== 'object' ||
132
+ Array.isArray(input) ||
133
+ typeof input.agentId !== 'string' ||
134
+ !input.agentId)
135
+ throw invalid('Agent target is invalid.');
136
+ return { ...input };
137
+ };
138
+ return {
139
+ spawn: (input) => invoke('agent.spawn', { ...input }),
140
+ list: async (input = {}) => {
141
+ if (!input || typeof input !== 'object' || Array.isArray(input))
142
+ throw invalid('Agent list input is invalid.');
143
+ return run(async (call) => structuredClone(await backend.list(call)), input.signal);
144
+ },
145
+ inspect: async (input) => {
146
+ const copied = target(input);
147
+ return run(async (call) => structuredClone(await backend.inspect(copied, call)), copied.signal);
148
+ },
149
+ wait: async (input) => {
150
+ if (!input || typeof input !== 'object' || Array.isArray(input))
151
+ throw invalid('Agent wait input is invalid.');
152
+ return invoke('agent.wait', input.agentId === undefined ? { ...input } : target(input));
153
+ },
154
+ message: async (input) => invoke('agent.message', target(input)),
155
+ cancel: async (input) => invoke('agent.cancel', target(input))
156
+ };
157
+ }
158
+ function invalid(message) {
159
+ return new MarAgentError('MAR_AGENT_EXTENSION_AGENT_INPUT_INVALID', message);
160
+ }
@@ -0,0 +1,260 @@
1
+ import type { CoreModelRequest } from './model-request.js';
2
+ import type { CorePromptInput, CorePromptResult } from './prompt-input.js';
3
+ import type { CoreModelEvent } from './model-stream.js';
4
+ import type { CorePromptComposition, CorePromptSectionInput } from './prompt-composition.js';
5
+ import type { CoreModelsApi } from './models.js';
6
+ import type { CoreAgentsApi, CoreAgentSpawnInput, CoreAgentWaitInput, CoreAgentMessageInput, CoreAgentTarget, CoreAgentSnapshot, CoreAgentSpawnResult, CoreAgentCompletion } from './agents.js';
7
+ import type { CoreDataApi } from './data.js';
8
+ import type { CoreMcpApi, CoreMcpConnectInput, CoreMcpConnectionInput, CoreMcpConnectionRef, CoreMcpCatalog, CoreMcpCompletion } from './mcp.js';
9
+ import type { CoreFilesApi, CoreFileOperations, CoreFileCompletion } from './files.js';
10
+ import type { CoreEnvironmentApi, CoreEnvironmentKey, CoreEnvironmentSet, CoreEnvironmentChange, CoreEnvironmentCompletion } from './environment.js';
11
+ import type { CoreSettingsApi, CoreSettingsReadInput, CoreSettingsUpdate, CoreSettingsDescription, CoreSettingsSnapshot, CoreSettingsResult, CoreSettingsCompletion } from './settings.js';
12
+ import type { CoreProcessesApi, CoreProcessInput, CoreProcessRunInput, CoreProcessHandle, CoreProcessResult, CoreProcessCompletion } from './processes.js';
13
+ import type { CoreNetworkApi, CoreHttpRequest, CoreHttpResponse, CoreHttpCompletion } from './network.js';
14
+ import type { CoreSessionApi, CoreAppendInput, CoreCompactInput, CoreSessionActionResult } from './session-input.js';
15
+ import type { ToolCall, ToolExecutionResult } from '../tools/contracts.js';
16
+ import type { CoreHookOptions, CoreInterceptor } from './hooks.js';
17
+ import type { CoreOperationContext } from './operation.js';
18
+ import type { CoreDisposer, CoreResourceRegistration, CoreResourceScope } from './resources.js';
19
+ import type { CoreServiceRegistry } from './services.js';
20
+ import type { CoreToolRef, CoreToolInvocation, CoreToolCheck, CoreToolSpec, CoreToolCompletion, CoreToolFailure } from './tools.js';
21
+ export interface CoreDomainOperations extends CoreFileOperations {
22
+ 'mcp.connect': {
23
+ input: CoreMcpConnectInput;
24
+ output: CoreMcpConnectionRef;
25
+ };
26
+ 'mcp.catalog': {
27
+ input: CoreMcpConnectionInput;
28
+ output: CoreMcpCatalog;
29
+ };
30
+ 'mcp.disconnect': {
31
+ input: CoreMcpConnectionInput;
32
+ output: void;
33
+ };
34
+ 'agent.spawn': {
35
+ input: CoreAgentSpawnInput;
36
+ output: CoreAgentSpawnResult;
37
+ };
38
+ 'agent.wait': {
39
+ input: CoreAgentWaitInput;
40
+ output: CoreAgentSnapshot;
41
+ };
42
+ 'agent.message': {
43
+ input: CoreAgentMessageInput;
44
+ output: CoreAgentSnapshot;
45
+ };
46
+ 'agent.cancel': {
47
+ input: CoreAgentTarget;
48
+ output: CoreAgentSnapshot;
49
+ };
50
+ 'environment.get': {
51
+ input: CoreEnvironmentKey;
52
+ output: string | undefined;
53
+ };
54
+ 'environment.set': {
55
+ input: CoreEnvironmentSet;
56
+ output: CoreEnvironmentChange;
57
+ };
58
+ 'settings.describe': {
59
+ input: CoreSettingsReadInput;
60
+ output: CoreSettingsDescription;
61
+ };
62
+ 'settings.read': {
63
+ input: CoreSettingsReadInput;
64
+ output: CoreSettingsSnapshot;
65
+ };
66
+ 'settings.update': {
67
+ input: CoreSettingsUpdate;
68
+ output: CoreSettingsResult;
69
+ };
70
+ 'process.spawn': {
71
+ input: CoreProcessInput;
72
+ output: CoreProcessHandle;
73
+ };
74
+ 'process.run': {
75
+ input: CoreProcessRunInput;
76
+ output: CoreProcessResult;
77
+ };
78
+ 'network.fetch': {
79
+ input: CoreHttpRequest;
80
+ output: CoreHttpResponse;
81
+ };
82
+ 'prompt.compose': {
83
+ input: string;
84
+ output: string;
85
+ };
86
+ 'prompt.submit': {
87
+ input: CorePromptInput;
88
+ output: CorePromptResult;
89
+ };
90
+ 'prompt.sections': {
91
+ input: CorePromptComposition;
92
+ output: CorePromptComposition;
93
+ };
94
+ 'prompt.section': {
95
+ input: CorePromptSectionInput;
96
+ output: string | null;
97
+ };
98
+ 'model.request': {
99
+ input: CoreModelRequest;
100
+ output: CoreModelRequest;
101
+ };
102
+ 'model.event': {
103
+ input: CoreModelEvent;
104
+ output: CoreModelEvent | undefined;
105
+ };
106
+ 'tool.invoke': {
107
+ input: ToolCall;
108
+ output: ToolExecutionResult;
109
+ };
110
+ 'session.append': {
111
+ input: CoreAppendInput;
112
+ output: CoreSessionActionResult;
113
+ };
114
+ 'session.compact': {
115
+ input: CoreCompactInput;
116
+ output: CoreSessionActionResult;
117
+ };
118
+ }
119
+ export interface CoreDomainFacts {
120
+ 'session.ending': {
121
+ readonly sessionId: string;
122
+ readonly reason: 'clear' | 'other';
123
+ readonly resumable: boolean;
124
+ };
125
+ 'session.ready': {
126
+ readonly sessionId: string;
127
+ };
128
+ 'model.event': CoreModelEvent;
129
+ 'mcp.completed': CoreMcpCompletion;
130
+ 'agent.completed': CoreAgentCompletion;
131
+ 'environment.completed': CoreEnvironmentCompletion;
132
+ 'settings.completed': CoreSettingsCompletion;
133
+ 'process.completed': CoreProcessCompletion;
134
+ 'network.completed': CoreHttpCompletion;
135
+ 'files.completed': CoreFileCompletion;
136
+ 'tool.completed': CoreToolCompletion;
137
+ 'tool.failed': CoreToolFailure;
138
+ 'tool.attempt.completed': CoreToolCompletion;
139
+ 'tool.attempt.failed': CoreToolFailure;
140
+ }
141
+ export interface CoreHookRegistrar {
142
+ intercept<Key extends keyof CoreDomainOperations>(key: Key, handler: CoreInterceptor<CoreDomainOperations[Key]['input'], CoreDomainOperations[Key]['output']>, options?: CoreHookOptions): CoreResourceRegistration;
143
+ observe<Key extends keyof CoreDomainFacts>(key: Key, handler: (fact: CoreDomainFacts[Key], call: CoreOperationContext) => void | Promise<void>, options?: CoreHookOptions): CoreResourceRegistration;
144
+ }
145
+ export interface CoreCapabilitySnapshot {
146
+ has(id: string): boolean;
147
+ list(): readonly string[];
148
+ }
149
+ export interface CoreCommandSpec {
150
+ readonly name: string;
151
+ readonly description?: string;
152
+ readonly override?: 'replace';
153
+ handler(args: unknown, call: CoreOperationContext): unknown | Promise<unknown>;
154
+ }
155
+ export interface CorePromptSection {
156
+ readonly id: string;
157
+ readonly text: string;
158
+ }
159
+ export interface CoreAgentExtensionContext {
160
+ readonly data?: Pick<CoreDataApi, 'store'>;
161
+ readonly resources: CoreResourceScope;
162
+ readonly services: CoreServiceRegistry;
163
+ readonly capabilities: CoreCapabilitySnapshot;
164
+ readonly bindings: Readonly<Record<string, unknown>>;
165
+ readonly observability: {
166
+ log(message: string): void;
167
+ };
168
+ }
169
+ export interface CoreSessionExtensionContext extends CoreAgentExtensionContext {
170
+ readonly mcp?: CoreMcpApi;
171
+ readonly agents?: CoreAgentsApi;
172
+ readonly environment?: CoreEnvironmentApi;
173
+ readonly settings?: CoreSettingsApi;
174
+ readonly processes?: CoreProcessesApi;
175
+ readonly network?: CoreNetworkApi;
176
+ readonly files?: CoreFilesApi;
177
+ readonly sessionId: string;
178
+ readonly models?: CoreModelsApi;
179
+ readonly data: CoreDataApi;
180
+ readonly session?: CoreSessionApi;
181
+ readonly hooks: CoreHookRegistrar;
182
+ readonly tools: {
183
+ register(spec: CoreToolSpec): CoreResourceRegistration;
184
+ catalog(): readonly CoreToolRef[];
185
+ check(call: CoreToolInvocation): Promise<CoreToolCheck>;
186
+ invoke(call: CoreToolInvocation): Promise<ToolExecutionResult>;
187
+ };
188
+ readonly commands: {
189
+ register(spec: CoreCommandSpec): CoreResourceRegistration;
190
+ list(): readonly Pick<CoreCommandSpec, 'name' | 'description'>[];
191
+ run(input: {
192
+ readonly name: string;
193
+ readonly args?: unknown;
194
+ }): Promise<unknown>;
195
+ };
196
+ readonly prompts: {
197
+ register(section: CorePromptSection): CoreResourceRegistration;
198
+ sections(): readonly CorePromptSection[];
199
+ compose(system: string): Promise<string>;
200
+ composeSections(input: CorePromptComposition): Promise<CorePromptComposition>;
201
+ composeDefault(facts?: import('./prompt-composition.js').CorePromptFacts): Promise<CorePromptComposition>;
202
+ };
203
+ readonly clock: {
204
+ now(): number;
205
+ sleep(input: {
206
+ readonly durationMs: number;
207
+ readonly signal?: AbortSignal;
208
+ }): Promise<void>;
209
+ after(durationMs: number, callback: (call: CoreOperationContext) => void | Promise<void>): CoreResourceRegistration;
210
+ every(durationMs: number, callback: (call: CoreOperationContext) => void | Promise<void>): CoreResourceRegistration;
211
+ };
212
+ }
213
+ export interface CoreServiceDeclaration {
214
+ readonly key: {
215
+ readonly id: string;
216
+ readonly scope: 'agent' | 'session';
217
+ };
218
+ readonly intent?: 'provide' | 'replace' | 'wrap';
219
+ }
220
+ export interface CoreServiceDependency {
221
+ readonly key: {
222
+ readonly id: string;
223
+ readonly scope: 'agent' | 'session';
224
+ };
225
+ readonly optional?: boolean;
226
+ }
227
+ export interface CoreRuntimeExtension {
228
+ readonly dataDirectory?: string;
229
+ readonly id: string;
230
+ readonly apiVersion: '1.0';
231
+ readonly bindings?: Readonly<Record<string, unknown>>;
232
+ readonly requires?: readonly {
233
+ readonly id: string;
234
+ }[];
235
+ readonly optional?: readonly {
236
+ readonly id: string;
237
+ }[];
238
+ readonly provides?: readonly CoreServiceDeclaration[];
239
+ readonly serviceDependencies?: readonly CoreServiceDependency[];
240
+ readonly failurePolicy?: 'disable-extension' | 'fail-composition';
241
+ setup?(context: CoreAgentExtensionContext): void | CoreDisposer | Promise<void | CoreDisposer>;
242
+ attachSession?(context: CoreSessionExtensionContext): void | CoreDisposer | Promise<void | CoreDisposer>;
243
+ }
244
+ export interface CoreExtensionSnapshot {
245
+ readonly revision: string;
246
+ readonly extensions: readonly CoreRuntimeExtension[];
247
+ }
248
+ export interface CoreExtensionSource {
249
+ resolve(input: {
250
+ readonly signal: AbortSignal;
251
+ }): Promise<CoreExtensionSnapshot>;
252
+ }
253
+ export interface CoreExtensionDiagnostic {
254
+ readonly code: string;
255
+ readonly extensionId?: string;
256
+ readonly sessionId?: string;
257
+ readonly revision?: string;
258
+ readonly operationId?: string;
259
+ readonly message?: string;
260
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,84 @@
1
+ import type { CoreCallbackRunner, CoreOperationContext } from './operation.js';
2
+ import type { CoreResourceRegistration } from './resources.js';
3
+ import { ExtensionResourceScope } from './resources.js';
4
+ export type CoreJsonValue = null | boolean | number | string | readonly CoreJsonValue[] | {
5
+ readonly [key: string]: CoreJsonValue;
6
+ };
7
+ export interface CoreCheckpoint {
8
+ readonly revision: number;
9
+ readonly value?: CoreJsonValue;
10
+ }
11
+ export interface CoreCheckpointApi {
12
+ get(input: {
13
+ readonly key: string;
14
+ }): Promise<CoreCheckpoint>;
15
+ set(input: {
16
+ readonly key: string;
17
+ readonly value: CoreJsonValue;
18
+ readonly expectedRevision?: number;
19
+ }): Promise<CoreCheckpoint>;
20
+ delete(input: {
21
+ readonly key: string;
22
+ readonly expectedRevision?: number;
23
+ }): Promise<CoreCheckpoint>;
24
+ }
25
+ export interface CoreCheckpointBackend {
26
+ read(extensionId: string, key: string, call: CoreOperationContext): Promise<CoreCheckpoint>;
27
+ write(extensionId: string, key: string, value: CoreJsonValue | undefined, expectedRevision: number | undefined, call: CoreOperationContext): Promise<CoreCheckpoint>;
28
+ }
29
+ export interface CoreStateChange {
30
+ readonly key: string;
31
+ readonly revision: number;
32
+ readonly value?: CoreJsonValue;
33
+ }
34
+ export interface CoreStateApi {
35
+ read(key: string): Promise<CoreJsonValue | undefined>;
36
+ write(key: string, value: CoreJsonValue): Promise<number>;
37
+ delete(key: string): Promise<number>;
38
+ watch(handler: (change: CoreStateChange, call: CoreOperationContext) => void | Promise<void>): CoreResourceRegistration;
39
+ }
40
+ export interface CoreDataApi {
41
+ readonly store?: CoreStoreApi;
42
+ readonly state: CoreStateApi;
43
+ readonly checkpoint?: CoreCheckpointApi;
44
+ }
45
+ export interface CoreStoreCommit {
46
+ readonly expectedRevision?: number;
47
+ readonly writes: readonly {
48
+ readonly key: string;
49
+ readonly value?: CoreJsonValue;
50
+ }[];
51
+ }
52
+ export interface CoreStoreApi {
53
+ get(input: {
54
+ readonly key: string;
55
+ }): Promise<CoreCheckpoint>;
56
+ keys(): Promise<{
57
+ readonly revision: number;
58
+ readonly keys: readonly string[];
59
+ }>;
60
+ set(input: {
61
+ readonly key: string;
62
+ readonly value: CoreJsonValue;
63
+ readonly expectedRevision?: number;
64
+ }): Promise<CoreCheckpoint>;
65
+ delete(input: {
66
+ readonly key: string;
67
+ readonly expectedRevision?: number;
68
+ }): Promise<CoreCheckpoint>;
69
+ commit(input: CoreStoreCommit): Promise<{
70
+ readonly revision: number;
71
+ }>;
72
+ }
73
+ export declare function cloneCoreJson(value: unknown): CoreJsonValue;
74
+ /** Explicit reactive state, not a proxy over the Session or a second persistence mechanism. */
75
+ export declare class ExtensionState {
76
+ #private;
77
+ readonly scope: ExtensionResourceScope;
78
+ readonly diagnose: () => void;
79
+ readonly callback: CoreCallbackRunner;
80
+ constructor(scope: ExtensionResourceScope, diagnose: () => void, callback?: CoreCallbackRunner);
81
+ read(key: string): CoreJsonValue | undefined;
82
+ write(key: string, value: CoreJsonValue | undefined, call: CoreOperationContext): Promise<number>;
83
+ watch(handler: (change: CoreStateChange, call: CoreOperationContext) => void | Promise<void>): CoreResourceRegistration;
84
+ }
@@ -0,0 +1,83 @@
1
+ import { MarAgentError } from '../error.js';
2
+ export function cloneCoreJson(value) {
3
+ try {
4
+ const ancestors = new Set();
5
+ const copy = (value) => {
6
+ if (value === null || typeof value === 'string' || typeof value === 'boolean')
7
+ return value;
8
+ if (typeof value === 'number' && Number.isFinite(value))
9
+ return value;
10
+ if (typeof value !== 'object' || !value || ancestors.has(value))
11
+ throw new Error();
12
+ ancestors.add(value);
13
+ try {
14
+ if (Array.isArray(value))
15
+ return Array.from(value, copy);
16
+ if (Object.prototype.toString.call(value) !== '[object Object]')
17
+ throw new Error();
18
+ // Define own JSON keys, including __proto__, without assigning to a prototype setter.
19
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, copy(item)]));
20
+ }
21
+ finally {
22
+ ancestors.delete(value);
23
+ }
24
+ };
25
+ return copy(value);
26
+ }
27
+ catch {
28
+ throw new MarAgentError('MAR_AGENT_EXTENSION_DATA_INVALID', 'Extension data must be serializable JSON.');
29
+ }
30
+ }
31
+ /** Explicit reactive state, not a proxy over the Session or a second persistence mechanism. */
32
+ export class ExtensionState {
33
+ scope;
34
+ diagnose;
35
+ callback;
36
+ #values = new Map();
37
+ #observers = [];
38
+ #revision = 0;
39
+ constructor(scope, diagnose, callback = async (call, run) => run(call)) {
40
+ this.scope = scope;
41
+ this.diagnose = diagnose;
42
+ this.callback = callback;
43
+ }
44
+ read(key) {
45
+ const value = this.#values.get(key);
46
+ return value === undefined ? undefined : cloneCoreJson(value);
47
+ }
48
+ async write(key, value, call) {
49
+ if (typeof key !== 'string')
50
+ throw new MarAgentError('MAR_AGENT_EXTENSION_DATA_INVALID', 'State key must be a string.');
51
+ const committed = value === undefined ? undefined : cloneCoreJson(value);
52
+ if (committed === undefined)
53
+ this.#values.delete(key);
54
+ else
55
+ this.#values.set(key, committed);
56
+ const revision = ++this.#revision;
57
+ const change = {
58
+ key,
59
+ revision,
60
+ ...(committed !== undefined ? { value: cloneCoreJson(committed) } : {})
61
+ };
62
+ if (this.scope.state === 'ACTIVE')
63
+ for (const observer of [...this.#observers]) {
64
+ try {
65
+ await this.scope.run((signal) => this.callback(Object.freeze({ ...call, signal }), (effective) => observer(structuredClone(change), effective)), call.signal);
66
+ }
67
+ catch {
68
+ call.signal.throwIfAborted();
69
+ this.diagnose();
70
+ }
71
+ }
72
+ return revision;
73
+ }
74
+ watch(handler) {
75
+ const registration = this.scope.effect(() => () => {
76
+ const index = this.#observers.indexOf(handler);
77
+ if (index !== -1)
78
+ this.#observers.splice(index, 1);
79
+ });
80
+ this.#observers.push(handler);
81
+ return registration;
82
+ }
83
+ }