Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion opencode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"opencode-worker": "./dist/index.js"
},
"dependencies": {
"iii-sdk": "^0.19.2",
"iii-sdk": "^0.20.0",
"yaml": "^2.6.0",
"zod": "^4.0.0"
},
Expand Down
18 changes: 9 additions & 9 deletions opencode/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions opencode/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* hot-reloads.
*/

import type { ISdk } from 'iii-sdk';
import type { IIIClient } from 'iii-sdk';
import {
type Config,
type RuntimeConfig,
Expand All @@ -24,7 +24,7 @@ const TIMEOUT_MS = 5_000;
/** Live snapshot shared with the handlers; `current` is whole-replaced on reload. */
export type ConfigHolder = { current: Config };

export async function registerOpencodeConfig(iii: ISdk, seed: Config): Promise<void> {
export async function registerOpencodeConfig(iii: IIIClient, seed: Config): Promise<void> {
await iii.trigger({
function_id: 'configuration::register',
payload: {
Expand All @@ -40,7 +40,7 @@ export async function registerOpencodeConfig(iii: ISdk, seed: Config): Promise<v
}

/** Fetch the live runtime config; null when unset/unreachable. */
export async function fetchRuntime(iii: ISdk): Promise<RuntimeConfig | null> {
export async function fetchRuntime(iii: IIIClient): Promise<RuntimeConfig | null> {
try {
const res = await iii.trigger<unknown, { value?: unknown }>({
function_id: 'configuration::get',
Expand All @@ -60,7 +60,10 @@ export async function fetchRuntime(iii: ISdk): Promise<RuntimeConfig | null> {
* Register the change handler + bind the `configuration` trigger. `onChange` is
* called once now (reconcile) and on every `configuration:updated`.
*/
export async function bindConfigTrigger(iii: ISdk, onChange: () => Promise<void>): Promise<void> {
export async function bindConfigTrigger(
iii: IIIClient,
onChange: () => Promise<void>,
): Promise<void> {
await onChange();
iii.registerFunction(
CONFIG_FN_ID,
Expand Down
4 changes: 2 additions & 2 deletions opencode/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/

import { randomUUID } from 'node:crypto';
import type { ISdk } from 'iii-sdk';
import type { IIIClient } from 'iii-sdk';
const PROCESS_EPOCH = randomUUID();
const seqBySession = new Map<string, number>();

export function makeEmitter(iii: ISdk, streamName: string) {
export function makeEmitter(iii: IIIClient, streamName: string) {
return async function emit(session_id: string, event: unknown): Promise<void> {
const seq = seqBySession.get(session_id) ?? 0;
seqBySession.set(session_id, seq + 1);
Expand Down
17 changes: 13 additions & 4 deletions opencode/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,29 @@ const { values } = parseArgs({
});

const seed = await loadConfig(String(values.config));
const url = values.url ? String(values.url) : seed.engine_url;
const url = values.url
? String(values.url)
: (process.env.III_URL ?? process.env.III_ENGINE_URL ?? seed.engine_url);
const bootConfig: Config = {
...seed,
engine_url: url,
opencode_executable: resolveOpencodeExecutable(seed.opencode_executable),
};

const iii = registerWorker(url, { workerName: 'opencode' });

try {
await registerOpencodeConfig(iii, seed);
await registerOpencodeConfig(iii, bootConfig);
} catch (err) {
console.warn(`configuration::register failed; continuing with the seed: ${String(err)}`);
}

const holder: ConfigHolder = { current: seed };
const holder: ConfigHolder = { current: bootConfig };
const refresh = async () => {
const runtime = (await fetchRuntime(iii)) ?? undefined;
const merged: Config = runtime ? { engine_url: seed.engine_url, ...runtime } : { ...seed };
const merged: Config = runtime
? { engine_url: bootConfig.engine_url, ...runtime }
: { ...bootConfig };
merged.opencode_executable = resolveOpencodeExecutable(merged.opencode_executable);
holder.current = merged;
};
Expand Down
10 changes: 5 additions & 5 deletions opencode/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { spawn } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import { createInterface } from 'node:readline';
import type { ISdk } from 'iii-sdk';
import type { IIIClient } from 'iii-sdk';
import { z } from 'zod';
import type { Config } from './config.js';
import type { Emit } from './events.js';
Expand Down Expand Up @@ -121,7 +121,7 @@ const SESSIONS_RESPONSE_FORMAT = jsonSchema(z.object({ sessions: z.array(Session
type LiveRun = { kill: () => void };
const live = new Map<string, LiveRun>();

async function markSessionError(iii: ISdk, session_id: string): Promise<void> {
async function markSessionError(iii: IIIClient, session_id: string): Promise<void> {
try {
const record = await loadSession(iii, session_id);
if (record && record.status === 'working') {
Expand Down Expand Up @@ -166,7 +166,7 @@ export function buildArgs(
}

export async function executeRun(
iii: ISdk,
iii: IIIClient,
cfg: Config,
emit: Emit,
emitRaw: Emit,
Expand All @@ -187,7 +187,7 @@ export async function executeRun(
}

async function runReserved(
iii: ISdk,
iii: IIIClient,
cfg: Config,
emit: Emit,
emitRaw: Emit,
Expand Down Expand Up @@ -358,7 +358,7 @@ async function runReserved(
return envelope;
}

export function register(iii: ISdk, getCfg: () => Config, emit: Emit, emitRaw: Emit): void {
export function register(iii: IIIClient, getCfg: () => Config, emit: Emit, emitRaw: Emit): void {
iii.registerFunction(
'opencode::run',
async (payload: unknown) =>
Expand Down
11 changes: 7 additions & 4 deletions opencode/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@
* with the same session_id resumes the underlying OpenCode conversation.
*/

import type { ISdk } from 'iii-sdk';
import type { IIIClient } from 'iii-sdk';
import type { SessionRecord } from './types.js';

const SCOPE = 'opencode_sessions';

export async function loadSession(iii: ISdk, session_id: string): Promise<SessionRecord | null> {
export async function loadSession(
iii: IIIClient,
session_id: string,
): Promise<SessionRecord | null> {
const res = await iii.trigger<unknown, SessionRecord | null>({
function_id: 'state::get',
payload: { scope: SCOPE, key: session_id },
});
return res && typeof res === 'object' && 'session_id' in res ? res : null;
}

export async function saveSession(iii: ISdk, record: SessionRecord): Promise<void> {
export async function saveSession(iii: IIIClient, record: SessionRecord): Promise<void> {
await iii.trigger({
function_id: 'state::set',
payload: { scope: SCOPE, key: record.session_id, value: record },
});
}

export async function listSessions(iii: ISdk): Promise<SessionRecord[]> {
export async function listSessions(iii: IIIClient): Promise<SessionRecord[]> {
const res = await iii.trigger<unknown, SessionRecord[] | null>({
function_id: 'state::list',
payload: { scope: SCOPE },
Expand Down
Loading