Implementation:0xsline OpenChatCut RunToolSession
Appearance
| Knowledge Sources | |
|---|---|
| Domains | CLI, Editing, Agent |
| Last Updated | 2026-10-22 14:00 GMT |
Overview
Concrete orchestrator that runs a batch of tool invocations inside one atomic edit session, then commits or discards the draft based on whether any changes were staged.
Description
The runToolSession function is the CLI's only write path. It wraps OfflineExternalEditRuntime to provide a single-call API for running multiple tools and then atomically applying or discarding the result.
Key behaviors:
- Creates a runtime via `OfflineExternalEditRuntime.create()` with the CLI editor URL.
- Begins a session with `approvalMode: 'auto'` and `clientName: 'occ CLI'`.
- Runs each invocation sequentially, collecting execution results.
- Checks staged changes — if `apply: true` but zero operations were staged, it discards instead of reviewing an empty draft.
- Commits or discards based on `options.apply`.
- Always disposes the runtime in a `finally` block to release ownership.
Usage
- CLI apply mode: `occ project edit --apply --ops '[...]'` calls `runToolSession` with `apply: true`.
- CLI dry-run mode: `occ project edit --ops '[...]'` (no `--apply`) calls with `apply: false` to preview changes.
- Programmatic editing: Any Node.js script can import and call `runToolSession` directly.
Code Reference
Source Location
- Repository: OpenChatCut
- File: cli/session.ts
Signature
export interface ToolInvocation {
readonly tool: string;
readonly args: Readonly<Record<string, unknown>>;
}
export interface ToolExecution {
readonly tool: string;
readonly result: unknown;
}
export interface SessionOutcome {
readonly applied: boolean;
readonly executions: readonly ToolExecution[];
readonly terminal: unknown;
readonly noChanges: boolean;
}
export async function runToolSession(
projectId: string,
invocations: readonly ToolInvocation[],
options: { readonly apply: boolean; readonly summary?: string },
): Promise<SessionOutcome>;
Import
import { runToolSession, type ToolInvocation, type SessionOutcome } from './session.ts';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| projectId | string | Yes | Target project ID |
| invocations | readonly ToolInvocation[] | Yes | Tools to run, each with name and args |
| options.apply | boolean | Yes | Commit (true) or discard (false) the draft |
| options.summary | string | No | Commit message for the review step |
Outputs
| Name | Type | Description |
|---|---|---|
| applied | boolean | Whether the draft was committed |
| executions | readonly ToolExecution[] | Per-tool execution results |
| terminal | unknown | Final session state from review or discard |
| noChanges | boolean | True if tools ran but staged nothing; draft was discarded |
Usage Examples
Apply Edits
import { runToolSession } from './session.ts';
const outcome = await runToolSession(
'my-project-id',
[
{ tool: 'import_media', args: { path: '/videos/clip.mp4' } },
{ tool: 'set_timeline_fps', args: { fps: 30 } },
],
{ apply: true, summary: 'Imported clip and set FPS' },
);
console.log(`Applied: ${outcome.applied}, No changes: ${outcome.noChanges}`);
for (const exec of outcome.executions) {
console.log(` ${exec.tool}:`, exec.result);
}
Dry-Run Preview
// Run tools but don't commit — project stays unchanged
const preview = await runToolSession(
'my-project-id',
[{ tool: 'import_media', args: { path: '/videos/clip.mp4' } }],
{ apply: false },
);
console.log('Would apply:', preview.executions.length, 'tools');
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment