@workser/engine 0.1.0
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/NOTICE +8 -0
- package/README.md +70 -0
- package/dist/src/compact.d.ts +44 -0
- package/dist/src/compact.js +116 -0
- package/dist/src/engine.d.ts +84 -0
- package/dist/src/engine.js +142 -0
- package/dist/src/index.d.ts +13 -0
- package/dist/src/index.js +13 -0
- package/dist/src/model.d.ts +56 -0
- package/dist/src/model.js +153 -0
- package/dist/src/place/e2b.d.ts +76 -0
- package/dist/src/place/e2b.js +114 -0
- package/dist/src/place/env.d.ts +3 -0
- package/dist/src/place/env.js +14 -0
- package/dist/src/place/local.d.ts +33 -0
- package/dist/src/place/local.js +138 -0
- package/dist/src/prompts.d.ts +27 -0
- package/dist/src/prompts.js +108 -0
- package/dist/src/session.d.ts +115 -0
- package/dist/src/session.js +523 -0
- package/dist/src/tools/files.d.ts +9 -0
- package/dist/src/tools/files.js +209 -0
- package/dist/src/tools/patch.d.ts +49 -0
- package/dist/src/tools/patch.js +272 -0
- package/dist/src/tools/relay.d.ts +25 -0
- package/dist/src/tools/relay.js +82 -0
- package/dist/src/tools/search.d.ts +5 -0
- package/dist/src/tools/search.js +99 -0
- package/dist/src/tools/shell.d.ts +26 -0
- package/dist/src/tools/shell.js +123 -0
- package/dist/src/tools/types.d.ts +49 -0
- package/dist/src/tools/types.js +16 -0
- package/dist/src/tools/workser-tool.d.ts +39 -0
- package/dist/src/tools/workser-tool.js +51 -0
- package/dist/src/vendor.d.ts +4 -0
- package/dist/src/vendor.js +17 -0
- package/package.json +42 -0
- package/vendor/opencode-1.18.23/COMMIT +1 -0
- package/vendor/opencode-1.18.23/LICENSE +21 -0
- package/vendor/opencode-1.18.23/session/prompt/anthropic.txt +105 -0
- package/vendor/opencode-1.18.23/session/prompt/beast.txt +147 -0
- package/vendor/opencode-1.18.23/session/prompt/build-switch.txt +5 -0
- package/vendor/opencode-1.18.23/session/prompt/codex.txt +79 -0
- package/vendor/opencode-1.18.23/session/prompt/copilot-gpt-5.txt +143 -0
- package/vendor/opencode-1.18.23/session/prompt/default.txt +95 -0
- package/vendor/opencode-1.18.23/session/prompt/gemini.txt +155 -0
- package/vendor/opencode-1.18.23/session/prompt/gpt.txt +107 -0
- package/vendor/opencode-1.18.23/session/prompt/kimi.txt +95 -0
- package/vendor/opencode-1.18.23/session/prompt/meta.txt +65 -0
- package/vendor/opencode-1.18.23/session/prompt/plan-mode.txt +70 -0
- package/vendor/opencode-1.18.23/session/prompt/plan-reminder-anthropic.txt +67 -0
- package/vendor/opencode-1.18.23/session/prompt/plan.txt +26 -0
- package/vendor/opencode-1.18.23/session/prompt/trinity.txt +97 -0
- package/vendor/opencode-1.18.23/session/system.ts.ref +152 -0
- package/vendor/opencode-1.18.23/tool/apply_patch.txt +33 -0
- package/vendor/opencode-1.18.23/tool/edit-replace.ts +535 -0
- package/vendor/opencode-1.18.23/tool/edit.txt +10 -0
- package/vendor/opencode-1.18.23/tool/glob.txt +6 -0
- package/vendor/opencode-1.18.23/tool/grep.txt +8 -0
- package/vendor/opencode-1.18.23/tool/read.txt +14 -0
- package/vendor/opencode-1.18.23/tool/shell/prompt.ts.ref +293 -0
- package/vendor/opencode-1.18.23/tool/shell/shell.txt +21 -0
- package/vendor/opencode-1.18.23/tool/todowrite.txt +44 -0
- package/vendor/opencode-1.18.23/tool/write.txt +8 -0
package/NOTICE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
This package contains material from OpenCode (https://github.com/sst/opencode),
|
|
2
|
+
version v1.18.23 (commit ef2880f379129aa048be9e9353e30aa168d42c17),
|
|
3
|
+
Copyright (c) 2025 opencode, used under the MIT License (vendor/opencode-1.18.23/LICENSE).
|
|
4
|
+
|
|
5
|
+
Vendored, unmodified: the system prompts (session/prompt/*.txt), the tool
|
|
6
|
+
descriptions (tool/*.txt, tool/shell/shell.txt), the shell prompt builder
|
|
7
|
+
(tool/shell/prompt.ts, reference only) and the edit tool's matching code
|
|
8
|
+
(tool/edit-replace.ts: lines 22-33 and 217-737 of tool/edit.ts).
|
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# @workser/engine
|
|
2
|
+
|
|
3
|
+
Workser's coding engine for the **loop in our service** mode (plan decision
|
|
4
|
+
D1): OpenCode's agent contract running on Node — in a Trigger.dev worker, or
|
|
5
|
+
anywhere — with every file and shell action going through a **Place**. The
|
|
6
|
+
machine can be a remote E2B sandbox or the project's cloud computer; the model
|
|
7
|
+
key (the run token) never goes there.
|
|
8
|
+
|
|
9
|
+
## What is OpenCode's here
|
|
10
|
+
|
|
11
|
+
Vendored from OpenCode **v1.18.23** (MIT, `vendor/opencode-1.18.23/`, see
|
|
12
|
+
`NOTICE`), unmodified:
|
|
13
|
+
|
|
14
|
+
- the system prompts (`session/prompt/*.txt`), chosen per model exactly like
|
|
15
|
+
upstream `SystemPrompt.provider()`, plus upstream's `<env>` block;
|
|
16
|
+
- the tool descriptions (`tool/*.txt`, `tool/shell/shell.txt` rendered like
|
|
17
|
+
upstream's bash profile);
|
|
18
|
+
- the edit tool's matching code (`tool/edit-replace.ts` — the nine replacers
|
|
19
|
+
and `replace()` from upstream `tool/edit.ts`).
|
|
20
|
+
|
|
21
|
+
The tools keep upstream's names, argument fields and descriptions, and
|
|
22
|
+
upstream's output formats: `bash`, `read` (numbered `N: line`, offset/limit,
|
|
23
|
+
the same notes), `write`, `edit`, `glob` (ripgrep, 100 newest), `grep`
|
|
24
|
+
(ripgrep, "Found N matches", ` Line N:`), `todowrite`.
|
|
25
|
+
|
|
26
|
+
What is NOT upstream: the loop itself. Upstream's session loop is built on
|
|
27
|
+
Effect services (SQLite storage, bus, LSP, formatters, snapshots) and Bun;
|
|
28
|
+
this is a small Node loop over OpenAI-compatible chat completions that sends
|
|
29
|
+
the same prompts and tools. Plain OpenCode stays the yardstick (the 20-task
|
|
30
|
+
check in `workser-runner/eval`).
|
|
31
|
+
|
|
32
|
+
## Use
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { RunnerClient } from '@workser/runtime';
|
|
36
|
+
import { connectE2BPlace, runEngine } from '@workser/engine';
|
|
37
|
+
|
|
38
|
+
const client = new RunnerClient({ baseUrl, runId, token }); // the run token
|
|
39
|
+
const brief = await client.start();
|
|
40
|
+
const place = await connectE2BPlace({ sandboxId, root: '/home/user/app' });
|
|
41
|
+
const { outcome } = await runEngine({
|
|
42
|
+
brief, place, runnerClient: client,
|
|
43
|
+
modelBaseUrl: 'https://agent.workser.ai/gateway/v1',
|
|
44
|
+
modelKey: () => client.currentToken,
|
|
45
|
+
});
|
|
46
|
+
await client.finish(outcome.status === 'completed' ? 'run.completed' : outcome.status === 'cancelled' ? 'run.cancelled' : 'run.failed',
|
|
47
|
+
outcome.status === 'completed' ? { summary: outcome.summary } : { message: outcome.message, error_kind: outcome.error_kind });
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`CoreEngine` implements the SDK `Engine`, so `workser-runner` can use it as
|
|
51
|
+
an engine too (`start`, `resume(state)`, `send`, `answer`, `stop`, `save`).
|
|
52
|
+
|
|
53
|
+
## Production pieces
|
|
54
|
+
|
|
55
|
+
- `apply_patch` for GPT models (upstream's rule); `edit` + `write` for the rest.
|
|
56
|
+
- A persistent shell: exported variables and the folder carry between `bash` calls.
|
|
57
|
+
- Streaming: `message.delta` events as the model writes.
|
|
58
|
+
- Compaction: old tool output cleared, then the middle summarized, at 80 % of the context budget (`input.context_tokens`, default 128k).
|
|
59
|
+
- Session mode (`mode: 'session'`) and `continue()` for a job restored from a checkpoint.
|
|
60
|
+
- In the runner: `workser-runner --engine core` (checkpoint in the run's journal folder).
|
|
61
|
+
|
|
62
|
+
## Rules
|
|
63
|
+
|
|
64
|
+
- Destructive commands (`sudo`, `rm -rf`, `git push`, `git reset --hard`)
|
|
65
|
+
ask the owner first — the same list as the runner's OpenCode config. A "no"
|
|
66
|
+
reaches the model as a denial.
|
|
67
|
+
- Every path is kept inside the Place's root.
|
|
68
|
+
- Model errors end the job as failed with a kind (`insufficient_credits`,
|
|
69
|
+
`model_refused`, `model_error`).
|
|
70
|
+
# workser-engine
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ChatMessage } from './model.js';
|
|
2
|
+
/**
|
|
3
|
+
* KEEPING A LONG JOB INSIDE THE MODEL'S WINDOW (plan: "compressing long
|
|
4
|
+
* conversations so long jobs keep fitting"). Two steps, cheapest first, the
|
|
5
|
+
* same order upstream OpenCode uses:
|
|
6
|
+
*
|
|
7
|
+
* 1. PRUNE: old tool outputs (a 2,000-line `read`, a test log) are replaced
|
|
8
|
+
* by a one-line note. The most recent `keepToolTokens` of tool output stay
|
|
9
|
+
* whole, and every tool call keeps its answer, so the conversation stays
|
|
10
|
+
* well-formed.
|
|
11
|
+
* 2. SUMMARIZE: when pruning isn't enough, the middle of the conversation
|
|
12
|
+
* is replaced by a summary the model writes of it (the goal, what was
|
|
13
|
+
* done, files touched, what is left). The system prompt, the job's first
|
|
14
|
+
* request and the most recent turns stay as they are.
|
|
15
|
+
*
|
|
16
|
+
* Token counts are estimates (about 4 characters per token) unless the model
|
|
17
|
+
* route reported the real prompt size, which is then preferred.
|
|
18
|
+
*/
|
|
19
|
+
export declare const PRUNED_NOTE = "[Old tool result content cleared]";
|
|
20
|
+
export interface CompactionBudget {
|
|
21
|
+
/** The most the conversation may be before a turn, in tokens. */
|
|
22
|
+
contextTokens: number;
|
|
23
|
+
/** Compact when a turn's prompt reaches this share of `contextTokens`. */
|
|
24
|
+
threshold: number;
|
|
25
|
+
/** Recent tool output kept whole by the prune step, in tokens. */
|
|
26
|
+
keepToolTokens: number;
|
|
27
|
+
}
|
|
28
|
+
export declare const DEFAULT_BUDGET: CompactionBudget;
|
|
29
|
+
export declare function estimateTokens(messages: ChatMessage[]): number;
|
|
30
|
+
export declare function overBudget(messages: ChatMessage[], budget: CompactionBudget, reportedPromptTokens?: number): boolean;
|
|
31
|
+
/** Step 1. Returns how many tool outputs were cleared (the array is changed in place). */
|
|
32
|
+
export declare function pruneToolOutputs(messages: ChatMessage[], keepToolTokens: number): number;
|
|
33
|
+
/**
|
|
34
|
+
* Where the kept tail starts for step 2. The system prompt (0) and the job's
|
|
35
|
+
* first owner message (1) always stay; the tail is the most recent turns
|
|
36
|
+
* that fit in `keepTokens`, starting at an assistant or owner message so no
|
|
37
|
+
* tool answer is separated from its call. -1 when there is nothing between
|
|
38
|
+
* the head and the tail to summarize.
|
|
39
|
+
*/
|
|
40
|
+
export declare function cutPoint(messages: ChatMessage[], keepTokens: number): number;
|
|
41
|
+
/** The request that asks the model to summarize the part being replaced. */
|
|
42
|
+
export declare function summaryRequest(part: ChatMessage[]): ChatMessage[];
|
|
43
|
+
/** Step 2's result: the system prompt, the goal with the summary, then the kept tail. */
|
|
44
|
+
export declare function withSummary(messages: ChatMessage[], cut: number, summary: string): ChatMessage[];
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KEEPING A LONG JOB INSIDE THE MODEL'S WINDOW (plan: "compressing long
|
|
3
|
+
* conversations so long jobs keep fitting"). Two steps, cheapest first, the
|
|
4
|
+
* same order upstream OpenCode uses:
|
|
5
|
+
*
|
|
6
|
+
* 1. PRUNE: old tool outputs (a 2,000-line `read`, a test log) are replaced
|
|
7
|
+
* by a one-line note. The most recent `keepToolTokens` of tool output stay
|
|
8
|
+
* whole, and every tool call keeps its answer, so the conversation stays
|
|
9
|
+
* well-formed.
|
|
10
|
+
* 2. SUMMARIZE: when pruning isn't enough, the middle of the conversation
|
|
11
|
+
* is replaced by a summary the model writes of it (the goal, what was
|
|
12
|
+
* done, files touched, what is left). The system prompt, the job's first
|
|
13
|
+
* request and the most recent turns stay as they are.
|
|
14
|
+
*
|
|
15
|
+
* Token counts are estimates (about 4 characters per token) unless the model
|
|
16
|
+
* route reported the real prompt size, which is then preferred.
|
|
17
|
+
*/
|
|
18
|
+
export const PRUNED_NOTE = '[Old tool result content cleared]';
|
|
19
|
+
export const DEFAULT_BUDGET = {
|
|
20
|
+
contextTokens: 128_000,
|
|
21
|
+
threshold: 0.8,
|
|
22
|
+
keepToolTokens: 40_000,
|
|
23
|
+
};
|
|
24
|
+
export function estimateTokens(messages) {
|
|
25
|
+
let chars = 0;
|
|
26
|
+
for (const message of messages) {
|
|
27
|
+
chars += (message.content ?? '').length + 16;
|
|
28
|
+
for (const call of message.tool_calls ?? [])
|
|
29
|
+
chars += call.function.name.length + call.function.arguments.length + 16;
|
|
30
|
+
}
|
|
31
|
+
return Math.ceil(chars / 4);
|
|
32
|
+
}
|
|
33
|
+
export function overBudget(messages, budget, reportedPromptTokens) {
|
|
34
|
+
const tokens = Math.max(estimateTokens(messages), reportedPromptTokens ?? 0);
|
|
35
|
+
return tokens >= budget.contextTokens * budget.threshold;
|
|
36
|
+
}
|
|
37
|
+
/** Step 1. Returns how many tool outputs were cleared (the array is changed in place). */
|
|
38
|
+
export function pruneToolOutputs(messages, keepToolTokens) {
|
|
39
|
+
let kept = 0;
|
|
40
|
+
let cleared = 0;
|
|
41
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
42
|
+
const message = messages[i];
|
|
43
|
+
if (message.role !== 'tool' || !message.content || message.content === PRUNED_NOTE)
|
|
44
|
+
continue;
|
|
45
|
+
const size = Math.ceil(message.content.length / 4);
|
|
46
|
+
if (kept + size <= keepToolTokens) {
|
|
47
|
+
kept += size;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
message.content = PRUNED_NOTE;
|
|
51
|
+
cleared++;
|
|
52
|
+
}
|
|
53
|
+
return cleared;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Where the kept tail starts for step 2. The system prompt (0) and the job's
|
|
57
|
+
* first owner message (1) always stay; the tail is the most recent turns
|
|
58
|
+
* that fit in `keepTokens`, starting at an assistant or owner message so no
|
|
59
|
+
* tool answer is separated from its call. -1 when there is nothing between
|
|
60
|
+
* the head and the tail to summarize.
|
|
61
|
+
*/
|
|
62
|
+
export function cutPoint(messages, keepTokens) {
|
|
63
|
+
let best = -1;
|
|
64
|
+
let size = 0;
|
|
65
|
+
for (let i = messages.length - 1; i > 2; i--) {
|
|
66
|
+
size += estimateTokens([messages[i]]);
|
|
67
|
+
const role = messages[i].role;
|
|
68
|
+
if (role !== 'assistant' && role !== 'user')
|
|
69
|
+
continue;
|
|
70
|
+
if (size > keepTokens && best !== -1)
|
|
71
|
+
break;
|
|
72
|
+
best = i;
|
|
73
|
+
if (size > keepTokens)
|
|
74
|
+
break; // the newest turn alone is over: keep just it
|
|
75
|
+
}
|
|
76
|
+
return best;
|
|
77
|
+
}
|
|
78
|
+
/** The request that asks the model to summarize the part being replaced. */
|
|
79
|
+
export function summaryRequest(part) {
|
|
80
|
+
const transcript = part
|
|
81
|
+
.map((message) => {
|
|
82
|
+
if (message.role === 'tool')
|
|
83
|
+
return `[tool result]\n${(message.content ?? '').slice(0, 4_000)}`;
|
|
84
|
+
const calls = (message.tool_calls ?? [])
|
|
85
|
+
.map((call) => `[called ${call.function.name} ${call.function.arguments.slice(0, 1_000)}]`)
|
|
86
|
+
.join('\n');
|
|
87
|
+
return `[${message.role}]\n${message.content ?? ''}${calls ? `\n${calls}` : ''}`;
|
|
88
|
+
})
|
|
89
|
+
.join('\n\n');
|
|
90
|
+
return [
|
|
91
|
+
{
|
|
92
|
+
role: 'system',
|
|
93
|
+
content: 'You summarize a coding session so another AI agent can continue it without the full history. ' +
|
|
94
|
+
'Write plain text with these parts: the goal; what has been done; files created or changed (paths); ' +
|
|
95
|
+
'commands that matter and their results; decisions and constraints from the owner; what is left to do next. ' +
|
|
96
|
+
'Be specific (names, paths, errors). Do not invent anything.',
|
|
97
|
+
},
|
|
98
|
+
{ role: 'user', content: `Summarize this session so far:\n\n${transcript.slice(-400_000)}` },
|
|
99
|
+
];
|
|
100
|
+
}
|
|
101
|
+
/** Step 2's result: the system prompt, the goal with the summary, then the kept tail. */
|
|
102
|
+
export function withSummary(messages, cut, summary) {
|
|
103
|
+
const goal = (messages[1]?.content ?? '').slice(0, 20_000);
|
|
104
|
+
const head = [
|
|
105
|
+
messages[0],
|
|
106
|
+
{
|
|
107
|
+
role: 'user',
|
|
108
|
+
content: `${goal}\n\n---\nThe conversation after this request was summarized to save space. ` +
|
|
109
|
+
`Summary of the work so far:\n\n${summary.trim()}`,
|
|
110
|
+
},
|
|
111
|
+
];
|
|
112
|
+
// Two owner messages in a row read as one; an acknowledgement keeps them apart.
|
|
113
|
+
if (messages[cut]?.role === 'user')
|
|
114
|
+
head.push({ role: 'assistant', content: 'Understood. Continuing from the summary.' });
|
|
115
|
+
return [...head, ...messages.slice(cut)];
|
|
116
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { Ask, AskResult, Engine, EngineEmit, EngineOutcome, EngineSessionState, Place, RunBrief, RunnerCommand } from '@workser/runtime';
|
|
2
|
+
import { CoreSession, type CoreSessionOptions } from './session.js';
|
|
3
|
+
import { type KeyRelay } from './tools/relay.js';
|
|
4
|
+
import { type ToolsClient } from './tools/workser-tool.js';
|
|
5
|
+
export interface CoreEngineOptions {
|
|
6
|
+
modelBaseUrl: string;
|
|
7
|
+
modelKey: string | (() => string);
|
|
8
|
+
model?: string;
|
|
9
|
+
ask?: (ask: Ask) => Promise<AskResult>;
|
|
10
|
+
fetch?: typeof fetch;
|
|
11
|
+
maxSteps?: number;
|
|
12
|
+
onCheckpoint?: (state: EngineSessionState) => Promise<void> | void;
|
|
13
|
+
mode?: 'task' | 'session';
|
|
14
|
+
onTurnEnded?: (summary: string) => Promise<void> | void;
|
|
15
|
+
stream?: boolean;
|
|
16
|
+
budget?: CoreSessionOptions['budget'];
|
|
17
|
+
askFor?: string[];
|
|
18
|
+
extraTools?: CoreSessionOptions['extraTools'];
|
|
19
|
+
extraPrompt?: string;
|
|
20
|
+
/** Variables (names, ids; never keys) on every command the loop runs. */
|
|
21
|
+
commandEnv?: Record<string, string>;
|
|
22
|
+
}
|
|
23
|
+
/** The first prompt of a job, from its brief (same fields the runner reads). */
|
|
24
|
+
export declare function promptOf(brief: RunBrief): string;
|
|
25
|
+
/**
|
|
26
|
+
* The SDK `Engine` for the loop-in-our-service mode, so `workser-runner` (or
|
|
27
|
+
* a Trigger.dev worker) can use it wherever it uses `OpenCodeEngine` today.
|
|
28
|
+
*/
|
|
29
|
+
export declare class CoreEngine implements Engine {
|
|
30
|
+
private readonly options;
|
|
31
|
+
readonly name = "workser-core";
|
|
32
|
+
constructor(options: CoreEngineOptions);
|
|
33
|
+
start(brief: RunBrief, place: Place, emit: EngineEmit): Promise<CoreSession>;
|
|
34
|
+
/**
|
|
35
|
+
* The session as it was saved. It does not go on by itself: call
|
|
36
|
+
* `continue()` to pick up a job that was cut off, or `send()` a new message.
|
|
37
|
+
*/
|
|
38
|
+
resume(state: EngineSessionState, place: Place, emit: EngineEmit, brief?: RunBrief): Promise<CoreSession>;
|
|
39
|
+
private begin;
|
|
40
|
+
}
|
|
41
|
+
/** Anything with the RunnerClient methods the engine uses. */
|
|
42
|
+
export interface RunnerLike extends Partial<ToolsClient> {
|
|
43
|
+
emit(type: string, data?: Record<string, unknown>): void;
|
|
44
|
+
ask(ask: Ask): Promise<AskResult>;
|
|
45
|
+
commands(handler: (command: RunnerCommand) => void | Promise<void>): Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
export interface RunEngineInput {
|
|
48
|
+
brief: RunBrief;
|
|
49
|
+
place: Place;
|
|
50
|
+
runnerClient: RunnerLike;
|
|
51
|
+
modelBaseUrl: string;
|
|
52
|
+
modelKey: string | (() => string);
|
|
53
|
+
model?: string;
|
|
54
|
+
fetch?: typeof fetch;
|
|
55
|
+
maxSteps?: number;
|
|
56
|
+
state?: EngineSessionState;
|
|
57
|
+
onCheckpoint?: (state: EngineSessionState) => Promise<void> | void;
|
|
58
|
+
/** Listen to the run's commands here (default true). Off when a runner already does. */
|
|
59
|
+
listenForCommands?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* The sandbox key relay's sealed grant (from the dispatch payload's
|
|
62
|
+
* `key_relay`), for a loop that runs outside the machine. The grant stays in
|
|
63
|
+
* this process: the agent gets a `vault_fetch` tool, never the grant or a key.
|
|
64
|
+
*/
|
|
65
|
+
relay?: KeyRelay | null;
|
|
66
|
+
/** Which tool permissions ask the owner (default `['bash']`). */
|
|
67
|
+
askFor?: string[];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The headless entry: one job, the loop here, the files on `place`, events
|
|
71
|
+
* and asks through the run's RunnerClient. Resolves with how the job ended;
|
|
72
|
+
* the caller sends the terminal event (`runnerClient.finish`).
|
|
73
|
+
*/
|
|
74
|
+
export declare function runEngine(input: RunEngineInput): Promise<{
|
|
75
|
+
outcome: EngineOutcome;
|
|
76
|
+
state: EngineSessionState;
|
|
77
|
+
}>;
|
|
78
|
+
/**
|
|
79
|
+
* What a loop running OUTSIDE the machine gets in place of the runner's
|
|
80
|
+
* broker: `vault_fetch` when the job has a key relay, `workser_tool` when the
|
|
81
|
+
* client can call the run's tools, and ids and key NAMES (never a key) in
|
|
82
|
+
* every command's environment, as the runner path sets them.
|
|
83
|
+
*/
|
|
84
|
+
export declare function serviceExtras(input: Pick<RunEngineInput, 'brief' | 'runnerClient' | 'relay' | 'fetch' | 'askFor'>): Partial<CoreEngineOptions>;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { CoreSession, ENGINE_NAME } from './session.js';
|
|
2
|
+
import { withCommandEnv } from './place/env.js';
|
|
3
|
+
import { vaultFetchTool } from './tools/relay.js';
|
|
4
|
+
import { workserTool } from './tools/workser-tool.js';
|
|
5
|
+
/** The first prompt of a job, from its brief (same fields the runner reads). */
|
|
6
|
+
export function promptOf(brief) {
|
|
7
|
+
const input = brief.run.input ?? {};
|
|
8
|
+
for (const key of ['prompt', 'message', 'text', 'goal']) {
|
|
9
|
+
const value = input[key];
|
|
10
|
+
if (typeof value === 'string' && value.trim())
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
return 'Continue the task described in this run.';
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The SDK `Engine` for the loop-in-our-service mode, so `workser-runner` (or
|
|
17
|
+
* a Trigger.dev worker) can use it wherever it uses `OpenCodeEngine` today.
|
|
18
|
+
*/
|
|
19
|
+
export class CoreEngine {
|
|
20
|
+
options;
|
|
21
|
+
name = ENGINE_NAME;
|
|
22
|
+
constructor(options) {
|
|
23
|
+
this.options = options;
|
|
24
|
+
}
|
|
25
|
+
async start(brief, place, emit) {
|
|
26
|
+
return this.begin(brief, place, emit, undefined, promptOf(brief));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The session as it was saved. It does not go on by itself: call
|
|
30
|
+
* `continue()` to pick up a job that was cut off, or `send()` a new message.
|
|
31
|
+
*/
|
|
32
|
+
async resume(state, place, emit, brief) {
|
|
33
|
+
if (state.engine !== ENGINE_NAME)
|
|
34
|
+
throw new Error(`This session belongs to engine "${state.engine}".`);
|
|
35
|
+
const fallback = brief ?? {
|
|
36
|
+
protocol: 1,
|
|
37
|
+
run: {
|
|
38
|
+
id: '', status: 'RUNNING', agent_id: '', session_id: '', project_id: null, project_task_id: null,
|
|
39
|
+
lane: 'workbench', place: null, kind: null, budget_credits: null, parent_run_id: null, input: {},
|
|
40
|
+
},
|
|
41
|
+
cursors: { runner_seq: 0, event_pos: 0, command_pos: 0 },
|
|
42
|
+
};
|
|
43
|
+
return this.begin(fallback, place, emit, state, null);
|
|
44
|
+
}
|
|
45
|
+
async begin(brief, place, emit, state, firstPrompt) {
|
|
46
|
+
const session = new CoreSession({
|
|
47
|
+
brief,
|
|
48
|
+
place: this.options.commandEnv ? withCommandEnv(place, this.options.commandEnv) : place,
|
|
49
|
+
emit,
|
|
50
|
+
modelBaseUrl: this.options.modelBaseUrl,
|
|
51
|
+
modelKey: this.options.modelKey,
|
|
52
|
+
...(this.options.model ? { model: this.options.model } : {}),
|
|
53
|
+
...(this.options.ask ? { ask: this.options.ask } : {}),
|
|
54
|
+
...(this.options.fetch ? { fetch: this.options.fetch } : {}),
|
|
55
|
+
...(this.options.maxSteps ? { maxSteps: this.options.maxSteps } : {}),
|
|
56
|
+
...(this.options.onCheckpoint ? { onCheckpoint: this.options.onCheckpoint } : {}),
|
|
57
|
+
...(this.options.mode ? { mode: this.options.mode } : {}),
|
|
58
|
+
...(this.options.onTurnEnded ? { onTurnEnded: this.options.onTurnEnded } : {}),
|
|
59
|
+
...(this.options.stream !== undefined ? { stream: this.options.stream } : {}),
|
|
60
|
+
...(this.options.budget ? { budget: this.options.budget } : {}),
|
|
61
|
+
...(this.options.askFor ? { askFor: this.options.askFor } : {}),
|
|
62
|
+
...(this.options.extraTools?.length ? { extraTools: this.options.extraTools } : {}),
|
|
63
|
+
...(this.options.extraPrompt ? { extraPrompt: this.options.extraPrompt } : {}),
|
|
64
|
+
...(state ? { state } : {}),
|
|
65
|
+
});
|
|
66
|
+
await session.open();
|
|
67
|
+
// Task mode: the job completes when the prompt's turns end and nothing is
|
|
68
|
+
// queued. Session mode: each prompt is one turn (see CoreSession).
|
|
69
|
+
if (firstPrompt !== null)
|
|
70
|
+
void session.prompt(firstPrompt);
|
|
71
|
+
return session;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The headless entry: one job, the loop here, the files on `place`, events
|
|
76
|
+
* and asks through the run's RunnerClient. Resolves with how the job ended;
|
|
77
|
+
* the caller sends the terminal event (`runnerClient.finish`).
|
|
78
|
+
*/
|
|
79
|
+
export async function runEngine(input) {
|
|
80
|
+
const engine = new CoreEngine({
|
|
81
|
+
modelBaseUrl: input.modelBaseUrl,
|
|
82
|
+
modelKey: input.modelKey,
|
|
83
|
+
ask: (ask) => input.runnerClient.ask(ask),
|
|
84
|
+
...serviceExtras(input),
|
|
85
|
+
...(input.model ? { model: input.model } : {}),
|
|
86
|
+
...(input.fetch ? { fetch: input.fetch } : {}),
|
|
87
|
+
...(input.maxSteps ? { maxSteps: input.maxSteps } : {}),
|
|
88
|
+
...(input.onCheckpoint ? { onCheckpoint: input.onCheckpoint } : {}),
|
|
89
|
+
});
|
|
90
|
+
const emit = (type, data) => input.runnerClient.emit(type, data ?? {});
|
|
91
|
+
let session;
|
|
92
|
+
if (input.state) {
|
|
93
|
+
session = await engine.resume(input.state, input.place, emit, input.brief);
|
|
94
|
+
void session.continue();
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
session = await engine.start(input.brief, input.place, emit);
|
|
98
|
+
}
|
|
99
|
+
if (input.listenForCommands !== false) {
|
|
100
|
+
void input.runnerClient.commands(async (command) => {
|
|
101
|
+
if (command.kind === 'cancel')
|
|
102
|
+
await session.stop(String(command.payload.reason ?? 'cancelled'));
|
|
103
|
+
else
|
|
104
|
+
await session.answer(command);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const outcome = await session.done;
|
|
108
|
+
return { outcome, state: await session.save() };
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* What a loop running OUTSIDE the machine gets in place of the runner's
|
|
112
|
+
* broker: `vault_fetch` when the job has a key relay, `workser_tool` when the
|
|
113
|
+
* client can call the run's tools, and ids and key NAMES (never a key) in
|
|
114
|
+
* every command's environment, as the runner path sets them.
|
|
115
|
+
*/
|
|
116
|
+
export function serviceExtras(input) {
|
|
117
|
+
const extraTools = [];
|
|
118
|
+
const notes = [];
|
|
119
|
+
const env = {};
|
|
120
|
+
if (input.brief.run.id)
|
|
121
|
+
env.WORKSER_RUN_ID = input.brief.run.id;
|
|
122
|
+
const relay = input.relay;
|
|
123
|
+
if (relay?.url && relay.grant && relay.names.length) {
|
|
124
|
+
extraTools.push(vaultFetchTool(relay, input.fetch));
|
|
125
|
+
env.WORKSER_VAULT_RELAY_NAMES = relay.names.join(',');
|
|
126
|
+
notes.push(`API keys: this job can use the business's saved keys (${relay.names.join(', ')}) only through the vault_fetch tool, writing {{vault:NAME}} where a key goes. ` +
|
|
127
|
+
'You never see a key; do not look for one in the environment or put one in a command.');
|
|
128
|
+
}
|
|
129
|
+
const client = input.runnerClient;
|
|
130
|
+
if (typeof client.listTools === 'function' && typeof client.callTool === 'function') {
|
|
131
|
+
extraTools.push(workserTool(client));
|
|
132
|
+
notes.push("Workser's tools and the project's connected apps are available through the workser_tool tool (action list, then call). " +
|
|
133
|
+
'If your list has inbox_read, sms_read and account_save, you have your own email address or number: use them to sign up to a service for the work you were assigned. ' +
|
|
134
|
+
'Ask the owner first (the first call does), sign up with your own address or number only, wait for the code, then save the account.');
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
...(extraTools.length ? { extraTools } : {}),
|
|
138
|
+
...(notes.length ? { extraPrompt: notes.join('\n\n') } : {}),
|
|
139
|
+
...(Object.keys(env).length ? { commandEnv: env } : {}),
|
|
140
|
+
...(input.askFor ? { askFor: input.askFor } : {}),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { CoreEngine, runEngine, promptOf, serviceExtras, type CoreEngineOptions, type RunEngineInput, type RunnerLike } from './engine.js';
|
|
2
|
+
export { CoreSession, ENGINE_NAME, type CoreSessionOptions } from './session.js';
|
|
3
|
+
export { LocalPlace } from './place/local.js';
|
|
4
|
+
export { E2BPlace, connectE2BPlace, type E2BSandboxLike } from './place/e2b.js';
|
|
5
|
+
export { OPENCODE_VERSION } from './vendor.js';
|
|
6
|
+
export { providerPrompt, environmentPrompt, bashDescription } from './prompts.js';
|
|
7
|
+
export { needsAsk } from './tools/shell.js';
|
|
8
|
+
export { ShellSession } from './tools/shell.js';
|
|
9
|
+
export { applyPatchTool, parsePatch, applyChunks, usesPatch, PatchError } from './tools/patch.js';
|
|
10
|
+
export { DEFAULT_BUDGET, estimateTokens, pruneToolOutputs, cutPoint, withSummary, PRUNED_NOTE, type CompactionBudget, } from './compact.js';
|
|
11
|
+
export { vaultFetchTool, type KeyRelay } from './tools/relay.js';
|
|
12
|
+
export { workserTool, type ToolsClient } from './tools/workser-tool.js';
|
|
13
|
+
export { withCommandEnv } from './place/env.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { CoreEngine, runEngine, promptOf, serviceExtras } from './engine.js';
|
|
2
|
+
export { CoreSession, ENGINE_NAME } from './session.js';
|
|
3
|
+
export { LocalPlace } from './place/local.js';
|
|
4
|
+
export { E2BPlace, connectE2BPlace } from './place/e2b.js';
|
|
5
|
+
export { OPENCODE_VERSION } from './vendor.js';
|
|
6
|
+
export { providerPrompt, environmentPrompt, bashDescription } from './prompts.js';
|
|
7
|
+
export { needsAsk } from './tools/shell.js';
|
|
8
|
+
export { ShellSession } from './tools/shell.js';
|
|
9
|
+
export { applyPatchTool, parsePatch, applyChunks, usesPatch, PatchError } from './tools/patch.js';
|
|
10
|
+
export { DEFAULT_BUDGET, estimateTokens, pruneToolOutputs, cutPoint, withSummary, PRUNED_NOTE, } from './compact.js';
|
|
11
|
+
export { vaultFetchTool } from './tools/relay.js';
|
|
12
|
+
export { workserTool } from './tools/workser-tool.js';
|
|
13
|
+
export { withCommandEnv } from './place/env.js';
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An OpenAI-compatible chat-completions client — Workser's gateway speaks
|
|
3
|
+
* this, and the key is the RUN token (the gateway books every call against
|
|
4
|
+
* the run). Nothing else: no provider key is known to the engine.
|
|
5
|
+
*/
|
|
6
|
+
export interface ChatMessage {
|
|
7
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
8
|
+
content: string | null;
|
|
9
|
+
tool_calls?: ToolCall[];
|
|
10
|
+
tool_call_id?: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface ToolCall {
|
|
14
|
+
id: string;
|
|
15
|
+
type: 'function';
|
|
16
|
+
function: {
|
|
17
|
+
name: string;
|
|
18
|
+
arguments: string;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export interface ChatTool {
|
|
22
|
+
type: 'function';
|
|
23
|
+
function: {
|
|
24
|
+
name: string;
|
|
25
|
+
description: string;
|
|
26
|
+
parameters: Record<string, unknown>;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export interface ChatResult {
|
|
30
|
+
message: ChatMessage;
|
|
31
|
+
finishReason: string | null;
|
|
32
|
+
usage?: {
|
|
33
|
+
prompt_tokens?: number;
|
|
34
|
+
completion_tokens?: number;
|
|
35
|
+
cost?: number;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export declare class ModelError extends Error {
|
|
39
|
+
readonly status: number;
|
|
40
|
+
constructor(message: string, status: number);
|
|
41
|
+
}
|
|
42
|
+
export declare function chat(input: {
|
|
43
|
+
baseUrl: string;
|
|
44
|
+
key: string;
|
|
45
|
+
model: string;
|
|
46
|
+
messages: ChatMessage[];
|
|
47
|
+
tools: ChatTool[];
|
|
48
|
+
fetch?: typeof fetch;
|
|
49
|
+
signal?: AbortSignal;
|
|
50
|
+
/**
|
|
51
|
+
* Stream the reply: called with each piece of text as the model writes it.
|
|
52
|
+
* The result is the same whole message either way; a route that answers
|
|
53
|
+
* with plain JSON instead of a stream is read as before.
|
|
54
|
+
*/
|
|
55
|
+
onDelta?: (text: string) => void;
|
|
56
|
+
}): Promise<ChatResult>;
|