Skip to content
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

use denops-bind-params instead of denops-arg-store #26

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions autoload/ollama/custom.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function ollama#custom#set_func_arg(function_name, arg_name, value)
call denops#request("ollama", "customSetFuncArg", [a:function_name, a:arg_name, a:value])
call denops#request("ollama", "params:set-one", [a:function_name, a:arg_name, a:value])
endfunction

function ollama#custom#patch_func_args(function_name, args)
call denops#request("ollama", "customPatchFuncArgs", [a:function_name, a:args])
call denops#request("ollama", "params:set-for-method", [a:function_name, a:args])
endfunction
4 changes: 2 additions & 2 deletions denops/ollama/dispatch/pull_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { pullModel as pullModelAPI } from "../api.ts";
import { getLogger } from "https://deno.land/[email protected]/log/mod.ts";
import * as helper from "https://deno.land/x/[email protected]/helper/mod.ts";
import { canceller } from "../util/cancellable.ts";
import { abortableAsyncIterable } from "https://deno.land/[email protected]/async/mod.ts";
import { abortable } from "https://deno.land/[email protected]/async/mod.ts";
import { isReqArgs } from "./types.ts";
import {
is,
Expand Down Expand Up @@ -33,7 +33,7 @@ export async function pullModel(denops: Denops, args: PullModelArgs) {
return;
}
for await (
const item of abortableAsyncIterable(result.body.values(), signal)
const item of abortable(result.body.values(), signal)
) {
if ("error" in item) throw new Error(item.error);
const words: string[] = [];
Expand Down
4 changes: 2 additions & 2 deletions denops/ollama/dispatch/start_chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { abortableAsyncIterable } from "https://deno.land/[email protected]/async/mod.ts";
import { abortable } from "https://deno.land/[email protected]/async/mod.ts";
import { Denops } from "https://deno.land/x/[email protected]/mod.ts";
import {
is,
Expand Down Expand Up @@ -59,7 +59,7 @@ class Chat extends ChatBase<number[]> {
return;
}
for await (
const item of abortableAsyncIterable(result.body.values(), signal)
const item of abortable(result.body.values(), signal)
) {
if ("error" in item) throw new Error(item.error);

Expand Down
4 changes: 2 additions & 2 deletions denops/ollama/dispatch/start_chat_in_ctx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { abortableAsyncIterable } from "https://deno.land/[email protected]/async/mod.ts";
import { abortable } from "https://deno.land/[email protected]/async/mod.ts";
import { type Denops } from "https://deno.land/x/[email protected]/mod.ts";
import {
is,
Expand Down Expand Up @@ -112,7 +112,7 @@ class Chat extends ChatBase<GenerateChatCompletionMessage[]> {
return;
}
for await (
const item of abortableAsyncIterable(result.body.values(), signal)
const item of abortable(result.body.values(), signal)
) {
if ("error" in item) throw new Error(item.error);
if (!item.message) continue;
Expand Down
87 changes: 22 additions & 65 deletions denops/ollama/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { Denops } from "https://deno.land/x/[email protected]/mod.ts";
import {
ensure,
is,
maybe,
Predicate,
} from "https://deno.land/x/[email protected]/mod.ts";
import { Denops } from "jsr:@denops/[email protected]";
import { ensure, is, maybe } from "jsr:@core/[email protected]";
import { bindDispatcher } from "jsr:@kyoh86/[email protected]";

import { init } from "./dispatch/init.ts";
import { isStartChatArgs, startChat } from "./dispatch/start_chat.ts";
Expand All @@ -16,84 +12,45 @@ import {
startChatInCtx,
} from "./dispatch/start_chat_in_ctx.ts";
import { complete, isCompleteArgs } from "./dispatch/complete.ts";
import {
ArgStore,
isArgs,
} from "https://denopkg.com/kyoh86/denops-arg-store@master/mod.ts";
import { isOpenLogArgs, openLog } from "./dispatch/open_log.ts";

export async function main(denops: Denops) {
const { cacheFile } = await init(denops);

const argStore = new ArgStore();

function ensureArgs<T>(func: string, uArgs: unknown, pred: Predicate<T>): T {
return ensure(argStore.getArgs(func, ensure(uArgs, isArgs)), pred);
}

denops.dispatcher = {
async open_log(uArgs: unknown) {
const args = ensureArgs("open_log", uArgs, isOpenLogArgs);
await openLog(denops, cacheFile, args);
denops.dispatcher = bindDispatcher({
async open_log(uParams: unknown) {
await openLog(denops, cacheFile, ensure(uParams, isOpenLogArgs));
},

async start_chat(uArgs: unknown) {
const args = ensureArgs("start_chat", uArgs, isStartChatArgs);
await startChat(denops, args);
async start_chat(uParams: unknown) {
await startChat(denops, ensure(uParams, isStartChatArgs));
},

async start_chat_in_ctx(uArgs: unknown) {
const args = ensureArgs(
"start_chat_in_ctx",
uArgs,
isStartChatInCtxArgs,
);
await startChatInCtx(denops, args);
async start_chat_in_ctx(uParams: unknown) {
await startChatInCtx(denops, ensure(uParams, isStartChatInCtxArgs));
},

async complete(uArgs: unknown) {
const args = ensureArgs("complete", uArgs, isCompleteArgs);
const callbackName = maybe(args.callback, is.String);
const callbackFunc = maybe(args.callback, is.AsyncFunction);
async complete(uParams: unknown) {
const params = ensure(uParams, isCompleteArgs);
const callbackName = maybe(params.callback, is.String);
const callbackFunc = maybe(params.callback, is.AsyncFunction);
const callback = callbackFunc ?? (async (msg: string) => {
await denops.call("denops#callback#call", callbackName, msg);
});

await complete(denops, { ...args, callback });
},

async list_models(uArgs: unknown) {
const args = ensureArgs("list_models", uArgs, isListModelsArgs);
await listModels(denops, args);
},

async pull_model(uArgs: unknown) {
const args = ensureArgs("pull_model", uArgs, isPullModelArgs);
await pullModel(denops, args);
},

async delete_model(uArgs: unknown) {
const args = ensureArgs("delete_model", uArgs, isDeleteModelArgs);
await deleteModel(denops, args);
await complete(denops, { ...params, callback });
},

customSetFuncArg(uFunc: unknown, uArg: unknown, value: unknown) {
argStore.setFuncArg(
ensure(uFunc, is.String),
ensure(uArg, is.String),
value,
);
async list_models(uParams: unknown) {
await listModels(denops, ensure(uParams, isListModelsArgs));
},

customPatchFuncArgs(uFunc: unknown, uArgs: unknown) {
argStore.patchFuncArgs(
ensure(uFunc, is.String),
ensure(uArgs, isArgs),
);
async pull_model(uParams: unknown) {
await pullModel(denops, ensure(uParams, isPullModelArgs));
},

customPatchArgs(uArgs: unknown) {
argStore.patchArgs(ensure(uArgs, is.RecordOf(isArgs)));
async delete_model(uParams: unknown) {
await deleteModel(denops, ensure(uParams, isDeleteModelArgs));
},
};
});
}
4 changes: 2 additions & 2 deletions denops/ollama/ui/spinner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import spinners from "npm:cli-spinners@3.0.0";
import { type SpinnerName as Kind } from "npm:cli-spinners@3.0.0";
import spinners from "npm:cli-spinners@3.1.0";
import { type SpinnerName as Kind } from "npm:cli-spinners@3.1.0";
import { type Denops } from "https://deno.land/x/[email protected]/mod.ts";
import * as batch from "https://deno.land/x/[email protected]/batch/mod.ts";
import * as fn from "https://deno.land/x/[email protected]/function/mod.ts";
Expand Down
2 changes: 1 addition & 1 deletion denops/ollama/ui/spinner_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test } from "https://deno.land/x/[email protected]/mod.ts";
import { bufnr } from "https://deno.land/x/[email protected]/function/mod.ts";
import * as target from "./spinner.ts";
import { default as sp, type SpinnerName } from "npm:cli-spinners@3.0.0";
import { default as sp, type SpinnerName } from "npm:cli-spinners@3.1.0";

test({
mode: "all",
Expand Down
10 changes: 9 additions & 1 deletion doc/ollama.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Customize |ollama-custom|
Function |ollama-function|
Commands |ollama-commands|
Keymaps |ollama-keymaps|
Denops commands |ollama-denops-commands|
API |ollama-api|
Other references |ollama-references|


Expand Down Expand Up @@ -329,6 +329,14 @@ ollama#custom#patch_func_args({function_name}, {args})
# should use the specified values for all functions.
<

==============================================================================
*ollama-api*
API ~

There're some denops API functions to interact with Ollama.

TODO:

==============================================================================
*ollama-references*
Other references ~
Expand Down