refactor: replace global singletons with AgentContext instance - #16
Merged
Conversation
hewliyang
force-pushed
the
refactor/agent-context
branch
2 times, most recently
from
March 23, 2026 13:09
fbf00c0 to
903b486
Compare
- Introduce AgentContext class that owns VFS, bash, and StorageNamespace - AgentRuntime accepts optional pre-built AgentContext (ChatController creates it) - Tools become factories (createBashTool, createReadTool, etc.) that receive context - Adapter.tools accepts (ctx: AgentContext) => AgentTool[] factory pattern - All localStorage helpers take StorageNamespace instead of raw prefix strings - Storage/db functions take StorageNamespace as first param - Skills functions take namespace + context explicitly - customCommands adapter receives (ns: StorageNamespace) from runtime - Remove all module-level VFS singletons and configureNamespace/getNamespace - Bridge VFS adapter matches AgentContext shape (snapshotVfs, readFile, etc.) - Bridge accepts lazy vfs getter: () => BridgeVfsAdapter | undefined - ChatInterface exposes onController callback for bridge wiring - App.svelte files pass context directly as bridge VFS adapter - Update all tests to use AgentContext instances
hewliyang
force-pushed
the
refactor/agent-context
branch
from
March 23, 2026 13:18
903b486 to
9878b65
Compare
…er config in runtime init - setSkillFiles/setStaticFiles now patch the live InMemoryFs in-place instead of calling reset(), which was destroying user uploads and generated files - setCustomCommands only resets bash (no FS mutation needed) - AgentRuntime.init() now applies adapter.staticFiles and adapter.customCommands to the context, so external SDK consumers don't silently lose VFS files and command snippets - Added customCommands to RuntimeAdapter interface
- Replace global singleton examples with AgentContext pattern - Document RuntimeAdapter.staticFiles/customCommands and init() behavior - Update all code examples (tools, sessions, skills, provider config) - Add AgentContext, custom commands, and built-in tools sections - All storage/skill functions now show namespace parameter - Add Word to Used By section
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the scattered module-level singleton pattern (
setCustomCommands(),configureNamespace(),getVfs(),getBash(), etc.) with a singleAgentContextclass thatAgentRuntimeowns and threads to all consumers.What changed
New:
AgentContext(packages/sdk/src/context.ts)InMemoryFs), bash (Bash), andStorageNamespacereadFile,writeFile,deleteFile,fileExists,listUploads)reset,restoreVfs,snapshotVfs)AgentRuntime, passed explicitly to tools and storage functionsTools → factories
bashTool→createBashTool(ctx: AgentContext)readTool→createReadTool(ctx: AgentContext)evalOfficeJsTool→createEvalOfficeJsTool(ctx)(Excel, PPT, Word variants)getOoxmlTool→createGetOoxmlTool(ctx)(Word)createExcelTools(ctx),createPptTools(ctx),createWordTools(ctx)Adapter pattern update
AppAdapter.toolsacceptsAgentTool[] | ((ctx: AgentContext) => AgentTool[])— factory or static arrayAppAdapter.customCommandsnow receives(ns: StorageNamespace) => CustomCommandsResultfrom the runtime"openexcel","openppt","openword") in custom command fileslocalStorage helpers take
StorageNamespaceloadSavedConfig(ns),saveConfig(ns, config)loadWebConfig(ns),saveWebConfig(ns, config)loadOAuthCredentials(ns, provider),saveOAuthCredentials(ns, provider, creds),removeOAuthCredentials(ns, provider)chat.context.namespaceinstead of a raw prefix stringStorage functions take
StorageNamespacestorage/db.tsfunctions (createSession,saveSession,listSessions, etc.) takensas first paramstorage/namespace.tsreduced to a type re-exportDeleted globals
setCustomCommands(),setStaticFiles(),setSkillFiles(),resetVfs(),getVfs(),getBash()configureNamespace(),getNamespace()readFile(),writeFile(),readFileBuffer(),fileExists(),deleteFile(),listUploads(),snapshotVfs(),restoreVfs()(all now onAgentContext)vfs/index.tsgutted to pure utilities only (getFileType,detectImageMimeType,toBase64)Tests
AgentContextinstances — no shared global stateresetVfs()/setStaticFiles({})ceremony in beforeEach/afterEachBridge
toolsas array or factoryapp.svelteremoved (bridge VFS support to be re-wired separately)Why
chat.context.namespaceinstead of magic prefix strings