import { LayerNode } from "@opencode-ai/core/effect/layer-node" // altimate_change start — makeRuntime for the restored Promise wrapper (bottom of file) import { makeRuntime } from "@/effect/run-service" // altimate_change end import { PermissionV1 } from "@opencode-ai/core/v1/permission" import { Config } from "@/config/config" import { serviceUse } from "@opencode-ai/core/effect/service-use" import { Provider } from "@/provider/provider" import { generateObject, streamObject, type ModelMessage } from "ai" import { Truncate } from "@/tool/truncate" import { Auth } from "../auth" import { ProviderTransform } from "@/provider/transform" import PROMPT_GENERATE from "./generate.txt" import PROMPT_COMPACTION from "./prompt/compaction.txt" import PROMPT_EXPLORE from "./prompt/explore.txt" import PROMPT_SUMMARY from "./prompt/summary.txt" import PROMPT_TITLE from "./prompt/title.txt" // altimate_change start - import custom agent mode prompts // PromptProfiles.PROMPT_BUILDER is assembled from core + pack fragments (byte-identical // to the former builder.txt — see profiles.ts and test/altimate/prompt-profiles.test.ts) import { PromptProfiles } from "../altimate/prompts/profiles" import PROMPT_ANALYST from "../altimate/prompts/analyst.txt" import PROMPT_REVIEWER from "../altimate/prompts/reviewer.txt" import PROMPT_DATA_EXPLORER from "../altimate/prompts/data-explorer.txt" import { Log } from "../util/log" import { Wildcard } from "../util/wildcard" // altimate_change end import { Permission } from "@/permission" import { mergeDeep, pipe, sortBy, values } from "remeda" import { Global } from "@opencode-ai/core/global" import path from "path" import { Plugin } from "@/plugin" import { Skill } from "../skill" import { Effect, Context, Layer, Schema } from "effect" import { InstanceState } from "@/effect/instance-state" import * as Option from "effect/Option" import * as OtelTracer from "@effect/opentelemetry/Tracer" import { AbsolutePath, type DeepMutable } from "@opencode-ai/core/schema" import { ProviderV2 } from "@opencode-ai/core/provider" import { ModelV2 } from "@opencode-ai/core/model" // altimate_change start — fork ID brands for re-branding at the namespace/core boundary import { ProviderID, ModelID } from "@/provider/schema" // altimate_change end import { LocationServiceMap } from "@opencode-ai/core/location-layer" import { PluginBoot } from "@opencode-ai/core/plugin/boot" import { Reference } from "@opencode-ai/core/reference" import { Location } from "@opencode-ai/core/location" export const Info = Schema.Struct({ name: Schema.String, description: Schema.optional(Schema.String), mode: Schema.Literals(["subagent", "primary", "all"]), native: Schema.optional(Schema.Boolean), hidden: Schema.optional(Schema.Boolean), topP: Schema.optional(Schema.Finite), temperature: Schema.optional(Schema.Finite), color: Schema.optional(Schema.String), permission: PermissionV1.Ruleset, model: Schema.optional( Schema.Struct({ modelID: ModelV2.ID, providerID: ProviderV2.ID, }), ), variant: Schema.optional(Schema.String), prompt: Schema.optional(Schema.String), options: Schema.Record(Schema.String, Schema.Unknown), steps: Schema.optional(Schema.Finite), }).annotate({ identifier: "Agent" }) export type Info = DeepMutable> const GeneratedAgent = Schema.Struct({ identifier: Schema.String, whenToUse: Schema.String, systemPrompt: Schema.String, }) export interface Interface { readonly get: (agent: string) => Effect.Effect readonly list: () => Effect.Effect readonly defaultInfo: () => Effect.Effect readonly defaultAgent: () => Effect.Effect readonly generate: (input: { description: string model?: { providerID: ProviderV2.ID; modelID: ModelV2.ID } }) => Effect.Effect< { identifier: string whenToUse: string systemPrompt: string }, Provider.DefaultModelError > } type State = Omit export class Service extends Context.Service()("@opencode/Agent") {} export const use = serviceUse(Service) export const layer = Layer.effect( Service, Effect.gen(function* () { const config = yield* Config.Service const auth = yield* Auth.Service const plugin = yield* Plugin.Service const skill = yield* Skill.Service const provider = yield* Provider.Service const locations = yield* LocationServiceMap const state = yield* InstanceState.make( Effect.fn("Agent.state")(function* (ctx) { const cfg = yield* config.get() const skillDirs = yield* skill.dirs() const referenceDirs = yield* Effect.gen(function* () { yield* (yield* PluginBoot.Service).wait() return (yield* (yield* Reference.Service).list()).map((reference) => reference.path) }).pipe(Effect.provide(locations.get(Location.Ref.make({ directory: AbsolutePath.make(ctx.directory) })))) const whitelistedDirs = [ Truncate.GLOB, path.join(Global.Path.tmp, "*"), ...skillDirs.map((dir) => path.join(dir, "*")), ...referenceDirs.map((dir) => path.join(dir, "*")), ] const readonlyExternalDirectory = { "*": "ask", ...Object.fromEntries(whitelistedDirs.map((dir) => [dir, "allow"])), } satisfies Record const defaults = Permission.fromConfig({ "*": "allow", doom_loop: "ask", external_directory: { "*": "ask", ...Object.fromEntries(whitelistedDirs.map((dir) => [dir, "allow"])), }, question: "deny", plan_enter: "deny", plan_exit: "deny", // altimate_change start — make the #209 sensitive-write guard actually fire. // The guard calls ctx.ask({ permission: "sensitive_write" }) before writing a // sensitive target (.env/.ssh/.git/...), but with no explicit rule it fell through // to the "*": "allow" catch-all above and auto-approved — silently neutralizing the // guard. Default it to "ask" so sensitive writes prompt (users can still override to // "allow" in config). Restrictive agents (analyst/reviewer/plan) keep their later // "*": "deny", so this only affects write-capable agents. sensitive_write: "ask", // altimate_change end // mirrors github.com/github/gitignore Node.gitignore pattern for .env files read: { "*": "allow", "*.env": "ask", "*.env.*": "ask", "*.env.example": "allow", }, // altimate_change start - bash safety defaults for destructive file/git/DDL commands // Safety defaults for bash commands. // IMPORTANT: "*": "ask" must come FIRST because evaluation uses last-match-wins. // // "ask" = user sees prompt and can approve. Used for destructive file/git // commands that are common in legitimate workflows (rm -rf ./build, // git push --force after rebase, git clean in CI). // "deny" = blocked entirely, no prompt. Used for database DDL that is // almost never intentional in an agent context. // // Users can override any of these in altimate-code.json. bash: { "*": "ask", "rm -rf *": "ask", "rm -fr *": "ask", "git push --force *": "ask", "git push -f *": "ask", "git reset --hard *": "ask", "git clean -f *": "ask", "DROP DATABASE *": "deny", "DROP SCHEMA *": "deny", "TRUNCATE *": "deny", "drop database *": "deny", "drop schema *": "deny", "truncate *": "deny", }, // altimate_change end }) const user = Permission.fromConfig(cfg.permission ?? {}) // altimate_change start - safety deny rules that cannot be overridden by wildcard allows // Safety deny rules that CANNOT be overridden by wildcard allows. // Appended after user config so they always take precedence via last-match-wins. // Users who need to override must use specific patterns like // `"DROP DATABASE test_db": "allow"` — wildcard `bash: "allow"` won't work. // Both UPPER and lowercase variants are included because Wildcard.match // is case-sensitive on Linux/macOS. const safetyDenials = Permission.fromConfig({ bash: { "DROP DATABASE *": "deny", "DROP SCHEMA *": "deny", "TRUNCATE *": "deny", "drop database *": "deny", "drop schema *": "deny", "truncate *": "deny", "Drop Database *": "deny", "Drop Schema *": "deny", "Truncate *": "deny", }, // SQL write safety denials sql_execute_write: { "DROP DATABASE *": "deny", "DROP SCHEMA *": "deny", "TRUNCATE *": "deny", "drop database *": "deny", "drop schema *": "deny", "truncate *": "deny", "Drop Database *": "deny", "Drop Schema *": "deny", "Truncate *": "deny", }, }) // Combine user config with safety denials so every agent inherits them const userWithSafety = Permission.merge(user, safetyDenials) // altimate_change end // altimate_change start — one-time warning state for the removed data-qa // default-agent migration (see defaultInfo() below) const log = Log.create({ service: "agent" }) let warnedRemovedDataQaDefault = false // altimate_change end // altimate_change start — read-only data allow-list shared by analyst and its data-explorer subagent // Bash is denied entirely: Wildcard.match can match literal operators, but // BashTool checks individual command nodes, omitting pipes/chaining and // redirects around groups (e.g. `(cat model.sql)>out.txt` becomes `cat model.sql`). // Trailing deny patterns therefore cannot enforce a read-only shell. This // removes even simple ls/cat/find/dbt reads; use read/grep/glob and SQL tools. const readOnlyDataPermission = { // SQL read tools sql_execute: "allow", sql_check: "allow", altimate_core_validate: "allow", sql_analyze: "allow", sql_translate: "allow", sql_optimize: "allow", lineage_check: "allow", sql_explain: "allow", sql_format: "allow", sql_fix: "allow", sql_autocomplete: "allow", sql_diff: "allow", // SQL writes denied sql_execute_write: "deny", // Warehouse/schema/finops warehouse_list: "allow", warehouse_test: "allow", warehouse_discover: "allow", schema_inspect: "allow", schema_index: "allow", schema_search: "allow", schema_cache_status: "allow", schema_detect_pii: "allow", schema_tags: "allow", schema_tags_list: "allow", finops_query_history: "allow", finops_analyze_credits: "allow", finops_expensive_queries: "allow", finops_warehouse_advice: "allow", finops_unused_resources: "allow", finops_role_grants: "allow", finops_role_hierarchy: "allow", finops_user_roles: "allow", // Core tools altimate_core_check: "allow", altimate_core_rewrite: "allow", // Read-only file access read: "allow", grep: "allow", glob: "allow", tool_lookup: "allow", // Materialized scratch tables for multi-step analysis (tool added separately; key is harmless if absent) sql_materialize: "allow", bash: "deny", } as const // Cap every configured ruleset at the subagent's read-only allow-list. // Replaying only rules for allowed tools preserves configured read denials // and asks while wildcard grants can never expose an unlisted tool. const dataExplorerPermission = (rules: PermissionV1.Ruleset) => Permission.merge( Permission.fromConfig({ "*": "deny" }), ...Object.entries(readOnlyDataPermission) .filter(([, action]) => action === "allow") .map(([permission]) => rules .filter((rule) => Wildcard.match(permission, rule.permission)) .map((rule) => ({ ...rule, permission })), ), ) // altimate_change end const agents: Record = { // altimate_change start - 3 modes: builder, analyst, plan (replaces upstream single "build" agent) builder: { name: "builder", description: "Create and modify dbt models, SQL, and data pipelines. Full read/write access.", prompt: PromptProfiles.PROMPT_BUILDER, options: {}, permission: Permission.merge( defaults, Permission.fromConfig({ question: "allow", plan_enter: "allow", sql_execute_write: "ask", }), userWithSafety, ), mode: "primary", native: true, }, analyst: { name: "analyst", description: "Read-only data exploration and analysis. Cannot modify files or run destructive SQL.", prompt: PROMPT_ANALYST, options: {}, permission: Permission.merge( defaults, Permission.fromConfig({ "*": "deny", ...readOnlyDataPermission, answer_commit: "allow", webfetch: "allow", websearch: "allow", question: "allow", // Skills return instruction text only (no side effects), so they are safe for a read-only agent. skill: "allow", // "*": "deny" above also overrides the default allow for skill directories; add it back // so files bundled with a global skill (stored outside the project) stay readable. external_directory: Object.fromEntries(skillDirs.map((dir) => [path.join(dir, "*"), "allow"])), // Workspace memory: read, reload and view the audit log. Reloading syncs like the // load every session runs (it can drop a local copy of a block the workspace has // archived), but cannot create or edit memory. Saving and deleting stay denied, so a // decision is recorded through builder. altimate_memory_read: "allow", altimate_memory_refresh: "allow", altimate_memory_audit: "allow", // Delegation is limited to the read-only data-explorer subagent: general/explore/build // carry broader permissions than analyst, and children do not inherit parent-agent limits. task: { "*": "deny", "data-explorer": "allow", }, // Training training_save: "allow", training_list: "allow", training_remove: "allow", }), userWithSafety, ), mode: "primary", native: true, }, // altimate_change start — read-only data subagent the analyst may delegate to "data-explorer": { name: "data-explorer", description: "Read-only data exploration helper: runs SELECT queries, inspects schemas, reads files; cannot modify files or data.", prompt: PROMPT_DATA_EXPLORER, options: {}, permission: dataExplorerPermission( Permission.merge( defaults, Permission.fromConfig({ "*": "deny", ...readOnlyDataPermission, // Writes denied explicitly (also covered by "*": "deny"; kept as an invariant). // Shell patterns cannot enforce read-only access (redirections and find -delete write). bash: "deny", sql_execute_write: "deny", edit: "deny", write: "deny", apply_patch: "deny", }), userWithSafety, // Re-apply the invariants after user config so a permissive global config cannot grant writes. Permission.fromConfig({ bash: "deny", sql_execute_write: "deny", edit: "deny", write: "deny", apply_patch: "deny", task: "deny", }), ), ), mode: "subagent", native: true, }, // altimate_change end // reviewer agent: dbt PR review verdict engine reviewer: { name: "reviewer", description: "dbt PR reviewer. Runs the dbt_pr_review verdict engine (lineage, equivalence, PII, grade) plus read-only analysis tools and posts findings. Edit/write tools are denied; bash prompts for approval.", prompt: PROMPT_REVIEWER, options: {}, permission: Permission.merge( defaults, Permission.fromConfig({ "*": "deny", // The verdict engine + the read-only analysis tools it composes. dbt_pr_review: "allow", impact_analysis: "allow", altimate_core_check: "allow", altimate_core_grade: "allow", altimate_core_equivalence: "allow", altimate_core_column_lineage: "allow", altimate_core_compare: "allow", lineage_check: "allow", sql_analyze: "allow", sql_diff: "allow", schema_detect_pii: "allow", // Writes denied — review never mutates the project. sql_execute_write: "deny", // Read-only file + repo access (structured tools, not bash). read: "allow", grep: "allow", glob: "allow", list: "allow", tool_lookup: "allow", // Reviews routinely span sibling repos and skill dirs; without this // the "*" deny above hard-fails read/grep/glob outside the project // (#978). "ask" keeps the user in the loop; whitelisted dirs stay // frictionless. external_directory: readonlyExternalDirectory, // Read-only web access so the reviewer can pull PR/issue URLs. webfetch: "allow", websearch: "allow", // Bash PROMPTS instead of hard-denying (#978: `gh pr view` is the // primary way to review a PR URL). A string-prefix allowlist can't // safely bound argv (redirects ride inside the matched command), so // every bash command requires explicit user approval here — the // reviewer still never runs shell commands silently. bash: "ask", }), // altimate_change start — reviewer safety must not be overridable by a permissive user // config (e.g. global `permission: {"*":"allow"}` or `bash:"allow"`). Merge user config, // THEN re-apply the reviewer read-only invariants, THEN safetyDenials LAST so DDL denies // still win over the reviewer's bash:"ask". (edit covers write/edit/apply_patch.) user, Permission.fromConfig({ bash: "ask", edit: "deny", sql_execute_write: "deny", }), safetyDenials, // altimate_change end ), mode: "primary", native: true, }, // altimate_change end plan: { name: "plan", description: "Plan mode. Disallows all edit tools.", options: {}, permission: Permission.merge( defaults, Permission.fromConfig({ question: "allow", plan_exit: "allow", task: { general: "deny", }, external_directory: { [path.join(Global.Path.data, "plans", "*")]: "allow", }, edit: { "*": "deny", [path.join(".opencode", "plans", "*.md")]: "allow", [path.relative(ctx.worktree, path.join(Global.Path.data, path.join("plans", "*.md")))]: "allow", }, }), // altimate_change start - inherit safety denials userWithSafety, // altimate_change end ), mode: "primary", native: true, }, general: { name: "general", description: `General-purpose agent for researching complex questions and executing multi-step tasks. Use this agent to execute multiple units of work in parallel.`, permission: Permission.merge( defaults, Permission.fromConfig({ todowrite: "deny", }), // altimate_change start - inherit safety denials userWithSafety, // altimate_change end ), options: {}, mode: "subagent", native: true, }, explore: { name: "explore", permission: Permission.merge( defaults, Permission.fromConfig({ "*": "deny", grep: "allow", glob: "allow", // altimate_change start — restore codesearch for explore (dropped by the v1.17.9 merge; // the tool gates on this permission and silently degrades to local grep without it) codesearch: "allow", // altimate_change end list: "allow", bash: "allow", webfetch: "allow", websearch: "allow", read: "allow", external_directory: readonlyExternalDirectory, }), // altimate_change start - inherit safety denials userWithSafety, // altimate_change end ), description: `Fast agent specialized for exploring codebases. Use this when you need to quickly find files by patterns (eg. "src/components/**/*.tsx"), search code for keywords (eg. "API endpoints"), or answer questions about the codebase (eg. "how do API endpoints work?"). When calling this agent, specify the desired thoroughness level: "quick" for basic searches, "medium" for moderate exploration, or "very thorough" for comprehensive analysis across multiple locations and naming conventions.`, prompt: PROMPT_EXPLORE, options: {}, mode: "subagent", native: true, }, compaction: { name: "compaction", mode: "primary", native: true, hidden: true, prompt: PROMPT_COMPACTION, permission: Permission.merge( defaults, Permission.fromConfig({ "*": "deny", }), // altimate_change start - inherit safety denials userWithSafety, // altimate_change end ), options: {}, }, title: { name: "title", mode: "primary", options: {}, native: true, hidden: true, temperature: 0.5, permission: Permission.merge( defaults, Permission.fromConfig({ "*": "deny", }), // altimate_change start - inherit safety denials userWithSafety, // altimate_change end ), prompt: PROMPT_TITLE, }, summary: { name: "summary", mode: "primary", options: {}, native: true, hidden: true, permission: Permission.merge( defaults, Permission.fromConfig({ "*": "deny", }), // altimate_change start - inherit safety denials userWithSafety, // altimate_change end ), prompt: PROMPT_SUMMARY, }, } for (const [key, value] of Object.entries(cfg.agent ?? {})) { if (value.disable) { delete agents[key] continue } let item = agents[key] if (!item) item = agents[key] = { name: key, mode: "all", // altimate_change start - new agents inherit safety denials permission: Permission.merge(defaults, userWithSafety), // altimate_change end options: {}, native: false, } if (value.model) { // altimate_change start — re-brand fork ProviderID/ModelID to core ProviderV2.ID/ModelV2.ID (identity at runtime) const parsed = Provider.parseModel(value.model) item.model = { providerID: ProviderV2.ID.make(parsed.providerID), modelID: ModelV2.ID.make(parsed.modelID), } // altimate_change end } item.variant = value.variant ?? item.variant item.prompt = value.prompt ?? item.prompt item.description = value.description ?? item.description item.temperature = value.temperature ?? item.temperature item.topP = value.top_p ?? item.topP item.mode = value.mode ?? item.mode item.color = value.color ?? item.color item.hidden = value.hidden ?? item.hidden item.name = value.name ?? item.name item.steps = value.steps ?? item.steps item.options = mergeDeep(item.options, value.options ?? {}) // altimate_change start - re-apply safety denials AFTER user config so they cannot be overridden item.permission = Permission.merge( item.permission, Permission.fromConfig(value.permission ?? {}), safetyDenials, ) if (key === "data-explorer") item.permission = dataExplorerPermission(item.permission) // altimate_change end } // Ensure Truncate.GLOB is allowed unless explicitly configured for (const name in agents) { const agent = agents[name] // altimate_change start — keep analyst's shell and write tools disabled after config and safety rules // Permission.disabled requires the final bash rule to deny "*". Without // it, narrower safety rules leave the tool exposed to commands that emit // no permission patterns (e.g. `>out.txt`). data-explorer is capped above. // The built-in tools that change files, memory, config, connections or external // systems are denied again too: last-match-wins would otherwise let a config grant // such as "*": "allow" turn them back on for the read-only agent. Most of them never // call ctx.ask, so removing them from the tool list is their only gate. batch is // denied because it runs other tools without that tool-list filter. if (name === "analyst") { // Keep what config resolved for data-explorer (it may deny or ask), but nothing broader. const dataExplorerAction = Permission.evaluate("task", "data-explorer", agent.permission).action agent.permission = Permission.merge( agent.permission, Permission.fromConfig({ ...Object.fromEntries( [ "bash", "sql_execute_write", "edit", "write", "apply_patch", "altimate_memory_write", "altimate_memory_delete", "altimate_memory_extract", "training_import", "warehouse_add", "warehouse_remove", "warehouse_install_driver", "mcp_discover", "datamate_manager", "sample_setup", "feedback_submit", // Runs caller-supplied SQL without the write classification sql_execute applies. "data_diff", "batch", ].map((tool) => [tool, "deny" as const]), ), // A trailing "*" deny is what removes task from the tool list when nothing is allowed. task: dataExplorerAction === "deny" ? "deny" : { "*": "deny", "data-explorer": dataExplorerAction }, }), ) } // altimate_change end const explicit = agent.permission.some((r) => { if (r.permission !== "external_directory") return false if (r.action !== "deny") return false return r.pattern === Truncate.GLOB }) if (explicit) continue agents[name].permission = Permission.merge( agents[name].permission, Permission.fromConfig({ external_directory: { [Truncate.GLOB]: "allow" } }), ) } const get = Effect.fnUntraced(function* (agent: string) { // altimate_change start — "build" alias for "builder" (renamed agent) if (!agents[agent] && agent === "build") return agents["builder"] // altimate_change end return agents[agent] }) const list = Effect.fnUntraced(function* () { const cfg = yield* config.get() return pipe( agents, values(), sortBy( // altimate_change start - default agent is "builder" not "build" [(x) => (cfg.default_agent ? x.name === cfg.default_agent : x.name === "builder"), "desc"], // altimate_change end [(x) => x.name, "asc"], ), ) }) const defaultInfo = Effect.fnUntraced(function* () { const c = yield* config.get() if (c.default_agent) { // altimate_change start — migrate the removed data-qa default to analyst let agent = agents[c.default_agent] // #1217 let `default_agent: "data-qa"` alone (no matching `agent.data-qa` // config entry) opt into the native data-qa profile. That profile is now // removed, so a persisted `default_agent: "data-qa"` would otherwise throw // here and strand every call site that resolves the default agent // (session/prompt.ts, the session HTTP routes, ACP) — upgrading users could // no longer start an ordinary default-agent session. Fall back to `analyst`, // the documented agent for read-only data questions, with a one-time warning // instead of a hard failure. A user who separately defines their own // `agent.data-qa` config entry is unaffected — `agents[c.default_agent]` // already resolves to that legitimate custom agent above. if (!agent && c.default_agent === "data-qa") { if (!warnedRemovedDataQaDefault) { warnedRemovedDataQaDefault = true log.warn( 'the "data-qa" agent was removed; defaulting to "analyst" for read-only data questions — set default_agent to override', ) } agent = agents["analyst"] } // altimate_change end if (!agent) throw new Error(`default agent "${c.default_agent}" not found`) if (agent.mode === "subagent") throw new Error(`default agent "${c.default_agent}" is a subagent`) if (agent.hidden === true) throw new Error(`default agent "${c.default_agent}" is hidden`) return agent } const visible = Object.values(agents).find((a) => a.mode !== "subagent" && a.hidden !== true) if (!visible) throw new Error("no primary visible agent found") return visible }) const defaultAgent = Effect.fnUntraced(function* () { return (yield* defaultInfo()).name }) return { get, list, defaultInfo, defaultAgent, } satisfies State }), ) return Service.of({ get: Effect.fn("Agent.get")(function* (agent: string) { return yield* InstanceState.useEffect(state, (s) => s.get(agent)) }), list: Effect.fn("Agent.list")(function* () { return yield* InstanceState.useEffect(state, (s) => s.list()) }), defaultInfo: Effect.fn("Agent.defaultInfo")(function* () { return yield* InstanceState.useEffect(state, (s) => s.defaultInfo()) }), defaultAgent: Effect.fn("Agent.defaultAgent")(function* () { return yield* InstanceState.useEffect(state, (s) => s.defaultAgent()) }), generate: Effect.fn("Agent.generate")(function* (input: { description: string model?: { providerID: ProviderV2.ID; modelID: ModelV2.ID } }) { const cfg = yield* config.get() const model = input.model ?? (yield* provider.defaultModel()) const resolved = yield* provider.getModel( // altimate_change start — re-brand core/fork ID union to fork ProviderID/ModelID (identity at runtime) ProviderID.make(model.providerID), ModelID.make(model.modelID), // altimate_change end ) const language = yield* provider.getLanguage(resolved) const tracer = cfg.experimental?.openTelemetry ? Option.getOrUndefined(yield* Effect.serviceOption(OtelTracer.OtelTracer)) : undefined const system = [PROMPT_GENERATE] yield* plugin.trigger("experimental.chat.system.transform", { model: resolved }, { system }) const existing = yield* InstanceState.useEffect(state, (s) => s.list()) // TODO: clean this up so provider specific logic doesnt bleed over const authInfo = yield* auth.get(model.providerID).pipe(Effect.orDie) const isOpenaiOauth = model.providerID === "openai" && authInfo?.type === "oauth" const params = { experimental_telemetry: { isEnabled: cfg.experimental?.openTelemetry, tracer, metadata: { userId: cfg.username ?? "unknown", }, }, temperature: 0.3, messages: [ ...(isOpenaiOauth ? [] : system.map( (item): ModelMessage => ({ role: "system", content: item, }), )), { role: "user", content: `Create an agent configuration based on this request: "${input.description}".\n\nIMPORTANT: The following identifiers already exist and must NOT be used: ${existing.map((i) => i.name).join(", ")}\n Return ONLY the JSON object, no other text, do not wrap in backticks`, }, ], model: language, schema: Object.assign( Schema.toStandardSchemaV1(GeneratedAgent), Schema.toStandardJSONSchemaV1(GeneratedAgent), ), } satisfies Parameters[0] if (isOpenaiOauth) { return yield* Effect.promise(async () => { const result = streamObject({ ...params, providerOptions: ProviderTransform.providerOptions(resolved, { instructions: system.join("\n"), store: false, }), onError: () => {}, }) for await (const part of result.fullStream) { if (part.type === "error") throw part.error } return result.object }) } return yield* Effect.promise(() => generateObject(params).then((r) => r.object)) }), }) }), ) // altimate_change start — Layer.suspend defers facade .defaultLayer reads past circular module-init export const defaultLayer = Layer.suspend(() => layer.pipe( Layer.provide(Plugin.defaultLayer), Layer.provide(Provider.defaultLayer), Layer.provide(Auth.defaultLayer), Layer.provide(Config.defaultLayer), Layer.provide(Skill.defaultLayer), Layer.provide(LocationServiceMap.layer), ), ) // altimate_change end const locationServiceMapNode = LayerNode.make(LocationServiceMap.layer, []) // altimate_change start — upstream_fix: thunk defers reading cyclically-imported facade // `.node` exports (Plugin/Provider/...) until buildLayer runs, avoiding load-time undefined. export const node = LayerNode.make(layer, () => [ Config.node, Auth.node, Plugin.node, Skill.node, Provider.node, locationServiceMapNode, ]) // altimate_change end // altimate_change start — restore the imperative Promise wrapper upstream removed in the // Effect-only migration; the HTTP server consumes Agent.list() directly. const { runPromise: runAgent } = makeRuntime(Service, defaultLayer as Layer.Layer) export async function list() { return runAgent((s) => s.list()) } export async function get(agent: string) { return runAgent((s) => s.get(agent)) } export async function defaultAgent() { return runAgent((s) => s.defaultAgent()) } // altimate_change end export * as Agent from "./agent"