mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-01 06:42:26 +00:00
sync
This commit is contained in:
78
packages/opencode/src/app/app.ts
Normal file
78
packages/opencode/src/app/app.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import fs from "fs/promises";
|
||||
import { AppPath } from "./path";
|
||||
import { Log } from "../util/log";
|
||||
import { Context } from "../util/context";
|
||||
|
||||
export namespace App {
|
||||
const log = Log.create({ service: "app" });
|
||||
|
||||
export type Info = Awaited<ReturnType<typeof create>>;
|
||||
|
||||
const ctx = Context.create<Info>("app");
|
||||
|
||||
async function create(input: { directory: string }) {
|
||||
const dataDir = AppPath.data(input.directory);
|
||||
await fs.mkdir(dataDir, { recursive: true });
|
||||
await Log.file(input.directory);
|
||||
|
||||
log.info("created", { path: dataDir });
|
||||
|
||||
const services = new Map<
|
||||
any,
|
||||
{
|
||||
state: any;
|
||||
shutdown?: (input: any) => Promise<void>;
|
||||
}
|
||||
>();
|
||||
|
||||
const result = {
|
||||
get services() {
|
||||
return services;
|
||||
},
|
||||
get root() {
|
||||
return input.directory;
|
||||
},
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function state<State>(
|
||||
key: any,
|
||||
init: (app: Info) => State,
|
||||
shutdown?: (state: Awaited<State>) => Promise<void>,
|
||||
) {
|
||||
return () => {
|
||||
const app = ctx.use();
|
||||
const services = app.services;
|
||||
if (!services.has(key)) {
|
||||
log.info("registering service", { name: key });
|
||||
services.set(key, {
|
||||
state: init(app),
|
||||
shutdown: shutdown,
|
||||
});
|
||||
}
|
||||
return services.get(key)?.state as State;
|
||||
};
|
||||
}
|
||||
|
||||
export async function use() {
|
||||
return ctx.use();
|
||||
}
|
||||
|
||||
export async function provide<T extends (app: Info) => any>(
|
||||
input: { directory: string },
|
||||
cb: T,
|
||||
) {
|
||||
const app = await create(input);
|
||||
|
||||
return ctx.provide(app, async () => {
|
||||
const result = await cb(app);
|
||||
for (const [key, entry] of app.services.entries()) {
|
||||
log.info("shutdown", { name: key });
|
||||
await entry.shutdown?.(await entry.state);
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
}
|
||||
11
packages/opencode/src/app/path.ts
Normal file
11
packages/opencode/src/app/path.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import path from "path";
|
||||
|
||||
export namespace AppPath {
|
||||
export function data(input: string) {
|
||||
return path.join(input, ".opencode");
|
||||
}
|
||||
|
||||
export function storage(input: string) {
|
||||
return path.join(data(input), "storage");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user