Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat(web): pass workspace_id explicitly on all API calls

Gener8dbcd1cf1536
diff --git a/src/web/src/components/dashboard-navbar.tsx b/src/web/src/components/dashboard-navbar.tsx
index 70e2cb4a6..c9f70fff8 100644
--- a/src/web/src/components/dashboard-navbar.tsx
+++ b/src/web/src/components/dashboard-navbar.tsx
@@ -172,14 +172,18 @@ export function DashboardNavbar() {
}
};
+ // NOTE: This component is currently unused. The workspaceId is hardcoded
+ // as a placeholder — if this component is revived it needs a workspace prop.
+ const workspaceId = ""
const loadRuntimes = useCallback(async () => {
+ if (!workspaceId) return
try {
- const r = await listRuntimes();
+ const r = await listRuntimes(workspaceId);
setRuntimes(r);
} catch {
// silent — runtimes are supplementary
}
- }, []);
+ }, [workspaceId]);
useEffect(() => {
loadRuntimes();
@@ -189,7 +193,7 @@ export function DashboardNavbar() {
setGeneratingToken(true);
setTokenCopied(false);
try {
- const res = await createMachineToken("cli");
+ const res = await createMachineToken("cli", workspaceId);
setGeneratedToken(res.token);
} catch (err) {
toast.error(
@@ -287,8 +291,8 @@ export function DashboardNavbar() {
`This will remove "${displayName}" and all its runtimes. Agents using these runtimes will be unlinked.`,
async () => {
try {
- await deleteMachine(daemonId);
- const r = await listRuntimes();
+ await deleteMachine(daemonId, workspaceId);
+ const r = await listRuntimes(workspaceId);
setRuntimes(r);
} catch (err) {
toast.error(
diff --git a/src/web/src/contexts/agent-context.tsx b/src/web/src/contexts/agent-context.tsx
index 62ceb63e1..1e27ed35f 100644
--- a/src/web/src/contexts/agent-context.tsx
+++ b/src/web/src/contexts/agent-context.tsx
@@ -49,7 +49,13 @@ export function useAgentContext() {
return ctx;
}
-export function AgentProvider({ children }: { children: ReactNode }) {
+export function AgentProvider({
+ workspaceId,
+ children,
+}: {
+ workspaceId: string;
+ children: ReactNode;
+}) {
const [agents, setAgents] = useState<Agent[]>([]);
const [runtimes, setRuntimes] = useState<Runtime[]>([]);
const [loading, setLoading] = useState(true);
@@ -57,7 +63,10 @@ export function AgentProvider({ children }: { children: ReactNode }) {
const reload = useCallback(async () => {
try {
- const [a, r] = await Promise.all([listAgents(), listRuntimes()]);
+ const [a, r] = await Promise.all([
+ listAgents(workspaceId),
+ listRuntimes(workspaceId),
+ ]);
setAgents(a);
setRuntimes(r);
} catch (err) {
@@ -66,7 +75,7 @@ export function AgentProvider({ children }: { children: ReactNode }) {
setLoading(false);
loadedRef.current = true;
}
- }, []);
+ }, [workspaceId]);
useEffect(() => {
reload();
@@ -75,7 +84,7 @@ export function AgentProvider({ children }: { children: ReactNode }) {
const handleCreateAgent = useCallback(
async (req: CreateAgentRequest): Promise<Agent | null> => {
try {
- const agent = await createAgent(req);
+ const agent = await createAgent(req, workspaceId);
await reload();
return agent;
} catch (err) {
@@ -85,13 +94,13 @@ export function AgentProvider({ children }: { children: ReactNode }) {
return null;
}
},
- [reload]
+ [reload, workspaceId]
);
const handleUpdateAgent = useCallback(
async (id: string, req: UpdateAgentRequest): Promise<boolean> => {
try {
- await updateAgent(id, req);
+ await updateAgent(id, req, workspaceId);
await reload();
return true;
} catch (err) {
@@ -101,13 +110,13 @@ export function AgentProvider({ children }: { children: ReactNode }) {
return false;
}
},
- [reload]
+ [reload, workspaceId]
);
const handleDeleteAgent = useCallback(
async (id: string): Promise<boolean> => {
try {
- await deleteAgent(id);
+ await deleteAgent(id, workspaceId);
await reload();
return true;
} catch (err) {
@@ -117,13 +126,13 @@ export function AgentProvider({ children }: { children: ReactNode }) {
return false;
}
},
- [reload]
+ [reload, workspaceId]
);
const chatWithAgent = useCallback(
async (agentId: string): Promise<string | null> => {
try {
- const conversation = await createConversation(agentId);
+ const conversation = await createConversation(agentId, workspaceId);
return conversation.id;
} catch (err) {
toast.error(
@@ -132,7 +141,7 @@ export function AgentProvider({ children }: { children: ReactNode }) {
return null;
}
},
- []
+ [workspaceId]
);
const getFirstOnlineRuntimeId = useCallback(() => {
@@ -142,7 +151,7 @@ export function AgentProvider({ children }: { children: ReactNode }) {
const handleGenerateToken = useCallback(async (): Promise<string | null> => {
try {
- const res = await createMachineToken("cli");
+ const res = await createMachineToken("cli", workspaceId);
return res.token;
} catch (err) {
toast.error(
@@ -150,12 +159,12 @@ export function AgentProvider({ children }: { children: ReactNode }) {
);
return null;
}
- }, []);
+ }, [workspaceId]);
const handleDeleteMachine = useCallback(
async (daemonId: string): Promise<boolean> => {
try {
- await deleteMachine(daemonId);
+ await deleteMachine(daemonId, workspaceId);
await reload();
return true;
} catch (err) {
@@ -165,7 +174,7 @@ export function AgentProvider({ children }: { children: ReactNode }) {
return false;
}
},
- [reload]
+ [reload, workspaceId]
);
return (
diff --git a/src/web/src/lib/api.ts b/src/web/src/lib/api.ts
index 171335450..01cd40185 100644
--- a/src/web/src/lib/api.ts
+++ b/src/web/src/lib/api.ts
@@ -20,11 +20,6 @@ export type Task = AgentTask;
const API_BASE = "";
async function apiFetch<T>(path: string, options?: RequestInit): Promise<T> {
- const workspaceId =
- typeof window !== "undefined"
- ? localStorage.getItem("alook_workspace_id")
- : null;
-
let res: Response;
try {
res = await fetch(API_BASE + path, {
@@ -32,7 +27,6 @@ async function apiFetch<T>(path: string, options?: RequestInit): Promise<T> {
credentials: "include",
headers: {
"Content-Type": "application/json",
- ...(workspaceId && { "X-Workspace-ID": workspaceId }),
...options?.headers,
},
});
@@ -45,7 +39,6 @@ async function apiFetch<T>(path: string, options?: RequestInit): Promise<T> {
if (res.status === 401) {
if (typeof window !== "undefined") {
- localStorage.removeItem("alook_workspace_id");
window.location.href = "/sign-in";
}
throw new ApiError("Unauthorized", 401);
@@ -97,57 +90,66 @@ export const createWorkspace = (name: string) =>
body: JSON.stringify({ name }),
});
+// Helper to build query strings with workspace_id
+function wsQuery(workspaceId: string, extra?: Record<string, string>): string {
+ const params = new URLSearchParams({ workspace_id: workspaceId, ...extra });
+ return `?${params.toString()}`;
+}
+
// Agents
-export const listAgents = () => apiFetch<Agent[]>("/api/agents");
+export const listAgents = (workspaceId: string) =>
+ apiFetch<Agent[]>(`/api/agents${wsQuery(workspaceId)}`);
-export const createAgent = (req: CreateAgentRequest) =>
- apiFetch<Agent>("/api/agents", {
+export const createAgent = (req: CreateAgentRequest, workspaceId: string) =>
+ apiFetch<Agent>(`/api/agents${wsQuery(workspaceId)}`, {
method: "POST",
body: JSON.stringify(req),
});
-export const updateAgent = (id: string, req: UpdateAgentRequest) =>
- apiFetch<Agent>(`/api/agents/${id}`, {
+export const updateAgent = (id: string, req: UpdateAgentRequest, workspaceId: string) =>
+ apiFetch<Agent>(`/api/agents/${id}${wsQuery(workspaceId)}`, {
method: "PATCH",
body: JSON.stringify(req),
});
-export const deleteAgent = (id: string) =>
- apiFetch<void>(`/api/agents/${id}`, { method: "DELETE" });
+export const deleteAgent = (id: string, workspaceId: string) =>
+ apiFetch<void>(`/api/agents/${id}${wsQuery(workspaceId)}`, { method: "DELETE" });
// Runtimes
-export const listRuntimes = () => apiFetch<Runtime[]>("/api/runtimes");
+export const listRuntimes = (workspaceId: string) =>
+ apiFetch<Runtime[]>(`/api/runtimes${wsQuery(workspaceId)}`);
-export const deleteMachine = (daemonId: string) =>
- apiFetch<void>(`/api/runtimes/machine?daemon_id=${encodeURIComponent(daemonId)}`, {
- method: "DELETE",
- });
+export const deleteMachine = (daemonId: string, workspaceId: string) =>
+ apiFetch<void>(
+ `/api/runtimes/machine${wsQuery(workspaceId, { daemon_id: daemonId })}`,
+ { method: "DELETE" }
+ );
// Conversations
-export const listConversations = () =>
- apiFetch<Conversation[]>("/api/conversations");
+export const listConversations = (workspaceId: string) =>
+ apiFetch<Conversation[]>(`/api/conversations${wsQuery(workspaceId)}`);
-export const createConversation = (agentId: string) =>
- apiFetch<Conversation>("/api/conversations", {
+export const createConversation = (agentId: string, workspaceId: string) =>
+ apiFetch<Conversation>(`/api/conversations${wsQuery(workspaceId)}`, {
method: "POST",
body: JSON.stringify({ agent_id: agentId }),
});
-export const getConversation = (id: string) =>
- apiFetch<Conversation>(`/api/conversations/${id}`);
+export const getConversation = (id: string, workspaceId: string) =>
+ apiFetch<Conversation>(`/api/conversations/${id}${wsQuery(workspaceId)}`);
-export const listAgentConversations = (agentId: string) =>
- apiFetch<Conversation[]>(`/api/agents/${agentId}/conversations`);
+export const listAgentConversations = (agentId: string, workspaceId: string) =>
+ apiFetch<Conversation[]>(`/api/agents/${agentId}/conversations${wsQuery(workspaceId)}`);
-export const deleteConversation = (id: string) =>
- apiFetch<void>(`/api/conversations/${id}`, { method: "DELETE" });
+export const deleteConversation = (id: string, workspaceId: string) =>
+ apiFetch<void>(`/api/conversations/${id}${wsQuery(workspaceId)}`, { method: "DELETE" });
-export const listMessages = (conversationId: string) =>
- apiFetch<Message[]>(`/api/conversations/${conversationId}/messages`);
+export const listMessages = (conversationId: string, workspaceId: string) =>
+ apiFetch<Message[]>(`/api/conversations/${conversationId}/messages${wsQuery(workspaceId)}`);
-export const sendMessage = (conversationId: string, content: string) =>
+export const sendMessage = (conversationId: string, content: string, workspaceId: string) =>
apiFetch<{ message: Message; task: Task }>(
- `/api/conversations/${conversationId}/messages`,
+ `/api/conversations/${conversationId}/messages${wsQuery(workspaceId)}`,
{
method: "POST",
body: JSON.stringify({ content }),
@@ -155,9 +157,9 @@ export const sendMessage = (conversationId: string, content: string) =>
);
// Machine tokens
-export const createMachineToken = (name?: string) =>
+export const createMachineToken = (name?: string, workspaceId?: string) =>
apiFetch<{ token: string; id: string; name: string; created_at: string }>(
- "/api/machine-tokens",
+ `/api/machine-tokens${workspaceId ? wsQuery(workspaceId) : ""}`,
{
method: "POST",
body: JSON.stringify({ name: name || "default" }),
@@ -165,17 +167,17 @@ export const createMachineToken = (name?: string) =>
);
// Tasks (polling)
-export const getTask = (id: string) => apiFetch<Task>(`/api/tasks/${id}`);
+export const getTask = (id: string, workspaceId: string) =>
+ apiFetch<Task>(`/api/tasks/${id}${wsQuery(workspaceId)}`);
-export const getTaskMessages = (id: string, since?: number) =>
+export const getTaskMessages = (id: string, workspaceId: string, since?: number) =>
apiFetch<TaskMessage[]>(
- `/api/tasks/${id}/messages${since ? `?since=${since}` : ""}`
+ `/api/tasks/${id}/messages${wsQuery(workspaceId, since ? { since: String(since) } : undefined)}`
);
// Auth (Better Auth — redirect helpers only, actual auth via Better Auth client)
export const signOut = async () => {
if (typeof window !== "undefined") {
- localStorage.removeItem("alook_workspace_id");
window.location.href = "/sign-in";
}
};