forked from Cloud-Pipelines/pipeline-editor
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: v2 - ai assistant web worker scaffolding #2328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maxy-shpfy
wants to merge
1
commit into
05-26-feat_v2_-_ai_assistant_chat_window
Choose a base branch
from
05-26-feat_v2_-_ai_assistant_web_worker_scaffolding
base: 05-26-feat_v2_-_ai_assistant_chat_window
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /** | ||
| * Shared types between the worker and the main thread. | ||
| */ | ||
|
|
||
| export interface AgentResponse { | ||
| answer: string; | ||
| threadId: string; | ||
| componentReferences: Record<string, { name: string; yamlText: string }>; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /** | ||
| * Web Worker entry point for the in-browser agent. | ||
| * | ||
| * A placeholder `ask()` that echoes the user's message — it | ||
| * proves the bundling, lazy-spawn, and Comlink round-trip are working | ||
| * end-to-end before we wire the LLM and the tool bridge. | ||
| * | ||
| * The `globalThis.process` stub below covers an unguarded | ||
| * `process.env.X` read in `@openai/agents-core` v0.4.x | ||
| * (`runner/sessionPersistence.mjs` reads | ||
| * `process.env.OPENAI_AGENTS__DEBUG_SAVE_SESSION` without a | ||
| * `typeof process` guard) that would otherwise throw | ||
| * `ReferenceError: process is not defined` on every turn that | ||
| * persists session state. The stub deliberately omits `.on` / | ||
| * `.exit` so the SDK's `typeof process.on === 'function'` checks | ||
| * still skip the Node-only branches and we do not pretend to be | ||
| * Node. It is inlined here (rather than living in a separate | ||
| * polyfill file) so Rolldown's worker bundle does not tree-shake | ||
| * the side-effect import, and so it runs in both `vite build` and | ||
| * `vite serve` (dev) modes, which `worker.rolldownOptions.define` | ||
| * would not. | ||
| */ | ||
| const __workerGlobal = globalThis as { process?: { env?: unknown } }; | ||
| if (typeof __workerGlobal.process === "undefined") { | ||
| __workerGlobal.process = { env: {} }; | ||
| } | ||
| // Anchor: keep Rolldown from tree-shaking the assignment above. In | ||
| // `vite build` Vite statically replaces `process.env` with `{}`, so the | ||
| // rest of the bundle never reads `globalThis.process` and Rolldown | ||
| // would otherwise treat the write as dead. Throwing on a hasOwnProperty | ||
| // check forces the write to be observable. | ||
| if (!Object.prototype.hasOwnProperty.call(globalThis, "process")) { | ||
| throw new Error("Tangle agent worker: globalThis.process polyfill failed"); | ||
| } | ||
|
|
||
| import * as Comlink from "comlink"; | ||
|
|
||
| import type { AgentResponse } from "./types"; | ||
|
|
||
| export interface AskParams { | ||
| message: string; | ||
| threadId?: string; | ||
| } | ||
|
|
||
| export interface AgentWorkerApi { | ||
| ask(params: AskParams): Promise<AgentResponse>; | ||
| ping(): Promise<"pong">; | ||
| } | ||
|
|
||
| let emitStatus: (status: { text: string }) => void = () => {}; | ||
|
|
||
| function generateThreadId(): string { | ||
| return `thread-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; | ||
| } | ||
|
|
||
| const api: AgentWorkerApi = { | ||
| async ping() { | ||
| return "pong"; | ||
| }, | ||
|
|
||
| async ask({ message, threadId }) { | ||
| /** | ||
| * todo: replace with actual AI response. | ||
| */ | ||
| return { | ||
| answer: `Worker echo: ${message}`, | ||
| threadId: threadId ?? generateThreadId(), | ||
| componentReferences: {}, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * Initialization entry point. Called once by the main thread immediately | ||
| * after spawning the worker. Splits init from ask() so future bridge / | ||
| * skill plumbing has an explicit lifecycle hook. | ||
| */ | ||
| export function init(onStatus: (status: { text: string }) => void): void { | ||
| emitStatus = onStatus; | ||
| // TODO: read `emitStatus` from the dispatcher's observability hooks; | ||
| // until then this no-op read keeps `noUnusedLocals` quiet without | ||
| // dropping the assignment that locks in the init() contract. | ||
| void emitStatus; | ||
| } | ||
|
|
||
| Comlink.expose({ ...api, init }); |
76 changes: 76 additions & 0 deletions
76
src/routes/v2/pages/Editor/components/AiChat/agentClient.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /** | ||
| * Main-thread client for the in-browser agent worker. | ||
| * | ||
| * Spawns a single Web Worker (lazy, on first use), wires it up over | ||
| * Comlink, and exposes a typed `ask()` method that the AI Chat store | ||
| * calls. | ||
| */ | ||
| import * as Comlink from "comlink"; | ||
|
|
||
| import type { AgentResponse } from "@/agent/types"; | ||
| import type { AgentWorkerApi } from "@/agent/worker"; | ||
|
|
||
| interface WorkerExports extends AgentWorkerApi { | ||
| init(onStatus: (status: { text: string }) => void): void; | ||
| } | ||
|
|
||
| interface InitDeps { | ||
| onStatus: (status: { text: string }) => void; | ||
| } | ||
|
|
||
| interface AskOptions { | ||
| message: string; | ||
| threadId?: string; | ||
| } | ||
|
|
||
| class AgentClient { | ||
| private worker: Worker | null = null; | ||
| private remote: Comlink.Remote<WorkerExports> | null = null; | ||
| private initPromise: Promise<void> | null = null; | ||
|
|
||
| private async ensureInit( | ||
| deps: InitDeps, | ||
| ): Promise<Comlink.Remote<WorkerExports>> { | ||
| if (!this.worker) { | ||
| this.worker = new Worker(new URL("@/agent/worker.ts", import.meta.url), { | ||
| type: "module", | ||
| name: "tangle-agent", | ||
| }); | ||
| this.remote = Comlink.wrap<WorkerExports>(this.worker); | ||
| } | ||
| if (!this.remote) { | ||
| throw new Error("Worker remote was not created"); | ||
| } | ||
| if (!this.initPromise) { | ||
| // Pass onStatus as a top-level Comlink-proxied arg. | ||
| // Each new one must stay a separate top-level argument because Comlink only applies its proxy | ||
| // transfer handler to top-level argument values, it does not | ||
| // recursively walk into properties of an object arg. | ||
| // | ||
| // Wrapping proxied values in a single object would cause structured-clone | ||
| // of the methods and fail. | ||
| this.initPromise = this.remote.init(Comlink.proxy(deps.onStatus)); | ||
| } | ||
| await this.initPromise; | ||
| return this.remote; | ||
| } | ||
|
|
||
| async ask(deps: InitDeps, options: AskOptions): Promise<AgentResponse> { | ||
| const remote = await this.ensureInit(deps); | ||
| return remote.ask(options); | ||
| } | ||
|
|
||
| terminate(): void { | ||
| this.worker?.terminate(); | ||
| this.worker = null; | ||
| this.remote = null; | ||
| this.initPromise = null; | ||
| } | ||
| } | ||
|
|
||
| let singleton: AgentClient | null = null; | ||
|
|
||
| export function getAgentClient(): AgentClient { | ||
| if (!singleton) singleton = new AgentClient(); | ||
| return singleton; | ||
| } |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be kept in? there is a lot of
shopifyreferences