@myagentroam/agent 0.9.141 → 0.9.142
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/agent-core/host/contracts.d.ts +64 -0
- package/dist/{host → agent-core/host}/local-agent-host.d.ts +6 -5
- package/dist/{host → agent-core/host}/local-agent-host.js +4 -4
- package/dist/{model → agent-core/model}/contracts.d.ts +2 -16
- package/dist/agent-core/model/recovery.d.ts +4 -0
- package/dist/agent-core/model/recovery.js +71 -0
- package/dist/{prompts → agent-core/prompts}/core.d.ts +3 -13
- package/dist/{prompts → agent-core/prompts}/index.d.ts +1 -1
- package/dist/{prompts → agent-core/prompts}/modes.d.ts +1 -1
- package/dist/{prompts → agent-core/prompts}/resources.d.ts +1 -1
- package/dist/{runtime/compact.d.ts → agent-core/runtime/compaction/history.d.ts} +2 -23
- package/dist/{runtime/compact.js → agent-core/runtime/compaction/history.js} +2 -92
- package/dist/agent-core/runtime/compaction/operation.d.ts +22 -0
- package/dist/agent-core/runtime/compaction/operation.js +91 -0
- package/dist/agent-core/runtime/context/contracts.d.ts +12 -0
- package/dist/{runtime/environment-context.d.ts → agent-core/runtime/context/environment.d.ts} +2 -2
- package/dist/{runtime/environment-context.js → agent-core/runtime/context/environment.js} +1 -1
- package/dist/{runtime/context-gc-runtime.d.ts → agent-core/runtime/context/gc-runtime.d.ts} +4 -4
- package/dist/{runtime/context-gc-runtime.js → agent-core/runtime/context/gc-runtime.js} +3 -3
- package/dist/{runtime/context-gc-tokens.d.ts → agent-core/runtime/context/gc-tokens.d.ts} +2 -2
- package/dist/{runtime/context-gc-tokens.js → agent-core/runtime/context/gc-tokens.js} +3 -3
- package/dist/{runtime/context-gc.d.ts → agent-core/runtime/context/gc.d.ts} +3 -3
- package/dist/{runtime/context-gc.js → agent-core/runtime/context/gc.js} +6 -6
- package/dist/{runtime → agent-core/runtime/context}/instructions.d.ts +1 -1
- package/dist/{runtime → agent-core/runtime/context}/instructions.js +1 -1
- package/dist/{runtime → agent-core/runtime/context}/normalize.d.ts +1 -1
- package/dist/{runtime/context-projection.d.ts → agent-core/runtime/context/projection.d.ts} +3 -3
- package/dist/{runtime/context-projection.js → agent-core/runtime/context/projection.js} +4 -4
- package/dist/agent-core/runtime/context/request-budget.d.ts +6 -0
- package/dist/agent-core/runtime/context/request-budget.js +22 -0
- package/dist/agent-core/runtime/context/restore.d.ts +15 -0
- package/dist/agent-core/runtime/context/restore.js +85 -0
- package/dist/agent-core/runtime/context/usage.d.ts +18 -0
- package/dist/agent-core/runtime/context/usage.js +138 -0
- package/dist/agent-core/runtime/context/world-state.d.ts +35 -0
- package/dist/agent-core/runtime/context/world-state.js +133 -0
- package/dist/agent-core/runtime/contracts.d.ts +37 -0
- package/dist/agent-core/runtime/event-projection.d.ts +5 -0
- package/dist/agent-core/runtime/event-projection.js +17 -0
- package/dist/agent-core/runtime/events.d.ts +142 -0
- package/dist/agent-core/runtime/execution.d.ts +50 -0
- package/dist/agent-core/runtime/execution.js +393 -0
- package/dist/{runtime → agent-core/runtime}/rollout-budget.d.ts +1 -1
- package/dist/agent-core/sdk/agent.js +209 -0
- package/dist/agent-core/sdk/model-runtime.d.ts +13 -0
- package/dist/agent-core/sdk/model-runtime.js +63 -0
- package/dist/agent-core/sdk/session-title.d.ts +3 -0
- package/dist/agent-core/sdk/session-title.js +42 -0
- package/dist/agent-core/sdk/session.d.ts +62 -0
- package/dist/agent-core/sdk/session.js +1147 -0
- package/dist/agent-core/sdk/subagent-backend.d.ts +3 -0
- package/dist/agent-core/sdk/subagent-backend.js +46 -0
- package/dist/agent-core/sdk/types.d.ts +56 -0
- package/dist/{session → agent-core/session}/catalog.d.ts +2 -1
- package/dist/{session → agent-core/session}/context-gc-checkpoint.js +1 -1
- package/dist/{session → agent-core/session}/jsonl-store.d.ts +2 -2
- package/dist/{session → agent-core/session}/jsonl-store.js +1 -1
- package/dist/agent-core/subagent/contracts.d.ts +91 -0
- package/dist/agent-core/subagent/contracts.js +1 -0
- package/dist/agent-core/subagent/scheduler.d.ts +117 -0
- package/dist/{subagent → agent-core/subagent}/session-controller.d.ts +3 -2
- package/dist/agent-core/tools/agent/agent-cancel.d.ts +7 -0
- package/dist/{tools → agent-core/tools/agent}/agent-cancel.js +1 -1
- package/dist/agent-core/tools/agent/agent-message.d.ts +7 -0
- package/dist/{tools → agent-core/tools/agent}/agent-message.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/agent-start.d.ts +3 -3
- package/dist/{tools → agent-core/tools/agent}/agent-start.js +3 -3
- package/dist/agent-core/tools/agent/agent-wait.d.ts +7 -0
- package/dist/{tools → agent-core/tools/agent}/agent-wait.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/question.d.ts +2 -2
- package/dist/{tools → agent-core/tools/agent}/question.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/skill.d.ts +2 -2
- package/dist/{tools → agent-core/tools/agent}/skill.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/todo-read.d.ts +2 -2
- package/dist/{tools → agent-core/tools/agent}/todo-write.d.ts +2 -2
- package/dist/{tools → agent-core/tools/agent}/todo-write.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/todo.d.ts +2 -9
- package/dist/{tools → agent-core/tools/agent}/todo.js +1 -1
- package/dist/{tools → agent-core/tools}/code-mode/cells.d.ts +1 -1
- package/dist/{tools → agent-core/tools}/code-mode/dispatch.d.ts +2 -4
- package/dist/{tools → agent-core/tools}/code-mode/dispatch.js +5 -4
- package/dist/{tools → agent-core/tools}/code-mode/engine.d.ts +1 -1
- package/dist/agent-core/tools/code-mode/engine.js +1 -0
- package/dist/{tools/code-mode.d.ts → agent-core/tools/code-mode/index.d.ts} +7 -7
- package/dist/{tools/code-mode.js → agent-core/tools/code-mode/index.js} +14 -15
- package/dist/{tools → agent-core/tools}/code-mode/state.d.ts +2 -13
- package/dist/agent-core/tools/code-mode/worker.d.ts +1 -0
- package/dist/agent-core/tools/contracts.d.ts +168 -0
- package/dist/agent-core/tools/contracts.js +1 -0
- package/dist/agent-core/tools/execution.d.ts +34 -0
- package/dist/agent-core/tools/execution.js +102 -0
- package/dist/agent-core/tools/local/apply-patch.d.ts +19 -0
- package/dist/{tools → agent-core/tools/local}/apply-patch.js +8 -2
- package/dist/{tools → agent-core/tools/local}/exec.d.ts +5 -17
- package/dist/{tools → agent-core/tools/local}/exec.js +4 -4
- package/dist/{tools → agent-core/tools/local}/read.d.ts +7 -15
- package/dist/{tools → agent-core/tools/local}/read.js +185 -2
- package/dist/{tools/local-registry.d.ts → agent-core/tools/local/registry.d.ts} +5 -5
- package/dist/agent-core/tools/local/registry.js +101 -0
- package/dist/{tools → agent-core/tools/local}/view-image.d.ts +4 -9
- package/dist/{tools → agent-core/tools/local}/view-image.js +2 -2
- package/dist/agent-core/tools/local/web-fetch.d.ts +4 -0
- package/dist/agent-core/tools/local/web-fetch.js +85 -0
- package/dist/agent-core/tools/local/web-search.d.ts +7 -0
- package/dist/agent-core/tools/local/web-search.js +34 -0
- package/dist/{tools/local-workspace-tools.js → agent-core/tools/local/workspace-tools.js} +1 -1
- package/dist/{sdk/mcp-catalog.d.ts → agent-core/tools/mcp/catalog.d.ts} +1 -2
- package/dist/{sdk/mcp-catalog.js → agent-core/tools/mcp/catalog.js} +1 -1
- package/dist/agent-core/tools/mcp/history.d.ts +4 -0
- package/dist/agent-core/tools/mcp/history.js +33 -0
- package/dist/{tools → agent-core/tools/mcp}/tool-search.d.ts +3 -3
- package/dist/{tools → agent-core/tools/mcp}/tool-search.js +4 -4
- package/dist/agent-core/tools/output-projection.d.ts +14 -0
- package/dist/agent-core/tools/output-projection.js +189 -0
- package/dist/{tools → agent-core/tools}/registry.d.ts +1 -1
- package/dist/agent-core/tools/registry.js +47 -0
- package/dist/{tools → agent-core/tools}/runtime-context.d.ts +8 -7
- package/dist/agent-core/tools/runtime-context.js +1 -0
- package/dist/{tools → agent-core/tools}/runtime.d.ts +1 -1
- package/dist/cli/codex-auth-file.d.ts +1 -1
- package/dist/cli/codex-auth-file.js +2 -2
- package/dist/cli/main.js +9 -9
- package/dist/index.d.ts +17 -15
- package/dist/index.js +8 -8
- package/dist/sandbox/linux/bubblewrap-sandbox-executor.d.ts +1 -1
- package/dist/sandbox/linux/bubblewrap-sandbox-executor.js +1 -1
- package/package.json +3 -3
- package/dist/host/contracts.d.ts +0 -21
- package/dist/sdk/agent.js +0 -2548
- package/dist/sdk/types.d.ts +0 -349
- package/dist/subagent/scheduler.d.ts +0 -206
- package/dist/tools/agent-cancel.d.ts +0 -7
- package/dist/tools/agent-message.d.ts +0 -7
- package/dist/tools/agent-wait.d.ts +0 -7
- package/dist/tools/apply-patch.d.ts +0 -32
- package/dist/tools/local-registry.js +0 -373
- package/dist/tools/registry.js +0 -47
- package/dist/tools/web-fetch.d.ts +0 -2
- package/dist/tools/web-fetch.js +0 -12
- package/dist/tools/web-search.d.ts +0 -2
- package/dist/tools/web-search.js +0 -15
- package/dist/{error.d.ts → agent-core/error.d.ts} +0 -0
- package/dist/{error.js → agent-core/error.js} +0 -0
- package/dist/{host → agent-core/host}/contracts.js +0 -0
- package/dist/{host → agent-core/host}/local-process-executor.d.ts +0 -0
- package/dist/{host → agent-core/host}/local-process-executor.js +0 -0
- package/dist/{host → agent-core/host}/network.d.ts +0 -0
- package/dist/{host → agent-core/host}/network.js +0 -0
- package/dist/{model → agent-core/model}/anthropic-messages.d.ts +0 -0
- package/dist/{model → agent-core/model}/anthropic-messages.js +0 -0
- package/dist/{model → agent-core/model}/anthropic-thinking.d.ts +0 -0
- package/dist/{model → agent-core/model}/anthropic-thinking.js +0 -0
- package/dist/{model → agent-core/model}/catalog-file.d.ts +0 -0
- package/dist/{model → agent-core/model}/catalog-file.js +0 -0
- package/dist/{model → agent-core/model}/configuration.d.ts +0 -0
- package/dist/{model → agent-core/model}/configuration.js +0 -0
- package/dist/{model → agent-core/model}/contracts.js +0 -0
- package/dist/{model → agent-core/model}/http.d.ts +0 -0
- package/dist/{model → agent-core/model}/http.js +0 -0
- package/dist/{model → agent-core/model}/openai-responses.d.ts +0 -0
- package/dist/{model → agent-core/model}/openai-responses.js +0 -0
- package/dist/{model → agent-core/model}/runtime-credential.d.ts +0 -0
- package/dist/{model → agent-core/model}/runtime-credential.js +0 -0
- package/dist/{model → agent-core/model}/sse.d.ts +0 -0
- package/dist/{model → agent-core/model}/sse.js +0 -0
- package/dist/{model → agent-core/model}/transport-test.d.ts +0 -0
- package/dist/{model → agent-core/model}/transport-test.js +0 -0
- package/dist/{prompts → agent-core/prompts}/compact.d.ts +0 -0
- package/dist/{prompts → agent-core/prompts}/compact.js +0 -0
- package/dist/{prompts → agent-core/prompts}/core.js +0 -0
- package/dist/{prompts → agent-core/prompts}/execution-budget.d.ts +0 -0
- package/dist/{prompts → agent-core/prompts}/execution-budget.js +0 -0
- package/dist/{prompts → agent-core/prompts}/index.js +0 -0
- package/dist/{prompts → agent-core/prompts}/modes.js +0 -0
- package/dist/{prompts → agent-core/prompts}/output.d.ts +0 -0
- package/dist/{prompts → agent-core/prompts}/output.js +0 -0
- package/dist/{prompts → agent-core/prompts}/resources.js +0 -0
- package/dist/{prompts → agent-core/prompts}/subagent.d.ts +0 -0
- package/dist/{prompts → agent-core/prompts}/subagent.js +0 -0
- package/dist/{prompts → agent-core/prompts}/title.d.ts +0 -0
- package/dist/{prompts → agent-core/prompts}/title.js +0 -0
- package/dist/{prompts → agent-core/prompts}/workflow.d.ts +0 -0
- package/dist/{prompts → agent-core/prompts}/workflow.js +0 -0
- package/dist/{runtime → agent-core/runtime}/async-queue.d.ts +0 -0
- package/dist/{runtime → agent-core/runtime}/async-queue.js +0 -0
- package/dist/{sdk/types.js → agent-core/runtime/context/contracts.js} +0 -0
- package/dist/{runtime → agent-core/runtime/context}/normalize.js +0 -0
- package/dist/{session/types.js → agent-core/runtime/contracts.js} +0 -0
- package/dist/{tools/code-mode/engine.js → agent-core/runtime/events.js} +0 -0
- package/dist/{runtime → agent-core/runtime}/execution-policy.d.ts +0 -0
- package/dist/{runtime → agent-core/runtime}/execution-policy.js +0 -0
- package/dist/{runtime → agent-core/runtime}/plan-output.d.ts +0 -0
- package/dist/{runtime → agent-core/runtime}/plan-output.js +0 -0
- package/dist/{runtime → agent-core/runtime}/rollout-budget.js +0 -0
- package/dist/{runtime → agent-core/runtime}/token-budget.d.ts +0 -0
- package/dist/{runtime → agent-core/runtime}/token-budget.js +0 -0
- package/dist/{sdk → agent-core/sdk}/agent.d.ts +0 -0
- package/dist/{tools/code-mode/worker.d.ts → agent-core/sdk/types.js} +0 -0
- package/dist/{session → agent-core/session}/catalog.js +0 -0
- package/dist/{session → agent-core/session}/context-gc-checkpoint.d.ts +0 -0
- package/dist/{session → agent-core/session}/context-gc-replacements.d.ts +0 -0
- package/dist/{session → agent-core/session}/context-gc-replacements.js +0 -0
- package/dist/{session → agent-core/session}/context-gc-subagent.d.ts +0 -0
- package/dist/{session → agent-core/session}/context-gc-subagent.js +0 -0
- package/dist/{session → agent-core/session}/transaction-recovery.d.ts +0 -0
- package/dist/{session → agent-core/session}/transaction-recovery.js +0 -0
- package/dist/{session → agent-core/session}/types.d.ts +0 -0
- package/dist/{tools/runtime-context.js → agent-core/session/types.js} +0 -0
- package/dist/{subagent → agent-core/subagent}/scheduler.js +0 -0
- package/dist/{subagent → agent-core/subagent}/session-controller.js +0 -0
- package/dist/{tools → agent-core/tools/agent}/todo-read.js +0 -0
- package/dist/{tools → agent-core/tools}/callers.d.ts +0 -0
- package/dist/{tools → agent-core/tools}/callers.js +0 -0
- package/dist/{tools → agent-core/tools}/code-mode/cells.js +1 -1
- /package/dist/{tools → agent-core/tools}/code-mode/quickjs-engine.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/quickjs-engine.js +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/state.js +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/text-output.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/text-output.js +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/worker.js +0 -0
- /package/dist/{tools → agent-core/tools}/execution-limits.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/execution-limits.js +0 -0
- /package/dist/{tools → agent-core/tools/local}/read-context.d.ts +0 -0
- /package/dist/{tools → agent-core/tools/local}/read-context.js +0 -0
- /package/dist/{tools/local-workspace-tools.d.ts → agent-core/tools/local/workspace-tools.d.ts} +0 -0
- /package/dist/{tools → agent-core/tools}/runtime.js +0 -0
- /package/dist/{tools → agent-core/tools}/scheduler.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/scheduler.js +0 -0
- /package/dist/{tools → agent-core/tools}/shared/path-resolver.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/shared/path-resolver.js +0 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { addModelCredentialProviders, closeModelAdapters, resolveAgentModels } from './model-runtime.js';
|
|
2
|
+
import { createSessionOpener } from './session.js';
|
|
3
|
+
import { MarAgentError } from '../error.js';
|
|
4
|
+
import { negotiateHost } from '../host/contracts.js';
|
|
5
|
+
import { AnthropicMessagesAdapter } from '../model/anthropic-messages.js';
|
|
6
|
+
import { OpenAiResponsesAdapter } from '../model/openai-responses.js';
|
|
7
|
+
import { resolveRolloutBudgetOptions } from '../runtime/rollout-budget.js';
|
|
8
|
+
import { JsonlSessionStore } from '../session/jsonl-store.js';
|
|
9
|
+
/** Validate models before Host negotiation and expose only built-in protocol adapters. */
|
|
10
|
+
export async function createMarAgent(options) {
|
|
11
|
+
if (Object.hasOwn(options, 'adapterFactory'))
|
|
12
|
+
throw new MarAgentError('MAR_AGENT_MODEL_CONFIG_INVALID', 'Custom model adapters are not supported.');
|
|
13
|
+
return createMarAgentWithAdapter(options);
|
|
14
|
+
}
|
|
15
|
+
/** Package-internal seam for deterministic adapter tests; not a package export. */
|
|
16
|
+
export async function createMarAgentWithAdapter(options) {
|
|
17
|
+
const rolloutBudgetOptions = resolveRolloutBudgetOptions(options.rolloutBudget);
|
|
18
|
+
const resolvedModels = resolveAgentModels(options.models);
|
|
19
|
+
let { models, defaultModelId } = resolvedModels;
|
|
20
|
+
let modelGeneration = 0;
|
|
21
|
+
const { credentialProviders } = resolvedModels;
|
|
22
|
+
const description = negotiateHost(await options.host.describe());
|
|
23
|
+
if (description.mode !== options.mode)
|
|
24
|
+
throw new MarAgentError('MAR_AGENT_HOST_INCOMPATIBLE', 'Host mode does not match Agent mode.');
|
|
25
|
+
const store = await JsonlSessionStore.open(description.homeDirectory);
|
|
26
|
+
const active = new Map();
|
|
27
|
+
const activeExecutionAdapters = new Set();
|
|
28
|
+
const executionResults = new Map();
|
|
29
|
+
const sessionRuntimes = new Map();
|
|
30
|
+
const sessionWriterOwner = {};
|
|
31
|
+
let disposed = false;
|
|
32
|
+
let reconfiguring = false;
|
|
33
|
+
const createAdapter = (modelId, configuredModels = models) => {
|
|
34
|
+
const configuration = configuredModels.get(modelId);
|
|
35
|
+
if (!configuration)
|
|
36
|
+
throw new MarAgentError('MAR_AGENT_MODEL_DISABLED', 'Selected model is unavailable.');
|
|
37
|
+
return (options.adapterFactory?.(configuration) ??
|
|
38
|
+
(configuration.protocol === 'OPENAI_RESPONSES'
|
|
39
|
+
? new OpenAiResponsesAdapter(configuration)
|
|
40
|
+
: new AnthropicMessagesAdapter(configuration)));
|
|
41
|
+
};
|
|
42
|
+
const activeSessionIds = () => new Set([...active.keys()].map((key) => key.slice(0, key.indexOf(':'))));
|
|
43
|
+
const activeAdaptersExcept = (sessionId) => {
|
|
44
|
+
const activeAdapters = new Set();
|
|
45
|
+
for (const [candidateSessionId, runtime] of sessionRuntimes)
|
|
46
|
+
if (candidateSessionId !== sessionId && activeSessionIds().has(candidateSessionId))
|
|
47
|
+
for (const adapter of runtime.adapters.values())
|
|
48
|
+
activeAdapters.add(adapter);
|
|
49
|
+
return activeAdapters;
|
|
50
|
+
};
|
|
51
|
+
const openSession = createSessionOpener({
|
|
52
|
+
host: options.host,
|
|
53
|
+
description,
|
|
54
|
+
settings: {
|
|
55
|
+
...(options.workspaceMapping === undefined
|
|
56
|
+
? {}
|
|
57
|
+
: { workspaceMapping: options.workspaceMapping }),
|
|
58
|
+
...(options.systemInstruction === undefined
|
|
59
|
+
? {}
|
|
60
|
+
: { systemInstruction: options.systemInstruction })
|
|
61
|
+
},
|
|
62
|
+
store,
|
|
63
|
+
registry: {
|
|
64
|
+
active,
|
|
65
|
+
activeExecutionAdapters,
|
|
66
|
+
executionResults,
|
|
67
|
+
sessionRuntimes,
|
|
68
|
+
sessionWriterOwner
|
|
69
|
+
},
|
|
70
|
+
rolloutBudgetOptions,
|
|
71
|
+
hasCustomAdapters: options.adapterFactory !== undefined,
|
|
72
|
+
modelState: () => ({ models, defaultModelId, generation: modelGeneration }),
|
|
73
|
+
createAdapter,
|
|
74
|
+
activeAdaptersExcept,
|
|
75
|
+
assertCanStart: () => {
|
|
76
|
+
if (disposed)
|
|
77
|
+
throw new MarAgentError('MAR_AGENT_DISPOSED', 'Agent is disposed.');
|
|
78
|
+
if (reconfiguring)
|
|
79
|
+
throw new MarAgentError('MAR_AGENT_EXECUTION_ACTIVE', 'Models are being reconfigured for the next execution.');
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
return {
|
|
83
|
+
createSession: async (input) => openSession((await store.createSession(input)).id, input.workspace),
|
|
84
|
+
openSession: (id) => openSession(id),
|
|
85
|
+
listSessions: (input) => store.list(input),
|
|
86
|
+
clearSession: async (id) => {
|
|
87
|
+
const runtime = sessionRuntimes.get(id);
|
|
88
|
+
if ([...active.keys()].some((key) => key.startsWith(`${id}:`)))
|
|
89
|
+
throw new MarAgentError('MAR_AGENT_EXECUTION_ACTIVE', 'A session cannot be cleared while an execution is active.');
|
|
90
|
+
await runtime?.subagents?.dispose();
|
|
91
|
+
await closeModelAdapters(runtime === undefined ? [] : [...runtime.adapters.values()]);
|
|
92
|
+
sessionRuntimes.delete(id);
|
|
93
|
+
await options.host.disposeSessionResources?.(id);
|
|
94
|
+
await store.clearSession(id);
|
|
95
|
+
},
|
|
96
|
+
reconfigureModels: async (input) => {
|
|
97
|
+
if (disposed)
|
|
98
|
+
throw new MarAgentError('MAR_AGENT_DISPOSED', 'Agent is disposed.');
|
|
99
|
+
if (reconfiguring)
|
|
100
|
+
throw new MarAgentError('MAR_AGENT_EXECUTION_ACTIVE', 'Models are already being reconfigured for the next execution.');
|
|
101
|
+
const next = resolveAgentModels(input.models, input.defaultModelId);
|
|
102
|
+
const activeSessions = activeSessionIds();
|
|
103
|
+
if ([...activeSessions].some((sessionId) => sessionRuntimes.get(sessionId)?.sourceType !== 'subagent'))
|
|
104
|
+
throw new MarAgentError('MAR_AGENT_EXECUTION_ACTIVE', 'Models cannot be reconfigured while a main execution is active.');
|
|
105
|
+
const activeAdapters = new Set(activeExecutionAdapters);
|
|
106
|
+
for (const [sessionId, runtime] of sessionRuntimes)
|
|
107
|
+
if (activeSessions.has(sessionId))
|
|
108
|
+
for (const adapter of runtime.adapters.values())
|
|
109
|
+
activeAdapters.add(adapter);
|
|
110
|
+
const inactiveRuntimes = [...sessionRuntimes].flatMap(([sessionId, runtime]) => activeSessions.has(sessionId) ? [] : [runtime]);
|
|
111
|
+
reconfiguring = true;
|
|
112
|
+
try {
|
|
113
|
+
const reconfigurationCommits = new Map();
|
|
114
|
+
for (const runtime of inactiveRuntimes)
|
|
115
|
+
for (const [modelId, adapter] of runtime.adapters) {
|
|
116
|
+
if (activeAdapters.has(adapter))
|
|
117
|
+
continue;
|
|
118
|
+
const configuration = next.models.get(modelId);
|
|
119
|
+
const commit = configuration === undefined ? undefined : adapter.prepareReconfigure?.(configuration);
|
|
120
|
+
if (commit !== undefined)
|
|
121
|
+
reconfigurationCommits.set(adapter, commit);
|
|
122
|
+
}
|
|
123
|
+
const retainedAdapters = new Set(reconfigurationCommits.keys());
|
|
124
|
+
const adapters = [
|
|
125
|
+
...new Set(inactiveRuntimes.flatMap((runtime) => [...runtime.adapters.values()]))
|
|
126
|
+
].filter((adapter) => !activeAdapters.has(adapter) && !retainedAdapters.has(adapter));
|
|
127
|
+
for (const runtime of inactiveRuntimes) {
|
|
128
|
+
for (const [modelId, adapter] of runtime.adapters)
|
|
129
|
+
if (!retainedAdapters.has(adapter))
|
|
130
|
+
runtime.adapters.delete(modelId);
|
|
131
|
+
}
|
|
132
|
+
await closeModelAdapters(adapters);
|
|
133
|
+
if (disposed)
|
|
134
|
+
throw new MarAgentError('MAR_AGENT_DISPOSED', 'Agent is disposed.');
|
|
135
|
+
for (const commit of reconfigurationCommits.values())
|
|
136
|
+
commit();
|
|
137
|
+
models = next.models;
|
|
138
|
+
defaultModelId = next.defaultModelId;
|
|
139
|
+
modelGeneration++;
|
|
140
|
+
for (const provider of next.credentialProviders)
|
|
141
|
+
credentialProviders.add(provider);
|
|
142
|
+
for (const runtime of inactiveRuntimes) {
|
|
143
|
+
runtime.adapterGeneration = modelGeneration;
|
|
144
|
+
}
|
|
145
|
+
const liveProviders = new Set(next.credentialProviders);
|
|
146
|
+
for (const adapter of activeExecutionAdapters)
|
|
147
|
+
addModelCredentialProviders(liveProviders, adapter.configuration);
|
|
148
|
+
for (const runtime of sessionRuntimes.values())
|
|
149
|
+
for (const adapter of runtime.adapters.values())
|
|
150
|
+
addModelCredentialProviders(liveProviders, adapter.configuration);
|
|
151
|
+
const obsoleteProviders = [...credentialProviders].filter((provider) => !liveProviders.has(provider));
|
|
152
|
+
const providerCloseResults = await Promise.allSettled(obsoleteProviders.map((provider) => provider.close()));
|
|
153
|
+
for (const [index, result] of providerCloseResults.entries())
|
|
154
|
+
if (result.status === 'fulfilled')
|
|
155
|
+
credentialProviders.delete(obsoleteProviders[index]);
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
reconfiguring = false;
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
deleteSession: async (id) => {
|
|
162
|
+
const sessionIds = await store.sessionTree(id);
|
|
163
|
+
const sessionIdSet = new Set(sessionIds);
|
|
164
|
+
const executionKeys = [...active.keys()].filter((key) => sessionIdSet.has(key.slice(0, key.indexOf(':'))));
|
|
165
|
+
for (const key of executionKeys)
|
|
166
|
+
active.get(key)?.abort();
|
|
167
|
+
const runtimes = sessionIds.flatMap((sessionId) => {
|
|
168
|
+
const runtime = sessionRuntimes.get(sessionId);
|
|
169
|
+
sessionRuntimes.delete(sessionId);
|
|
170
|
+
return runtime === undefined ? [] : [runtime];
|
|
171
|
+
});
|
|
172
|
+
await Promise.all(runtimes.map((runtime) => runtime.subagents?.dispose()));
|
|
173
|
+
await Promise.allSettled(executionKeys.flatMap((key) => {
|
|
174
|
+
const result = executionResults.get(key);
|
|
175
|
+
return result === undefined ? [] : [result];
|
|
176
|
+
}));
|
|
177
|
+
await closeModelAdapters(runtimes.flatMap((runtime) => [...runtime.adapters.values()]));
|
|
178
|
+
await Promise.all(sessionIds.map((sessionId) => options.host.disposeSessionResources?.(sessionId)));
|
|
179
|
+
await store.delete(id);
|
|
180
|
+
},
|
|
181
|
+
dispose: async () => {
|
|
182
|
+
if (disposed)
|
|
183
|
+
return;
|
|
184
|
+
disposed = true;
|
|
185
|
+
for (const controller of active.values())
|
|
186
|
+
controller.abort();
|
|
187
|
+
await Promise.all([...sessionRuntimes.values()].map((runtime) => runtime.subagents?.dispose().catch(() => undefined)));
|
|
188
|
+
await Promise.allSettled([...executionResults.values()]);
|
|
189
|
+
await closeModelAdapters([...sessionRuntimes.values()].flatMap((runtime) => [...runtime.adapters.values()]));
|
|
190
|
+
let disposalError;
|
|
191
|
+
const providerResults = await Promise.allSettled([...credentialProviders].map((provider) => provider.close()));
|
|
192
|
+
disposalError = providerResults.find((result) => result.status === 'rejected')?.reason;
|
|
193
|
+
try {
|
|
194
|
+
await options.host.dispose();
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
disposalError ??= error;
|
|
198
|
+
}
|
|
199
|
+
try {
|
|
200
|
+
await store.close();
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
disposalError ??= error;
|
|
204
|
+
}
|
|
205
|
+
if (disposalError !== undefined)
|
|
206
|
+
throw disposalError;
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type MarAgentModelConfiguration, type MarAgentReasoningEffort, type ModelCredentialProvider } from '../model/configuration.js';
|
|
2
|
+
import type { ModelAdapter, ModelTurnSession } from '../model/contracts.js';
|
|
3
|
+
export type ResolvedModelConfiguration = MarAgentModelConfiguration & {
|
|
4
|
+
readonly defaultReasoningEffort: MarAgentReasoningEffort;
|
|
5
|
+
};
|
|
6
|
+
export declare function resolveAgentModels(configurations: readonly MarAgentModelConfiguration[], requestedDefaultModelId?: string): {
|
|
7
|
+
readonly models: Map<string, ResolvedModelConfiguration>;
|
|
8
|
+
readonly defaultModelId: string;
|
|
9
|
+
readonly credentialProviders: Set<ModelCredentialProvider>;
|
|
10
|
+
};
|
|
11
|
+
export declare function createModelTurnSession(adapter: ModelAdapter): ModelTurnSession;
|
|
12
|
+
export declare function closeModelAdapters(adapters: Iterable<ModelAdapter>): Promise<void>;
|
|
13
|
+
export declare function addModelCredentialProviders(target: Set<ModelCredentialProvider>, configuration: MarAgentModelConfiguration): void;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { MarAgentError } from '../error.js';
|
|
2
|
+
import { resolveModelDefaultReasoningEffort } from '../model/configuration.js';
|
|
3
|
+
export function resolveAgentModels(configurations, requestedDefaultModelId) {
|
|
4
|
+
const configuredModels = configurations.map((model) => ({
|
|
5
|
+
...model,
|
|
6
|
+
defaultReasoningEffort: resolveModelDefaultReasoningEffort(model)
|
|
7
|
+
}));
|
|
8
|
+
const credentialProviders = new Set();
|
|
9
|
+
for (const model of configuredModels) {
|
|
10
|
+
if ((model.apiKey === undefined) === (model.credentialProvider === undefined))
|
|
11
|
+
throw new MarAgentError('MAR_AGENT_MODEL_CONFIG_INVALID', 'Exactly one model credential source is required.');
|
|
12
|
+
if (model.responsesEncoding !== undefined &&
|
|
13
|
+
(model.protocol !== 'OPENAI_RESPONSES' ||
|
|
14
|
+
!['STANDARD', 'LITE'].includes(model.responsesEncoding)))
|
|
15
|
+
throw new MarAgentError('MAR_AGENT_MODEL_CONFIG_INVALID', 'Responses encoding is invalid.');
|
|
16
|
+
if (model.fastMode === true && model.protocol !== 'OPENAI_RESPONSES')
|
|
17
|
+
throw new MarAgentError('MAR_AGENT_MODEL_CONFIG_INVALID', 'Fast mode requires Responses.');
|
|
18
|
+
if (model.serviceTier !== undefined &&
|
|
19
|
+
(model.protocol !== 'OPENAI_RESPONSES' ||
|
|
20
|
+
!['auto', 'default', 'flex', 'priority', 'fast', 'ultrafast'].includes(model.serviceTier)))
|
|
21
|
+
throw new MarAgentError('MAR_AGENT_MODEL_CONFIG_INVALID', 'Responses service tier is invalid.');
|
|
22
|
+
if (model.credentialProvider !== undefined)
|
|
23
|
+
credentialProviders.add(model.credentialProvider);
|
|
24
|
+
}
|
|
25
|
+
const models = new Map(configuredModels.filter((model) => model.enabled).map((model) => [model.id, model]));
|
|
26
|
+
if (models.size === 0)
|
|
27
|
+
throw new MarAgentError('MAR_AGENT_MODEL_CONFIG_INVALID', 'At least one enabled model is required.');
|
|
28
|
+
const defaultModelId = requestedDefaultModelId ?? models.keys().next().value;
|
|
29
|
+
if (!models.has(defaultModelId))
|
|
30
|
+
throw new MarAgentError('MAR_AGENT_MODEL_CONFIG_INVALID', 'Default model is unavailable.');
|
|
31
|
+
return { models, defaultModelId, credentialProviders };
|
|
32
|
+
}
|
|
33
|
+
export function createModelTurnSession(adapter) {
|
|
34
|
+
const turn = adapter.createTurnSession?.() ?? {
|
|
35
|
+
start: (request, signal) => adapter.start(request, signal),
|
|
36
|
+
close: async () => undefined
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
async *start(request, signal) {
|
|
40
|
+
const before = request.retryState?.attempts;
|
|
41
|
+
try {
|
|
42
|
+
yield* turn.start(request, signal);
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
// Custom adapters may not participate in the shared attempt accounting.
|
|
46
|
+
if (request.retryState && request.retryState.attempts === before)
|
|
47
|
+
request.retryState.attempts++;
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
resetContinuation: () => turn.resetContinuation?.(),
|
|
51
|
+
close: () => turn.close()
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export async function closeModelAdapters(adapters) {
|
|
55
|
+
const results = await Promise.allSettled([...new Set(adapters)].flatMap((adapter) => adapter.close === undefined ? [] : [adapter.close()]));
|
|
56
|
+
const failure = results.find((result) => result.status === 'rejected');
|
|
57
|
+
if (failure !== undefined)
|
|
58
|
+
throw failure.reason;
|
|
59
|
+
}
|
|
60
|
+
export function addModelCredentialProviders(target, configuration) {
|
|
61
|
+
if (configuration.credentialProvider !== undefined)
|
|
62
|
+
target.add(configuration.credentialProvider);
|
|
63
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { MarAgentReasoningEffort } from '../model/configuration.js';
|
|
2
|
+
import type { ModelAdapter, ModelAttemptDiagnostic } from '../model/contracts.js';
|
|
3
|
+
export declare function generateSessionTitle(adapter: ModelAdapter, prompt: string, reasoningEffort: MarAgentReasoningEffort, signal: AbortSignal, onAttemptDiagnostic: (diagnostic: ModelAttemptDiagnostic) => Promise<void> | void): Promise<string | undefined>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { sessionTitlePrompt } from '../prompts/title.js';
|
|
2
|
+
import { createModelTurnSession } from './model-runtime.js';
|
|
3
|
+
export async function generateSessionTitle(adapter, prompt, reasoningEffort, signal, onAttemptDiagnostic) {
|
|
4
|
+
let titleInput = '';
|
|
5
|
+
let characters = 0;
|
|
6
|
+
for (const character of prompt) {
|
|
7
|
+
if (characters++ >= 1000)
|
|
8
|
+
break;
|
|
9
|
+
titleInput += character;
|
|
10
|
+
}
|
|
11
|
+
let output = '';
|
|
12
|
+
let completed = false;
|
|
13
|
+
const turn = createModelTurnSession(adapter);
|
|
14
|
+
try {
|
|
15
|
+
for await (const event of turn.start({
|
|
16
|
+
retryState: { attempts: 0, maxAttempts: 1 },
|
|
17
|
+
system: sessionTitlePrompt,
|
|
18
|
+
messages: [{ role: 'user', content: titleInput }],
|
|
19
|
+
tools: [],
|
|
20
|
+
reasoningEffort,
|
|
21
|
+
onAttemptDiagnostic
|
|
22
|
+
}, signal)) {
|
|
23
|
+
if (event.type === 'text.completed')
|
|
24
|
+
output = event.text;
|
|
25
|
+
else if (event.type === 'completed')
|
|
26
|
+
completed = event.reason === 'completed';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
finally {
|
|
30
|
+
await turn.close();
|
|
31
|
+
}
|
|
32
|
+
if (!completed)
|
|
33
|
+
return undefined;
|
|
34
|
+
const line = output
|
|
35
|
+
.replace(/<think>[\s\S]*?<\/think>\s*/giu, '')
|
|
36
|
+
.split(/\r?\n/u)
|
|
37
|
+
.map((value) => value.trim())
|
|
38
|
+
.find(Boolean);
|
|
39
|
+
if (!line)
|
|
40
|
+
return undefined;
|
|
41
|
+
return [...line].slice(0, 50).join('');
|
|
42
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ContextualWorldState } from '../runtime/context/world-state.js';
|
|
2
|
+
import type { ResolvedModelConfiguration } from './model-runtime.js';
|
|
3
|
+
import type { HostDescription, MarAgentHost } from '../host/contracts.js';
|
|
4
|
+
import { type MarAgentReasoningEffort } from '../model/configuration.js';
|
|
5
|
+
import type { ModelAdapter } from '../model/contracts.js';
|
|
6
|
+
import { RolloutBudget, resolveRolloutBudgetOptions } from '../runtime/rollout-budget.js';
|
|
7
|
+
import { JsonlSessionStore } from '../session/jsonl-store.js';
|
|
8
|
+
import { SubagentSessionController, type SubagentMailboxMessage, type SubagentMessageDelivery, type SubagentTurnHandle } from '../subagent/session-controller.js';
|
|
9
|
+
import { createSessionToolScheduler } from '../tools/scheduler.js';
|
|
10
|
+
import { TodoStore } from '../tools/agent/todo.js';
|
|
11
|
+
import type { CreateMarAgentOptions, ExecutionResult, MarAgentSession } from './types.js';
|
|
12
|
+
export interface SessionRuntime {
|
|
13
|
+
todo?: TodoStore;
|
|
14
|
+
readonly scheduleTool: ReturnType<typeof createSessionToolScheduler>;
|
|
15
|
+
readonly sourceType: 'user' | 'subagent';
|
|
16
|
+
adapterGeneration: number;
|
|
17
|
+
adapters: Map<string, ModelAdapter>;
|
|
18
|
+
contextualWorldState?: ContextualWorldState;
|
|
19
|
+
subagents?: SubagentSessionController;
|
|
20
|
+
subagentsOpening?: Promise<SubagentSessionController>;
|
|
21
|
+
}
|
|
22
|
+
interface InternalMarAgentSession extends MarAgentSession {
|
|
23
|
+
startSubagentTurn(input: {
|
|
24
|
+
readonly prompt: string;
|
|
25
|
+
readonly modelId: string;
|
|
26
|
+
readonly reasoningEffort: MarAgentReasoningEffort;
|
|
27
|
+
readonly role: 'general-purpose' | 'explore';
|
|
28
|
+
readonly initialMailbox: readonly SubagentMailboxMessage[];
|
|
29
|
+
readonly turnId: string;
|
|
30
|
+
readonly delivery?: SubagentMessageDelivery;
|
|
31
|
+
readonly sharedRolloutBudget?: RolloutBudget;
|
|
32
|
+
readonly mcpExecutionScopeId: string;
|
|
33
|
+
}): SubagentTurnHandle;
|
|
34
|
+
}
|
|
35
|
+
/** Agent-owned registries shared with Session orchestration, not public execution state. */
|
|
36
|
+
export interface AgentSessionRegistry {
|
|
37
|
+
readonly active: Map<string, AbortController>;
|
|
38
|
+
readonly activeExecutionAdapters: Set<ModelAdapter>;
|
|
39
|
+
readonly executionResults: Map<string, Promise<ExecutionResult>>;
|
|
40
|
+
readonly sessionRuntimes: Map<string, SessionRuntime>;
|
|
41
|
+
readonly sessionWriterOwner: object;
|
|
42
|
+
}
|
|
43
|
+
interface SessionServices {
|
|
44
|
+
readonly host: MarAgentHost;
|
|
45
|
+
readonly description: HostDescription;
|
|
46
|
+
readonly settings: Pick<CreateMarAgentOptions, 'workspaceMapping' | 'systemInstruction'>;
|
|
47
|
+
readonly store: JsonlSessionStore;
|
|
48
|
+
readonly registry: AgentSessionRegistry;
|
|
49
|
+
readonly rolloutBudgetOptions: ReturnType<typeof resolveRolloutBudgetOptions>;
|
|
50
|
+
readonly hasCustomAdapters: boolean;
|
|
51
|
+
readonly modelState: () => {
|
|
52
|
+
readonly models: ReadonlyMap<string, ResolvedModelConfiguration>;
|
|
53
|
+
readonly defaultModelId: string;
|
|
54
|
+
readonly generation: number;
|
|
55
|
+
};
|
|
56
|
+
readonly createAdapter: (modelId: string, models: ReadonlyMap<string, ResolvedModelConfiguration>) => ModelAdapter;
|
|
57
|
+
readonly activeAdaptersExcept: (sessionId: string) => ReadonlySet<ModelAdapter>;
|
|
58
|
+
readonly assertCanStart: () => void;
|
|
59
|
+
}
|
|
60
|
+
/** SDK Session orchestration owns warm resources; Runtime owns only the model/tool loop. */
|
|
61
|
+
export declare function createSessionOpener(services: SessionServices): (sessionId: string, workspace?: string) => Promise<InternalMarAgentSession>;
|
|
62
|
+
export {};
|