Skip to content

Commit

Permalink
refactor(api): update SupportProxy type to use boolean and enhance cr…
Browse files Browse the repository at this point in the history
…eateCallable with state management
  • Loading branch information
Sma1lboy committed Jan 21, 2025
1 parent 9d8fbbe commit 02e80a6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 17 deletions.
2 changes: 1 addition & 1 deletion clients/tabby-chat-panel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export interface ClientApiMethods {
type ClientApiMethod = keyof ClientApiMethods

type SupportProxy = {
[K in ClientApiMethod]: Promise<boolean>
[K in ClientApiMethod]: boolean
}
export interface ClientApi extends ClientApiMethods {
/**
Expand Down
76 changes: 61 additions & 15 deletions clients/tabby-threads/source/targets/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import type {
ThreadEncoder,
ThreadEncoderApi,
AnyFunction,
} from "../types.ts";

import {
RELEASE_METHOD,
RETAINED_BY,
RETAIN_METHOD,
StackFrame,
isMemoryManageable,
} from "../memory";
} from "../types";

import { RELEASE_METHOD, RETAINED_BY, RETAIN_METHOD } from "../constants";

import { StackFrame, isMemoryManageable } from "../memory";
import { createBasicEncoder } from "../encoding/basic";

export type { ThreadTarget };
Expand Down Expand Up @@ -79,8 +75,7 @@ interface MessageMap {
[FUNCTION_APPLY]: [string, string, any];
[FUNCTION_RESULT]: [string, Error?, any?];
[CHECK_CAPABILITY]: [string, string];
[EXPOSE_LIST]: [string, string[]]; // Request to exchange methods: [callId, our_methods]
// The other side will respond with their methods via RESULT
[EXPOSE_LIST]: [string, string[]];
}

type MessageData = {
Expand Down Expand Up @@ -197,10 +192,18 @@ export async function createThread<

// Create proxy for method calls
console.log("[createThread] Creating proxy without waiting for response");
const call = createCallable<Thread<Target>>(handlerForCall, callable, {
exchangeMethods,
requestMethods,
});
const call = createCallable<Thread<Target>>(
handlerForCall,
callable,
{
exchangeMethods,
requestMethods,
},
{
getMethodsCache: () => theirMethodsCache,
isTerminated: () => terminated,
}
);

const encoderApi: ThreadEncoderApi = {
functions: {
Expand Down Expand Up @@ -575,6 +578,10 @@ function createCallable<T>(
methods?: {
exchangeMethods: () => void;
requestMethods: () => Promise<string[]>;
},
state?: {
getMethodsCache: () => string[] | null;
isTerminated: () => boolean;
}
): T {
console.log("[createCallable] Creating callable with methods:", callable);
Expand Down Expand Up @@ -602,6 +609,21 @@ function createCallable<T>(
console.log("[createCallable] Accessing requestMethods");
return methods?.requestMethods;
}
if (property === "supports") {
return new Proxy(
{},
{
get(_target, method: string) {
if (!state) return false;
const cache = state.getMethodsCache();
if (cache !== null && !state.isTerminated()) {
return cache.includes(method);
}
return false;
},
}
);
}
console.log("[createCallable] Accessing property:", property);
if (cache.has(property)) {
console.log("[createCallable] Using cached handler for:", property);
Expand All @@ -613,6 +635,30 @@ function createCallable<T>(
cache.set(property, handler);
return handler;
},
has(_target, property) {
console.log("[createCallable] Checking has property:", property);
if (
property === "then" ||
property === "exchangeMethods" ||
property === "requestMethods"
) {
return true;
}
if (!state) {
console.log("[createCallable] No state available, returning false");
return false;
}
const cache = state.getMethodsCache();
if (cache !== null && !state.isTerminated()) {
console.log(
"[createCallable] Checking cache for method:",
property
);
return cache.includes(String(property));
}
console.log("[createCallable] No cache available, returning false");
return false;
},
}
);
} else {
Expand Down
9 changes: 8 additions & 1 deletion ee/tabby-ui/app/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ export default function ChatPage() {
server?.onLoaded({
apiVersion: TABBY_CHAT_PANEL_API_VERSION
})

if ('refresh' in server) {
// eslint-disable-next-line no-console
console.log('refresh in server')
}
// eslint-disable-next-line no-console
console.log('support v2?:', server?.supports['onApplyInEditorV2'])

const checkCapabilities = async () => {
server
?.hasCapability('onApplyInEditorV2')
Expand All @@ -267,7 +275,6 @@ export default function ChatPage() {
)
})
}

checkCapabilities().then(() => {
setIsServerLoaded(true)
})
Expand Down

0 comments on commit 02e80a6

Please sign in to comment.