omsago 0.0.0-stage → 1.0.4
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/LICENSE +21 -0
- package/README.md +113 -2
- package/dist/client/discovery.d.ts +4 -0
- package/dist/client/discovery.js +133 -0
- package/dist/client/errors.d.ts +8 -0
- package/dist/client/errors.js +20 -0
- package/dist/client/events.d.ts +6 -0
- package/dist/client/events.js +192 -0
- package/dist/client/index.d.ts +15 -0
- package/dist/client/index.js +70 -0
- package/dist/client/messages.d.ts +4 -0
- package/dist/client/messages.js +186 -0
- package/dist/client/prompts.d.ts +3 -0
- package/dist/client/prompts.js +231 -0
- package/dist/client/requests.d.ts +100 -0
- package/dist/client/requests.js +81 -0
- package/dist/client/session-routes.d.ts +3 -0
- package/dist/client/session-routes.js +94 -0
- package/dist/client/sessions.d.ts +6 -0
- package/dist/client/sessions.js +29 -0
- package/dist/client/state.d.ts +26 -0
- package/dist/client/state.js +52 -0
- package/dist/client/tool-parts.d.ts +3 -0
- package/dist/client/tool-parts.js +51 -0
- package/dist/domains/agents.d.ts +3 -0
- package/dist/domains/agents.js +58 -0
- package/dist/domains/commands.d.ts +4 -0
- package/dist/domains/commands.js +65 -0
- package/dist/domains/index.d.ts +4 -0
- package/dist/domains/index.js +102 -0
- package/dist/domains/mcp.d.ts +3 -0
- package/dist/domains/mcp.js +79 -0
- package/dist/domains/permissions.d.ts +6 -0
- package/dist/domains/permissions.js +33 -0
- package/dist/domains/prompt.d.ts +5 -0
- package/dist/domains/prompt.js +32 -0
- package/dist/domains/tools.d.ts +5 -0
- package/dist/domains/tools.js +150 -0
- package/dist/domains/value.d.ts +11 -0
- package/dist/domains/value.js +49 -0
- package/dist/enums.d.ts +205 -0
- package/dist/enums.js +187 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +90 -0
- package/dist/session/index.d.ts +4 -0
- package/dist/session/index.js +133 -0
- package/dist/session/message-restore.d.ts +9 -0
- package/dist/session/message-restore.js +176 -0
- package/dist/session/messages.d.ts +3 -0
- package/dist/session/messages.js +222 -0
- package/dist/session/model.d.ts +6 -0
- package/dist/session/model.js +86 -0
- package/dist/session/prompt.d.ts +4 -0
- package/dist/session/prompt.js +115 -0
- package/dist/tui.d.ts +3 -0
- package/dist/tui.js +17 -0
- package/dist/user-discovery.d.ts +13 -0
- package/dist/user-discovery.js +117 -0
- package/package.json +77 -4
- package/scripts/postinstall.mjs +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 iworkforces
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,114 @@
|
|
|
1
|
-
#
|
|
1
|
+
# oh-my-sago
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`oh-my-sago` (`omsago`) is an OpenCode v2 server plugin that runs the installed
|
|
4
|
+
`oh-my-openagent` runtime through a compatibility bridge. It reuses OmO's agents,
|
|
5
|
+
prompts, tools, hooks, and managers rather than maintaining copies of them.
|
|
6
|
+
|
|
7
|
+
## Local installation
|
|
8
|
+
|
|
9
|
+
Use Bun 1.4.4 or newer and an existing OpenCode 2.3.0 installation:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
bun install
|
|
13
|
+
bun run build
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The install command skips dependency lifecycle scripts. The test harness resolves
|
|
17
|
+
the installed native OpenCode executable directly and does not require its package
|
|
18
|
+
shim's postinstall step.
|
|
19
|
+
|
|
20
|
+
Add this repository's **directory**, after building it, to your OpenCode v2 config:
|
|
21
|
+
|
|
22
|
+
```json
|
|
23
|
+
{
|
|
24
|
+
"$schema": "https://opencode.ai/config.json",
|
|
25
|
+
"plugins": ["omsago@latest"]
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Replace the example path with your checkout. Do not load `oh-my-openagent` or
|
|
30
|
+
`oh-my-opencode` alongside `omsago`; `omsago` starts OmO itself. Quit and restart
|
|
31
|
+
OpenCode after changing the plugin configuration. No global configuration is
|
|
32
|
+
changed by the build or tests, and this package has not been published by these
|
|
33
|
+
instructions.
|
|
34
|
+
|
|
35
|
+
OmO retains ownership of its own configuration and migrations. In its shared
|
|
36
|
+
`.omo/omo.json` or `.omo/omo.jsonc`, OpenCode engine settings belong under the
|
|
37
|
+
literal `[opencode]` key. For example:
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"[opencode]": {
|
|
42
|
+
"agents": {
|
|
43
|
+
"sisyphus": { "model": "openai/gpt-6.2-astra" }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Select a model available in your OpenCode installation. Provider credentials stay
|
|
50
|
+
with OpenCode; the bridge does not implement another authentication system.
|
|
51
|
+
|
|
52
|
+
## Implemented server behavior
|
|
53
|
+
|
|
54
|
+
- The public OmO `omoPlugin` factory supplies agents, commands, MCP configuration,
|
|
55
|
+
custom tools, and lifecycle cleanup.
|
|
56
|
+
- Native domain registrations translate tool schemas, tool input/output hooks,
|
|
57
|
+
permission checks, shell environment hooks, and legacy configuration changes.
|
|
58
|
+
Tool execution still uses the original OmO validators.
|
|
59
|
+
- Session hooks adapt prompts, message and system transformations, generation
|
|
60
|
+
parameters, request headers, and compaction context. Unchanged native media,
|
|
61
|
+
signed reasoning, and provider metadata are retained.
|
|
62
|
+
- A generated legacy SDK client routes discovery, session operations, prompts,
|
|
63
|
+
cancellation, status, messages, and compaction to native v2 APIs. Native events
|
|
64
|
+
are translated for OmO's existing managers.
|
|
65
|
+
|
|
66
|
+
The native CLI integration suite exercises a real OmO agent turn and skill,
|
|
67
|
+
protected writes, synchronous delegation, background completion and failure,
|
|
68
|
+
interruption, and replacement/unload/reload. Models are deterministic local
|
|
69
|
+
fixtures; the tests do not use credentials or call a model service. A failed
|
|
70
|
+
background model can take about 30 seconds to settle because OmO's existing
|
|
71
|
+
manager confirms generic idle errors over multiple observations.
|
|
72
|
+
|
|
73
|
+
## Compatibility boundaries
|
|
74
|
+
|
|
75
|
+
There is no OmO TUI/sidebar export. Optional integrations such as workspace
|
|
76
|
+
adapters, tmux attachment, and UI notifications are not ported by this milestone.
|
|
77
|
+
|
|
78
|
+
The client facade is deliberately limited to implemented routes. Unsupported
|
|
79
|
+
legacy calls return an error, usually HTTP 501, rather than an empty successful
|
|
80
|
+
response. In particular, the legacy session-todo endpoint is unavailable: the v2
|
|
81
|
+
plugin context has no authoritative current-todo read API. Legacy history is
|
|
82
|
+
projected from the native context window, so it does not expose the full
|
|
83
|
+
pre-compaction archive. The legacy configuration view is a domain-derived
|
|
84
|
+
snapshot, not the original merged v1 config object.
|
|
85
|
+
|
|
86
|
+
Commands support text templates, `$ARGUMENTS`, positional arguments, and
|
|
87
|
+
agent/model selection. Legacy command subtasks and shell/file expansion fail
|
|
88
|
+
explicitly. Native v2 features without a legacy equivalent, and legacy hooks
|
|
89
|
+
without a v2 equivalent such as compaction autocontinue, are not claimed as
|
|
90
|
+
compatible.
|
|
91
|
+
|
|
92
|
+
Development retains Effect 4.1.4. OpenCode supplies its own Effect and plugin
|
|
93
|
+
runtime instances when loading the plugin; do not bundle a second runtime into
|
|
94
|
+
the plugin. The integration suite uses that native loader rather than importing
|
|
95
|
+
an embedded host with a different dependency graph.
|
|
96
|
+
|
|
97
|
+
## Development
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
bun run typecheck
|
|
101
|
+
bun run lint
|
|
102
|
+
bun run format:check
|
|
103
|
+
bun run build
|
|
104
|
+
bun test
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Tests isolate HOME, XDG directories, workspaces, subprocesses, and ephemeral
|
|
108
|
+
localhost ports. They close owned servers and remove temporary scenario roots.
|
|
109
|
+
`dist/` is generated by the build; the package exports both its root and `/server`
|
|
110
|
+
entry points from `dist/index.js`.
|
|
111
|
+
|
|
112
|
+
The bridge is MIT-licensed. `oh-my-openagent` remains a separate dependency under
|
|
113
|
+
its [Sustainable Use License](https://github.com/code-yeongyu/oh-my-openagent/blob/dev/LICENSE.md);
|
|
114
|
+
this repository's license does not relicense upstream code.
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode/plugin";
|
|
2
|
+
import type { Project } from "@opencode-ai/sdk";
|
|
3
|
+
export declare function currentProject(context: Plugin.Context): Project;
|
|
4
|
+
export declare function discoveryRoute(context: Plugin.Context, request: Request): Promise<Response>;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { NativeAction, PermissionEffect } from "../enums.js";
|
|
2
|
+
import { unsupported } from "./errors.js";
|
|
3
|
+
export function currentProject(context) {
|
|
4
|
+
return {
|
|
5
|
+
id: context.location.project.id,
|
|
6
|
+
worktree: context.location.project.directory,
|
|
7
|
+
time: { created: 0 },
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
function legacyAgent(agent) {
|
|
11
|
+
const effect = (name) => agent.permissions.findLast((rule) => rule.action === name && rule.resource === "*")?.effect ??
|
|
12
|
+
PermissionEffect.Ask;
|
|
13
|
+
return {
|
|
14
|
+
name: agent.id,
|
|
15
|
+
mode: agent.mode,
|
|
16
|
+
builtIn: false,
|
|
17
|
+
hidden: agent.hidden,
|
|
18
|
+
permission: {
|
|
19
|
+
edit: effect(NativeAction.Edit),
|
|
20
|
+
bash: Object.fromEntries(agent.permissions
|
|
21
|
+
.filter((rule) => rule.action === NativeAction.Shell)
|
|
22
|
+
.map((rule) => [rule.resource, rule.effect])),
|
|
23
|
+
},
|
|
24
|
+
tools: Object.fromEntries(agent.permissions
|
|
25
|
+
.filter((rule) => rule.resource === "*")
|
|
26
|
+
.map((rule) => [rule.action, rule.effect !== PermissionEffect.Deny])),
|
|
27
|
+
options: { ...agent.request.settings, ...agent.request.body },
|
|
28
|
+
...(agent.description === undefined ? {} : { description: agent.description }),
|
|
29
|
+
...(agent.system === undefined ? {} : { prompt: agent.system }),
|
|
30
|
+
...(agent.steps === undefined ? {} : { maxSteps: agent.steps }),
|
|
31
|
+
...(agent.model === undefined
|
|
32
|
+
? {}
|
|
33
|
+
: {
|
|
34
|
+
model: { providerID: agent.model.providerID, modelID: agent.model.id },
|
|
35
|
+
...(agent.model.variant === undefined ? {} : { variant: agent.model.variant }),
|
|
36
|
+
}),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function providerModel(model) {
|
|
40
|
+
const cost = model.cost.find((tier) => tier.tier === undefined);
|
|
41
|
+
return {
|
|
42
|
+
id: model.id,
|
|
43
|
+
name: model.name,
|
|
44
|
+
release_date: new Date(model.time.released).toISOString().slice(0, 10),
|
|
45
|
+
attachment: model.capabilities.input.some((input) => input !== "text"),
|
|
46
|
+
reasoning: model.variants.length > 0,
|
|
47
|
+
temperature: model.capabilities.temperature === true,
|
|
48
|
+
tool_call: model.capabilities.tools,
|
|
49
|
+
limit: model.limit,
|
|
50
|
+
status: model.status,
|
|
51
|
+
options: { ...model.settings, ...model.body },
|
|
52
|
+
headers: model.headers ?? {},
|
|
53
|
+
variants: Object.fromEntries(model.variants.map((variant) => [variant.id, { ...variant.settings, ...variant.body }])),
|
|
54
|
+
...(cost === undefined
|
|
55
|
+
? {}
|
|
56
|
+
: {
|
|
57
|
+
cost: {
|
|
58
|
+
input: cost.input,
|
|
59
|
+
output: cost.output,
|
|
60
|
+
cache_read: cost.cache.read,
|
|
61
|
+
cache_write: cost.cache.write,
|
|
62
|
+
},
|
|
63
|
+
}),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export async function discoveryRoute(context, request) {
|
|
67
|
+
const url = new URL(request.url);
|
|
68
|
+
const location = { directory: url.searchParams.get("directory") ?? context.location.directory };
|
|
69
|
+
const options = { signal: request.signal };
|
|
70
|
+
if (request.method !== "GET")
|
|
71
|
+
return unsupported(`${request.method} ${url.pathname}`);
|
|
72
|
+
switch (url.pathname) {
|
|
73
|
+
case "/project/current":
|
|
74
|
+
return Response.json(currentProject(context));
|
|
75
|
+
case "/agent":
|
|
76
|
+
return Response.json((await context.agent.list({ location }, options)).data.map(legacyAgent));
|
|
77
|
+
case "/skill":
|
|
78
|
+
return Response.json((await context.skill.list({ location }, options)).data.map((skill) => ({
|
|
79
|
+
name: skill.name,
|
|
80
|
+
description: skill.description ?? "",
|
|
81
|
+
location: skill.path,
|
|
82
|
+
content: skill.content,
|
|
83
|
+
})));
|
|
84
|
+
case "/config": {
|
|
85
|
+
const selected = (await context.model.default({ location }, options)).data;
|
|
86
|
+
const agents = (await context.agent.list({ location }, options)).data;
|
|
87
|
+
const config = {
|
|
88
|
+
agent: Object.fromEntries(agents.map((agent) => [
|
|
89
|
+
agent.id,
|
|
90
|
+
{
|
|
91
|
+
...(agent.system === undefined ? {} : { prompt: agent.system }),
|
|
92
|
+
mode: agent.mode,
|
|
93
|
+
...(agent.model === undefined
|
|
94
|
+
? {}
|
|
95
|
+
: { model: `${agent.model.providerID}/${agent.model.id}` }),
|
|
96
|
+
...(agent.description === undefined ? {} : { description: agent.description }),
|
|
97
|
+
},
|
|
98
|
+
])),
|
|
99
|
+
...(selected === null ? {} : { model: `${selected.providerID}/${selected.id}` }),
|
|
100
|
+
};
|
|
101
|
+
return Response.json(config);
|
|
102
|
+
}
|
|
103
|
+
case "/provider": {
|
|
104
|
+
const providers = (await context.provider.list({ location }, options)).data;
|
|
105
|
+
const models = (await context.model.list({ location }, options)).data;
|
|
106
|
+
const selected = (await context.model.default({ location }, options)).data;
|
|
107
|
+
const result = {
|
|
108
|
+
all: providers.map((provider) => ({
|
|
109
|
+
id: provider.id,
|
|
110
|
+
name: provider.name,
|
|
111
|
+
env: [],
|
|
112
|
+
npm: provider.package,
|
|
113
|
+
models: Object.fromEntries(models
|
|
114
|
+
.filter((model) => model.providerID === provider.id)
|
|
115
|
+
.map((model) => [model.id, providerModel(model)])),
|
|
116
|
+
})),
|
|
117
|
+
connected: providers
|
|
118
|
+
.filter((provider) => provider.activation !== "disabled" &&
|
|
119
|
+
models.some((model) => model.providerID === provider.id && model.enabled))
|
|
120
|
+
.map((provider) => provider.id),
|
|
121
|
+
default: Object.fromEntries(providers.flatMap((provider) => {
|
|
122
|
+
const model = selected?.providerID === provider.id
|
|
123
|
+
? selected
|
|
124
|
+
: models.find((candidate) => candidate.providerID === provider.id && candidate.enabled);
|
|
125
|
+
return model === undefined ? [] : [[provider.id, model.id]];
|
|
126
|
+
})),
|
|
127
|
+
};
|
|
128
|
+
return Response.json(result);
|
|
129
|
+
}
|
|
130
|
+
default:
|
|
131
|
+
return unsupported(`${request.method} ${url.pathname}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare class BridgeError extends Error {
|
|
2
|
+
readonly status: number;
|
|
3
|
+
readonly name: "BridgeError";
|
|
4
|
+
constructor(status: number, message: string);
|
|
5
|
+
}
|
|
6
|
+
export declare function unsupported(operation: string): never;
|
|
7
|
+
export declare function assertNever(value: never): never;
|
|
8
|
+
export declare function errorResponse(error: unknown): Response;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ErrorName } from "../enums.js";
|
|
2
|
+
export class BridgeError extends Error {
|
|
3
|
+
status;
|
|
4
|
+
name = ErrorName.Bridge;
|
|
5
|
+
constructor(status, message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.status = status;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function unsupported(operation) {
|
|
11
|
+
throw new BridgeError(501, `Legacy operation is unsupported: ${operation}`);
|
|
12
|
+
}
|
|
13
|
+
export function assertNever(value) {
|
|
14
|
+
throw new BridgeError(500, `Unexpected native variant: ${String(value)}`);
|
|
15
|
+
}
|
|
16
|
+
export function errorResponse(error) {
|
|
17
|
+
if (!(error instanceof Error))
|
|
18
|
+
throw error;
|
|
19
|
+
return Response.json({ name: ErrorName.BadRequest, data: { message: error.message } }, { status: error instanceof BridgeError ? error.status : 500 });
|
|
20
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { ErrorName, LegacyEventType, SessionEventType, SessionStatusKind } from "../enums.js";
|
|
2
|
+
import { BridgeError, assertNever } from "./errors.js";
|
|
3
|
+
import { legacyError, legacyMessages } from "./messages.js";
|
|
4
|
+
import { legacySession, listSessions } from "./sessions.js";
|
|
5
|
+
const types = [
|
|
6
|
+
SessionEventType.Created,
|
|
7
|
+
SessionEventType.Deleted,
|
|
8
|
+
SessionEventType.Renamed,
|
|
9
|
+
SessionEventType.Moved,
|
|
10
|
+
SessionEventType.AgentSelected,
|
|
11
|
+
SessionEventType.ModelSelected,
|
|
12
|
+
SessionEventType.MetadataUpdated,
|
|
13
|
+
SessionEventType.Permissions,
|
|
14
|
+
SessionEventType.Archived,
|
|
15
|
+
SessionEventType.Unarchived,
|
|
16
|
+
SessionEventType.ExecutionStarted,
|
|
17
|
+
SessionEventType.ExecutionSucceeded,
|
|
18
|
+
SessionEventType.ExecutionFailed,
|
|
19
|
+
SessionEventType.ExecutionInterrupted,
|
|
20
|
+
SessionEventType.InboxDelivered,
|
|
21
|
+
SessionEventType.Synthetic,
|
|
22
|
+
SessionEventType.StepStarted,
|
|
23
|
+
SessionEventType.StepEnded,
|
|
24
|
+
SessionEventType.StepFailed,
|
|
25
|
+
SessionEventType.TextDelta,
|
|
26
|
+
SessionEventType.TextEnded,
|
|
27
|
+
SessionEventType.ReasoningDelta,
|
|
28
|
+
SessionEventType.ReasoningEnded,
|
|
29
|
+
SessionEventType.ToolCalled,
|
|
30
|
+
SessionEventType.ToolProgress,
|
|
31
|
+
SessionEventType.ToolSuccess,
|
|
32
|
+
SessionEventType.ToolFailed,
|
|
33
|
+
];
|
|
34
|
+
export function clientEvents(context) {
|
|
35
|
+
const controller = new AbortController();
|
|
36
|
+
const sessions = new Map();
|
|
37
|
+
const messages = new Map();
|
|
38
|
+
let loop;
|
|
39
|
+
let disposed = false;
|
|
40
|
+
return {
|
|
41
|
+
async start(hooks) {
|
|
42
|
+
if (disposed)
|
|
43
|
+
throw new BridgeError(409, "The client bridge is disposed");
|
|
44
|
+
if (loop !== undefined)
|
|
45
|
+
throw new BridgeError(409, "The client bridge event subscription already started");
|
|
46
|
+
const signal = controller.signal;
|
|
47
|
+
const iterator = context.event.subscribe({ types, signal })[Symbol.asyncIterator]();
|
|
48
|
+
const first = iterator.next();
|
|
49
|
+
const seeded = listSessions(context, new Request("http://omsago.invalid/session", { signal })).then((records) => {
|
|
50
|
+
for (const session of records)
|
|
51
|
+
sessions.set(session.id, session);
|
|
52
|
+
});
|
|
53
|
+
const emit = async (event) => {
|
|
54
|
+
await hooks.event?.({ event });
|
|
55
|
+
};
|
|
56
|
+
loop = (async () => {
|
|
57
|
+
try {
|
|
58
|
+
await seeded;
|
|
59
|
+
let next = await first;
|
|
60
|
+
while (!next.done) {
|
|
61
|
+
const event = next.value;
|
|
62
|
+
const { sessionID } = event.data;
|
|
63
|
+
if (event.type === SessionEventType.Deleted) {
|
|
64
|
+
const session = sessions.get(sessionID);
|
|
65
|
+
if (session === undefined)
|
|
66
|
+
throw new BridgeError(500, `Deleted session ${sessionID} has no observed snapshot`);
|
|
67
|
+
await emit({
|
|
68
|
+
type: LegacyEventType.SessionDeleted,
|
|
69
|
+
properties: { info: legacySession(session) },
|
|
70
|
+
});
|
|
71
|
+
sessions.delete(sessionID);
|
|
72
|
+
messages.delete(sessionID);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const options = { signal };
|
|
76
|
+
const session = await context.session.get({ sessionID }, options);
|
|
77
|
+
sessions.set(sessionID, session);
|
|
78
|
+
switch (event.type) {
|
|
79
|
+
case SessionEventType.Created:
|
|
80
|
+
await emit({
|
|
81
|
+
type: LegacyEventType.SessionCreated,
|
|
82
|
+
properties: { info: legacySession(session) },
|
|
83
|
+
});
|
|
84
|
+
break;
|
|
85
|
+
case SessionEventType.Renamed:
|
|
86
|
+
case SessionEventType.Moved:
|
|
87
|
+
case SessionEventType.AgentSelected:
|
|
88
|
+
case SessionEventType.ModelSelected:
|
|
89
|
+
case SessionEventType.MetadataUpdated:
|
|
90
|
+
case SessionEventType.Permissions:
|
|
91
|
+
case SessionEventType.Archived:
|
|
92
|
+
case SessionEventType.Unarchived:
|
|
93
|
+
await emit({
|
|
94
|
+
type: LegacyEventType.SessionUpdated,
|
|
95
|
+
properties: { info: legacySession(session) },
|
|
96
|
+
});
|
|
97
|
+
break;
|
|
98
|
+
case SessionEventType.ExecutionStarted:
|
|
99
|
+
await emit({
|
|
100
|
+
type: LegacyEventType.SessionStatus,
|
|
101
|
+
properties: { sessionID, status: { type: SessionStatusKind.Busy } },
|
|
102
|
+
});
|
|
103
|
+
break;
|
|
104
|
+
case SessionEventType.ExecutionFailed:
|
|
105
|
+
await emit({
|
|
106
|
+
type: LegacyEventType.SessionError,
|
|
107
|
+
properties: { sessionID, error: legacyError(event.data.error) },
|
|
108
|
+
});
|
|
109
|
+
await emit({
|
|
110
|
+
type: LegacyEventType.SessionStatus,
|
|
111
|
+
properties: { sessionID, status: { type: SessionStatusKind.Idle } },
|
|
112
|
+
});
|
|
113
|
+
break;
|
|
114
|
+
case SessionEventType.ExecutionInterrupted:
|
|
115
|
+
await emit({
|
|
116
|
+
type: LegacyEventType.SessionError,
|
|
117
|
+
properties: {
|
|
118
|
+
sessionID,
|
|
119
|
+
error: { name: ErrorName.Aborted, data: { message: event.data.reason } },
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
await emit({
|
|
123
|
+
type: LegacyEventType.SessionStatus,
|
|
124
|
+
properties: { sessionID, status: { type: SessionStatusKind.Idle } },
|
|
125
|
+
});
|
|
126
|
+
break;
|
|
127
|
+
case SessionEventType.ExecutionSucceeded:
|
|
128
|
+
await emit({
|
|
129
|
+
type: LegacyEventType.SessionStatus,
|
|
130
|
+
properties: { sessionID, status: { type: SessionStatusKind.Idle } },
|
|
131
|
+
});
|
|
132
|
+
await emit({ type: LegacyEventType.SessionIdle, properties: { sessionID } });
|
|
133
|
+
break;
|
|
134
|
+
case SessionEventType.InboxDelivered:
|
|
135
|
+
case SessionEventType.Synthetic:
|
|
136
|
+
case SessionEventType.StepStarted:
|
|
137
|
+
case SessionEventType.StepEnded:
|
|
138
|
+
case SessionEventType.StepFailed:
|
|
139
|
+
case SessionEventType.TextDelta:
|
|
140
|
+
case SessionEventType.TextEnded:
|
|
141
|
+
case SessionEventType.ReasoningDelta:
|
|
142
|
+
case SessionEventType.ReasoningEnded:
|
|
143
|
+
case SessionEventType.ToolCalled:
|
|
144
|
+
case SessionEventType.ToolProgress:
|
|
145
|
+
case SessionEventType.ToolSuccess:
|
|
146
|
+
case SessionEventType.ToolFailed:
|
|
147
|
+
break;
|
|
148
|
+
default:
|
|
149
|
+
assertNever(event);
|
|
150
|
+
}
|
|
151
|
+
const current = legacyMessages(session, await context.session.context({ sessionID }, options));
|
|
152
|
+
const previous = messages.get(sessionID) ?? [];
|
|
153
|
+
for (const message of current) {
|
|
154
|
+
const old = previous.find((entry) => entry.info.id === message.info.id);
|
|
155
|
+
if (JSON.stringify(old?.info) !== JSON.stringify(message.info))
|
|
156
|
+
await emit({
|
|
157
|
+
type: LegacyEventType.MessageUpdated,
|
|
158
|
+
properties: { info: message.info },
|
|
159
|
+
});
|
|
160
|
+
for (const part of message.parts) {
|
|
161
|
+
const prior = old?.parts.find((entry) => entry.id === part.id);
|
|
162
|
+
if (JSON.stringify(prior) !== JSON.stringify(part))
|
|
163
|
+
await emit({ type: LegacyEventType.MessagePartUpdated, properties: { part } });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
messages.set(sessionID, current);
|
|
167
|
+
}
|
|
168
|
+
next = await iterator.next();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
if (signal.aborted &&
|
|
173
|
+
(error === signal.reason || (error instanceof Error && error.name === ErrorName.Abort)))
|
|
174
|
+
return;
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
await iterator.return?.();
|
|
179
|
+
}
|
|
180
|
+
})();
|
|
181
|
+
void loop.catch((error) => {
|
|
182
|
+
console.error("Client bridge event subscription failed", error);
|
|
183
|
+
});
|
|
184
|
+
await seeded;
|
|
185
|
+
},
|
|
186
|
+
async dispose() {
|
|
187
|
+
disposed = true;
|
|
188
|
+
controller.abort();
|
|
189
|
+
await loop;
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode/plugin";
|
|
2
|
+
import type { Hooks, PluginInput } from "@opencode-ai/plugin";
|
|
3
|
+
import { createOpencodeClient as createSkillClient } from "@opencode-ai/sdk/v2";
|
|
4
|
+
import { ClientSessionState } from "./state.js";
|
|
5
|
+
export type ClientBridge = {
|
|
6
|
+
readonly input: PluginInput & {
|
|
7
|
+
readonly client: PluginInput["client"] & {
|
|
8
|
+
readonly app: PluginInput["client"]["app"] & Pick<ReturnType<typeof createSkillClient>["app"], "skills">;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
readonly sessionState: ClientSessionState;
|
|
12
|
+
readonly start: (hooks: Hooks) => Promise<void>;
|
|
13
|
+
readonly dispose: () => Promise<void>;
|
|
14
|
+
};
|
|
15
|
+
export declare function createClientBridge(context: Plugin.Context): ClientBridge;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { $ } from "bun";
|
|
2
|
+
import { createOpencodeClient } from "@opencode-ai/sdk";
|
|
3
|
+
import { createOpencodeClient as createSkillClient } from "@opencode-ai/sdk/v2";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { currentProject, discoveryRoute } from "./discovery.js";
|
|
6
|
+
import { BridgeError, errorResponse, unsupported } from "./errors.js";
|
|
7
|
+
import { clientEvents } from "./events.js";
|
|
8
|
+
import { sessionRoutes } from "./session-routes.js";
|
|
9
|
+
import { ClientSessionState } from "./state.js";
|
|
10
|
+
export function createClientBridge(context) {
|
|
11
|
+
const sessionState = new ClientSessionState();
|
|
12
|
+
const sessions = sessionRoutes(context, sessionState);
|
|
13
|
+
const events = clientEvents(context);
|
|
14
|
+
const serverUrl = new URL("http://omsago.invalid");
|
|
15
|
+
const transport = async (input, init) => {
|
|
16
|
+
const request = input instanceof Request
|
|
17
|
+
? init === undefined
|
|
18
|
+
? input
|
|
19
|
+
: new Request(input, init)
|
|
20
|
+
: new Request(input.toString(), init);
|
|
21
|
+
request.signal.throwIfAborted();
|
|
22
|
+
try {
|
|
23
|
+
const url = new URL(request.url);
|
|
24
|
+
if (url.origin !== serverUrl.origin)
|
|
25
|
+
throw new BridgeError(400, "The client bridge cannot route a foreign origin");
|
|
26
|
+
const path = url.pathname.split("/").filter(Boolean).map(decodeURIComponent);
|
|
27
|
+
return path[0] === "session"
|
|
28
|
+
? await sessions(request, path)
|
|
29
|
+
: await discoveryRoute(context, request);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (request.signal.aborted)
|
|
33
|
+
throw error;
|
|
34
|
+
if (error instanceof z.ZodError || error instanceof SyntaxError || error instanceof URIError)
|
|
35
|
+
return errorResponse(new BridgeError(400, error.message));
|
|
36
|
+
return errorResponse(error);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const config = {
|
|
40
|
+
baseUrl: serverUrl.href,
|
|
41
|
+
directory: context.location.directory,
|
|
42
|
+
fetch: Object.assign(transport, {
|
|
43
|
+
preconnect() {
|
|
44
|
+
unsupported("fetch.preconnect");
|
|
45
|
+
},
|
|
46
|
+
}),
|
|
47
|
+
};
|
|
48
|
+
const client = createOpencodeClient(config);
|
|
49
|
+
const skillClient = createSkillClient(config);
|
|
50
|
+
const app = Object.assign(client.app, { skills: skillClient.app.skills.bind(skillClient.app) });
|
|
51
|
+
const input = {
|
|
52
|
+
client: Object.assign(client, { app }),
|
|
53
|
+
project: currentProject(context),
|
|
54
|
+
directory: context.location.directory,
|
|
55
|
+
worktree: context.location.project.directory,
|
|
56
|
+
serverUrl,
|
|
57
|
+
$,
|
|
58
|
+
experimental_workspace: {
|
|
59
|
+
register(_type, _adapter) {
|
|
60
|
+
unsupported("experimental_workspace.register");
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
input,
|
|
66
|
+
sessionState,
|
|
67
|
+
start: (hooks) => events.start(hooks),
|
|
68
|
+
dispose: () => events.dispose(),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { SessionInfo, SessionMessageInfo, SessionStructuredError } from "@opencode/client";
|
|
2
|
+
import type { AssistantMessage, SessionMessagesResponse } from "@opencode-ai/sdk";
|
|
3
|
+
export declare function legacyError(error: SessionStructuredError): NonNullable<AssistantMessage["error"]>;
|
|
4
|
+
export declare function legacyMessages(session: SessionInfo, records: readonly SessionMessageInfo[]): SessionMessagesResponse;
|