mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 18:01:22 +00:00
Simplify ArtifactsRuntimeProvider constructor
- Take artifactsPanel and agent directly instead of 5 separate function parameters - Define minimal ArtifactsPanelLike and AgentLike interfaces to avoid circular deps - Update all call sites (ChatPanel, browser-javascript) to use simplified constructor - Much cleaner and easier to use
This commit is contained in:
parent
c0ad46fa01
commit
695bcf9b6f
2 changed files with 35 additions and 45 deletions
|
|
@ -112,34 +112,7 @@ export class ChatPanel extends LitElement {
|
|||
}
|
||||
|
||||
// Add artifacts provider (always available)
|
||||
providers.push(
|
||||
new ArtifactsRuntimeProvider(
|
||||
() => this.artifactsPanel!.artifacts,
|
||||
async (filename: string, content: string) => {
|
||||
await this.artifactsPanel!.tool.execute("", {
|
||||
command: "create",
|
||||
filename,
|
||||
content,
|
||||
});
|
||||
},
|
||||
async (filename: string, content: string) => {
|
||||
await this.artifactsPanel!.tool.execute("", {
|
||||
command: "rewrite",
|
||||
filename,
|
||||
content,
|
||||
});
|
||||
},
|
||||
async (filename: string) => {
|
||||
await this.artifactsPanel!.tool.execute("", {
|
||||
command: "delete",
|
||||
filename,
|
||||
});
|
||||
},
|
||||
(message: any) => {
|
||||
this.agent!.appendMessage(message);
|
||||
},
|
||||
),
|
||||
);
|
||||
providers.push(new ArtifactsRuntimeProvider(this.artifactsPanel!, this.agent!));
|
||||
|
||||
return providers;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,6 +1,18 @@
|
|||
import { ARTIFACTS_RUNTIME_PROVIDER_DESCRIPTION } from "../../prompts/tool-prompts.js";
|
||||
import type { SandboxRuntimeProvider } from "./SandboxRuntimeProvider.js";
|
||||
|
||||
// Define minimal interface for ArtifactsPanel to avoid circular dependencies
|
||||
interface ArtifactsPanelLike {
|
||||
artifacts: Map<string, { content: string }>;
|
||||
tool: {
|
||||
execute(toolCallId: string, args: { command: string; filename: string; content?: string }): Promise<any>;
|
||||
};
|
||||
}
|
||||
|
||||
interface AgentLike {
|
||||
appendMessage(message: any): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Artifacts Runtime Provider
|
||||
*
|
||||
|
|
@ -10,18 +22,14 @@ import type { SandboxRuntimeProvider } from "./SandboxRuntimeProvider.js";
|
|||
*/
|
||||
export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
|
||||
constructor(
|
||||
private getArtifactsFn: () => Map<string, { content: string }>,
|
||||
private createArtifactFn: (filename: string, content: string, title?: string) => Promise<void>,
|
||||
private updateArtifactFn: (filename: string, content: string, title?: string) => Promise<void>,
|
||||
private deleteArtifactFn: (filename: string) => Promise<void>,
|
||||
private appendMessageFn?: (message: any) => void,
|
||||
private artifactsPanel: ArtifactsPanelLike,
|
||||
private agent?: AgentLike,
|
||||
) {}
|
||||
|
||||
getData(): Record<string, any> {
|
||||
// Inject artifact snapshot for offline mode
|
||||
const snapshot: Record<string, string> = {};
|
||||
const artifacts = this.getArtifactsFn();
|
||||
artifacts.forEach((artifact, filename) => {
|
||||
this.artifactsPanel.artifacts.forEach((artifact, filename) => {
|
||||
snapshot[filename] = artifact.content;
|
||||
});
|
||||
return { artifacts: snapshot };
|
||||
|
|
@ -153,15 +161,13 @@ export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
|
|||
try {
|
||||
switch (action) {
|
||||
case "has": {
|
||||
const artifacts = this.getArtifactsFn();
|
||||
const exists = artifacts.has(filename);
|
||||
const exists = this.artifactsPanel.artifacts.has(filename);
|
||||
respond({ success: true, result: exists });
|
||||
break;
|
||||
}
|
||||
|
||||
case "get": {
|
||||
const artifacts = this.getArtifactsFn();
|
||||
const artifact = artifacts.get(filename);
|
||||
const artifact = this.artifactsPanel.artifacts.get(filename);
|
||||
if (!artifact) {
|
||||
respond({ success: false, error: `Artifact not found: ${filename}` });
|
||||
} else {
|
||||
|
|
@ -172,8 +178,12 @@ export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
|
|||
|
||||
case "create": {
|
||||
try {
|
||||
await this.createArtifactFn(filename, content, filename);
|
||||
this.appendMessageFn?.({
|
||||
await this.artifactsPanel.tool.execute("", {
|
||||
command: "create",
|
||||
filename,
|
||||
content,
|
||||
});
|
||||
this.agent?.appendMessage({
|
||||
role: "artifact",
|
||||
action: "create",
|
||||
filename,
|
||||
|
|
@ -190,8 +200,12 @@ export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
|
|||
|
||||
case "update": {
|
||||
try {
|
||||
await this.updateArtifactFn(filename, content, filename);
|
||||
this.appendMessageFn?.({
|
||||
await this.artifactsPanel.tool.execute("", {
|
||||
command: "rewrite",
|
||||
filename,
|
||||
content,
|
||||
});
|
||||
this.agent?.appendMessage({
|
||||
role: "artifact",
|
||||
action: "update",
|
||||
filename,
|
||||
|
|
@ -207,8 +221,11 @@ export class ArtifactsRuntimeProvider implements SandboxRuntimeProvider {
|
|||
|
||||
case "delete": {
|
||||
try {
|
||||
await this.deleteArtifactFn(filename);
|
||||
this.appendMessageFn?.({
|
||||
await this.artifactsPanel.tool.execute("", {
|
||||
command: "delete",
|
||||
filename,
|
||||
});
|
||||
this.agent?.appendMessage({
|
||||
role: "artifact",
|
||||
action: "delete",
|
||||
filename,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue