@taskyon/tyclient 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/tyclient.mjs CHANGED
@@ -129,22 +129,19 @@ with the function name.`
129
129
  // ../taskyon/src/types/node.ts
130
130
  var Annotation = z3.union([
131
131
  z3.object({
132
- type: z3.literal("url_citation"),
133
- url_citation: z3.object({
134
- end_index: z3.number(),
135
- start_index: z3.number(),
136
- title: z3.string(),
137
- url: z3.string(),
138
- content: z3.string()
139
- }).partial().optional()
140
- }),
132
+ type: z3.literal("url"),
133
+ id: z3.string(),
134
+ end_index: z3.number(),
135
+ start_index: z3.number(),
136
+ title: z3.string(),
137
+ url: z3.string(),
138
+ content: z3.string()
139
+ }).partial().required({ type: true }),
141
140
  z3.object({
142
- type: z3.literal("file"),
143
- content: z3.object({
144
- text: z3.string(),
145
- type: z3.string()
146
- }).array().optional()
147
- })
141
+ type: z3.literal("document"),
142
+ text: z3.string().optional(),
143
+ content: z3.string().optional()
144
+ }).partial().required({ type: true })
148
145
  ]);
149
146
  var MessageContent = z3.object({
150
147
  type: z3.literal("message"),
@@ -595,19 +592,19 @@ function MessageChannelBridge(dport, mport) {
595
592
 
596
593
  // ../taskyon/src/api/index.ts
597
594
  var createChatCompletionTask = (args) => toolCall({ name: "chatCompletion", arguments: args });
598
- var processTasks = (tyPort) => async (taskList, opts) => {
595
+ var sendTasks = (tyPort) => async (taskList, opts) => {
599
596
  const tasks = await forgeTaskChain(taskList);
600
597
  tyPort.send({
601
598
  type: "tasks",
602
599
  tasks,
603
600
  execute: true,
604
- show: true,
601
+ show: opts.show ?? true,
605
602
  // we want to show this task in our GUI as a succesful test
606
- origin: "Taskyon Diagnostics"
603
+ origin: window.origin
607
604
  });
608
605
  const initialIds = tasks.map((t) => t.id);
609
606
  const subTasks = new Set(initialIds);
610
- const subTasksCreated = tyPort.receive.narrow((m) => {
607
+ const subTaskStream = tyPort.receive.narrow((m) => {
611
608
  console.log("api received", m);
612
609
  if (m.type === "taskCreated" && "task" in m && !!m.task?.id && !!m.task?.parentID && subTasks.has(m.task?.parentID)) {
613
610
  subTasks.add(m.task?.id);
@@ -615,12 +612,19 @@ var processTasks = (tyPort) => async (taskList, opts) => {
615
612
  }
616
613
  return false;
617
614
  }).map((msg) => msg.task);
618
- const condition = opts.quitCondition ? subTasksCreated.filter(opts.quitCondition) : subTasksCreated.filter((t) => t.content.type === "message");
619
- const unsub = condition((m) => console.log("received matching message on port:", m));
620
- const lastMsg = await condition.wait(opts);
621
- console.log("finished processin all tasks!");
622
- unsub();
623
- return lastMsg;
615
+ return { initialIds, subTaskStream };
616
+ };
617
+ var processTasks = (tyPort) => {
618
+ const send = sendTasks(tyPort);
619
+ return async (taskList, quitCondition, opts) => {
620
+ const { subTaskStream } = await send(taskList, opts);
621
+ const condition = typeof quitCondition === "string" ? subTaskStream.filter((t) => t.content.type === quitCondition) : typeof quitCondition === "object" && Array.isArray(quitCondition) ? subTaskStream.filter((t) => quitCondition.includes(t.content.type)) : subTaskStream.filter(quitCondition);
622
+ const unsub = condition((m) => console.log("received matching message on port:", m));
623
+ const lastMsg = await condition.wait(opts);
624
+ console.log("finished processin all tasks!");
625
+ unsub();
626
+ return lastMsg;
627
+ };
624
628
  };
625
629
 
626
630
  // src/index.ts
@@ -638,7 +642,6 @@ var waitForApiChannel = (iframe) => {
638
642
  if (stopped) return;
639
643
  const channel = new MessageChannel();
640
644
  const targetOrigin = new URL(iframe.src, location.href).origin || "*";
641
- console.log("tyclient establishing iframe communication to", targetOrigin);
642
645
  const handleFirst = (ev) => {
643
646
  console.log("tyclient received first message from taskyon!", ev);
644
647
  stopped = true;
@@ -686,48 +689,58 @@ async function initializeTaskyon(options) {
686
689
  p[c.name] = c;
687
690
  return p;
688
691
  }, {});
689
- const taskyon = document.getElementById("taskyon");
692
+ const taskyon = document.getElementById(options.iframeId ?? "taskyon");
690
693
  const controller = new AbortController();
691
694
  const { x: clientSidePort, y: towardsIframe } = createDuplexChannel();
692
- if (taskyon !== null && taskyon.tagName === "IFRAME" && taskyon.contentWindow !== null) {
693
- console.log("make sure, we can ");
694
- const iframeMessagePort = await waitForApiChannel(taskyon);
695
- MessageChannelBridge(towardsIframe, iframeMessagePort);
696
- const send = (msg) => {
697
- console.log("tyclient sending", msg);
698
- clientSidePort.send(safeClone(msg));
699
- };
700
- console.log("tyclient send our configuration!");
695
+ if (!taskyon || taskyon.tagName !== "IFRAME" || taskyon.contentWindow === null)
696
+ throw new Error(`we could not find the taskyon iframe with id: ${options.iframeId}`);
697
+ console.log("make sure, we can ");
698
+ const iframeMessagePort = await waitForApiChannel(taskyon);
699
+ MessageChannelBridge(towardsIframe, iframeMessagePort);
700
+ const send = (msg) => {
701
+ console.log("tyclient sending", msg);
702
+ clientSidePort.send(safeClone(msg));
703
+ };
704
+ console.log("tyclient send our configuration!");
705
+ send({
706
+ type: "configurationMessage",
707
+ conf: options.configuration,
708
+ persist: options.persist,
709
+ origin: window.location.origin,
710
+ peerId: options?.name
711
+ });
712
+ console.log("tyclient sending our functions!");
713
+ options.tools.forEach((t) => {
714
+ const { function: _toolfunc, ...fdescr } = t;
701
715
  send({
702
- type: "configurationMessage",
703
- conf: options.configuration,
704
- persist: options.persist,
705
- origin: window.location.origin,
706
- peerId: options?.name
716
+ type: "functionDescription",
717
+ ...fdescr
707
718
  });
708
- console.log("tyclient sending our functions!");
709
- options.tools.forEach((t) => {
710
- const { function: _toolfunc, ...fdescr } = t;
711
- send({
712
- type: "functionDescription",
713
- ...fdescr
714
- });
715
- });
716
- console.log("tyclient set up function listener!");
717
- clientSidePort.receive((msg) => console.log("tyclient received message", msg));
718
- clientSidePort.receive.narrow((msg) => msg.type === "functionCall")(async (msg) => {
719
- const tool = toolMap[msg.functionName];
720
- if (tool) {
721
- const res = await handleFunctionExecution(msg.arguments ?? {}, tool, controller.signal);
722
- send({ type: "functionResponse", functionName: tool.name, response: res });
723
- console.log("tyclient tool send functionResponse to iframe", res, tool);
724
- }
725
- });
726
- }
719
+ });
720
+ console.log("tyclient set up function listener!");
721
+ clientSidePort.receive((msg) => console.log("tyclient received message", msg));
722
+ clientSidePort.receive.narrow((msg) => msg.type === "functionCall")(async (msg) => {
723
+ const tool = toolMap[msg.functionName];
724
+ if (tool) {
725
+ const res = await handleFunctionExecution(msg.arguments ?? {}, tool, controller.signal);
726
+ send({ type: "functionResponse", functionName: tool.name, response: res });
727
+ console.log("tyclient tool send functionResponse to iframe", res, tool);
728
+ }
729
+ });
727
730
  return {
728
- processTasks: processTasks(clientSidePort),
731
+ sendTasks: sendTasks(clientSidePort),
732
+ waitForTaskResult: processTasks(clientSidePort),
729
733
  port: clientSidePort,
730
- sendFile: (file) => sendFile(clientSidePort.send)(file)
734
+ sendFile: (file) => sendFile(clientSidePort.send)(file),
735
+ reconfigure: (options2) => {
736
+ send({
737
+ type: "configurationMessage",
738
+ conf: options2.configuration,
739
+ persist: options2.persist,
740
+ origin: window.location.origin,
741
+ peerId: options2?.name
742
+ });
743
+ }
731
744
  };
732
745
  }
733
746